This project has moved and is read-only. For the latest updates, please go here.

Is there any way to add table in First Page of existing word document

Feb 11, 2014 at 7:21 AM
Edited Feb 11, 2014 at 7:22 AM
I want to add table with some data in first page of existing word document.


How can I achieve this using Docx Library
Feb 11, 2014 at 10:00 AM
Have you tried to use the example described here? http://cathalscorner.blogspot.com/2010/06/docx-and-tables.html
Feb 11, 2014 at 10:42 AM
Edited Feb 11, 2014 at 10:43 AM
Yes i have done exactly given in this link but it adds table at the last page not at the first page because..

..

I want to add table in First page only..
Feb 11, 2014 at 1:29 PM
Edited Feb 11, 2014 at 1:40 PM
Okey. You just need to turn to the first paragraph and insert a table in front of him.
                var firstParagraph = document.Paragraphs.FirstOrDefault() ?? document.InsertParagraph("",false);
                firstParagraph.InsertTableBeforeSelf(t);
And just in case the complete code.
        private static void Main(string[] args){
            using (DocX document = DocX.Load(@"DocXInsertTable.docx")){
                Table t = document.AddTable(2, 3);
                t.Alignment = Alignment.center;
                t.Design = TableDesign.MediumGrid1Accent2;
                 t.Rows[0].Cells[0].Paragraphs.First().Append("A");
                t.Rows[0].Cells[1].Paragraphs.First().Append("B");
                t.Rows[0].Cells[2].Paragraphs.First().Append("C");
                t.Rows[1].Cells[0].Paragraphs.First().Append("D");
                t.Rows[1].Cells[1].Paragraphs.First().Append("E");
                t.Rows[1].Cells[2].Paragraphs.First().Append("F");

                var firstParagraph = document.Paragraphs.FirstOrDefault() ?? document.InsertParagraph("",false);
                firstParagraph.InsertTableBeforeSelf(t);

                document.Save();
            }
        }
Marked as answer by kalpesh_vaghela on 2/11/2014 at 10:33 PM
Feb 12, 2014 at 6:32 AM
Edited Feb 12, 2014 at 6:33 AM
Thanks for reply.

Using this code i can add table in first page successfully but my requirement is little bit different

suppose following is my existing document

         --------------------------------------
         |    Existing Content                   |
         ---------------------------------------

After using your given code it looks like because i my case table contains only 4-5 rows so it occupy less space so existing contains remains in first page only

        -------------------------------------
       |  Inserted Table                        |
       ---------------------------------------
       ---------------------------------------
       |  Existing Content                         |
       ---------------------------------------

so i want to add table in first page as well as existing contain must move to second page so at last first page must contains only table..

Something like

First Page
       -------------------------------------------
       | Inserted Table                               |
       --------------------------------------------

Second Page
            --------------------------------------
           |      Existing Contains                |

---------------------------------------

Can we achieve this using "Docx" library ?


One more thing using open xml sdk directly we can achieve this but i need to use "Docx" library because i want to add image in table and that requirement i could not achieve using directly open xml sdk.

Thanks in advance
Feb 12, 2014 at 7:19 AM
Edited Feb 12, 2014 at 8:33 AM
HI chekakis,

I found work arround for this

First a have open word document with Open XML SDK and Add paragraph at first page with Page Break and save file

again i have opened document using Docx Library and added table according to your suggestion and it works

Code
using (OpenXML.Packaging.WordprocessingDocument wordDoc = OpenXML.Packaging.WordprocessingDocument.Open(stream1, true))
                        {
                            OpenXML.Wordprocessing.Paragraph para = new OpenXML.Wordprocessing.Paragraph(new OpenXML.Wordprocessing.Run(new OpenXML.Wordprocessing.Break() { Type = OpenXML.Wordprocessing.BreakValues.Page }));
                            wordDoc.MainDocumentPart.Document.Body.InsertAt(para, 0);
                            wordDoc.MainDocumentPart.Document.Save();
                            // Saving File Here
                       }       

using (DocX docx = DocX.Load(stream))
                 { 
                Table t = document.AddTable(2, 3);
                t.Alignment = Alignment.center;
                t.Design = TableDesign.MediumGrid1Accent2;
                 t.Rows[0].Cells[0].Paragraphs.First().Append("A");
                t.Rows[0].Cells[1].Paragraphs.First().Append("B");
                t.Rows[0].Cells[2].Paragraphs.First().Append("C");
                t.Rows[1].Cells[0].Paragraphs.First().Append("D");
                t.Rows[1].Cells[1].Paragraphs.First().Append("E");
                t.Rows[1].Cells[2].Paragraphs.First().Append("F");

                var firstParagraph = document.Paragraphs.FirstOrDefault() ?? document.InsertParagraph("",false);
                firstParagraph.InsertTableBeforeSelf(t);

                document.Save();
}
But if you find this using "docx" library then let me know because in this case i need to open word file twice so it may be performance issue if file is large.
Marked as answer by kalpesh_vaghela on 2/11/2014 at 11:19 PM
Feb 12, 2014 at 7:59 AM
Yes, it can be done using novacode :)
Perhaps there is a more elegant way to solve your problem. This is the first I could find.
Replace this code
var firstParagraph = document.Paragraphs.FirstOrDefault() ?? document.InsertParagraph("",false);
firstParagraph.InsertTableBeforeSelf(t);
on this one
var firstParagraph = document.Paragraphs.FirstOrDefault() ?? document.InsertParagraph("",false);
firstParagraph.InsertParagraphBeforeSelf("");
firstParagraph = document.Paragraphs.First();
firstParagraph.InsertTableBeforeSelf(t);
firstParagraph.InsertPageBreakAfterSelf();
Marked as answer by kalpesh_vaghela on 2/12/2014 at 12:33 AM
Feb 12, 2014 at 8:33 AM
It works....


Thanks