Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 19, 2015 8:34 pm

I need to create a header that has:

image HorixzonalAlignment.Left
table with 6 Fields to the right of the image, left justified and right justified.
See image that I have attached.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Wed May 20, 2015 7:43 am

Hello,

Thanks for your inquiry.
Please refer to the following code:
Code: Select all
           Document doc = new Document();
            Section sec = doc.AddSection();         
            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
            Paragraph paragraph = header.AddParagraph();
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;         
            DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"F:\image\image1.jpg"));
                         
            Table  table =  header.AddTable();
            table.ResetCells(3, 2);

            table.TableFormat.WrapTextAround = true;           
            table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
            table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
            table.TableFormat.Positioning.VertPosition = 43;           
             
           
            String[][] data = {
                                  new string[] {"1.left","1.right"},
                                  new string[] {"2.left","2.right"},
                                  new string[] {"3.left","3.right"}
                              };
           
             for (int r = 0; r < 3; r++)
             {
                 TableRow dataRow = table.Rows[r];
                 for (int c = 0; c < 2; c++)
                 {
                     if (c == 0)
                     {
                         Paragraph par = dataRow.Cells[c].AddParagraph();
                         par.AppendText(data[r][c]);
                         par.Format.HorizontalAlignment = HorizontalAlignment.Left;
                         dataRow.Cells[c].Width = 180;
                     }
                     else
                     {
                         Paragraph par = dataRow.Cells[c].AddParagraph();
                         par.AppendText(data[r][c]);
                         par.Format.HorizontalAlignment = HorizontalAlignment.Right;
                         dataRow.Cells[c].Width = 180;
                     }                   
                 }
             }
             doc.SaveToFile("result.docx", FileFormat.Docx);


Best Regards,
Betsy
E-iceblue support team
Last edited by Betsy on Thu May 21, 2015 8:49 am, edited 1 time in total.
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu May 21, 2015 8:29 am

Hello,

Have your tried the code I provided ? Has it fulfilled your requirement ?

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu May 21, 2015 3:05 pm

Well it is working but look at how it looks on multiple pages. see my attachment.

Code: Select all
Document document = new Document();
Section section = document.AddSection();
section.PageSetup.PageSize = PageSize.A3;
section.PageSetup.Orientation = PageOrientation.Portrait;

InsertHeaderAndFooter(section);

section = document.AddSection();
section.AddParagraph().AppendText("1");
section = document.AddSection();
section.AddParagraph().AppendText("2");
section = document.AddSection();
section.AddParagraph().AppendText("3");

// save
document.SaveToFile(@"E:\temp\test.docx", FileFormat.Docx2010);
document.SaveToFile(@"E:\temp\test.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE", @"E:\temp\test.docx");

private static void InsertHeaderAndFooter(Section section)
{
    HeaderFooter header1 = section.HeadersFooters.Header;
    HeaderFooter footer = section.HeadersFooters.Footer;
    InsertHeader0(section);

    //insert pagenumber in footer
    Paragraph footerParagraphRight = footer.AddParagraph();
    footerParagraphRight.AppendField("page number", FieldType.FieldPage);
    footerParagraphRight.AppendText(" of ");
    footerParagraphRight.AppendField("number of pages", FieldType.FieldNumPages);
    footerParagraphRight.Format.HorizontalAlignment = HorizontalAlignment.Right;

}

private static void InsertHeader0(Section section)
{
   HeaderFooter header = section.HeadersFooters.Header;
   Paragraph paragraph = header.AddParagraph();
   paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
   DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"E:\MACLogoSharp.jpg"));
   headerimage.HeightScale = 40;
   headerimage.WidthScale = 40;

   Table table = header.AddTable();
   table.ResetCells(3, 2);

   table.TableFormat.WrapTextAround = true;
   table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
   table.TableFormat.Positioning.VertPosition = 43;


   String[][] data = {
                            new string[] {"Elgin, City of (General Obligation Debt)","Last Revised 10/12/2014"},
                            new string[] {"","TMR # 1447"},
                            new string[] {"Bastrop, Travis Counties","Page 1 of 11"}
                        };

   for (int r = 0; r < 3; r++)
   {
      TableRow dataRow = table.Rows[r];
      for (int c = 0; c < 2; c++)
      {
         if (c == 0)
         {
            Paragraph par = dataRow.Cells[c].AddParagraph();
            par.AppendText(data[r][c]);
            par.Format.HorizontalAlignment = HorizontalAlignment.Left;
            dataRow.Cells[c].Width = 180;
         }
         else
         {
            Paragraph par = dataRow.Cells[c].AddParagraph();
            par.AppendText(data[r][c]);
            par.Format.HorizontalAlignment = HorizontalAlignment.Right;
            dataRow.Cells[c].Width = 180;
         }
      }
   }
}


terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Fri May 22, 2015 2:20 am

Hello,

Thanks for your feedback. Please add the following code:
Code: Select all
….
   table.TableFormat.HorizontalAlignment = RowAlignment.Right;
   table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
….
.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri May 22, 2015 2:49 pm

Betsy, thank you for your help.

I have attached my cs code and a snip of what the header looks like.

1. I need the header contents to spread out over the entire header so the cells don't wrap.
2. I also would like to add the page numbers to the bottom right cell in the header.
3. Note how the page paragraph is mingled with the header contents.
4. Can the evaluation warning message move down because we will never know if this is working correctly.

Thank you.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Mon May 25, 2015 2:51 am

Hello,

Please refer to the following code:
Code: Select all
Document document = new Document();
               Section section = document.AddSection();
               section.PageSetup.PageSize = PageSize.A3;
               section.PageSetup.Orientation = PageOrientation.Portrait;

               InsertHeaderAndFooter(section);
               section = document.AddSection();           
               section = document.AddSection();             
               section = document.AddSection();
           
               // save
               document.SaveToFile(@"test.docx", FileFormat.Docx2010);           
               System.Diagnostics.Process.Start("test.docx");
        }

             private static void InsertHeaderAndFooter(Section section)
               {
                 HeaderFooter header1 = section.HeadersFooters.Header;
                 HeaderFooter footer = section.HeadersFooters.Footer;
                 InsertHeader0(section);
 
             }

             private static void InsertHeader0(Section section)
             {
                 HeaderFooter header = section.HeadersFooters.Header;
                 Paragraph paragraph = header.AddParagraph();
                 paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
                 DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"F:\image\5040.jpg"));
                 headerimage.HeightScale = 60;
                 headerimage.WidthScale = 60;

                 Table table = header.AddTable();
                 table.ResetCells(3, 2);           
                 table.TableFormat.WrapTextAround = true;
               
                 //set the position of the table
                 table.TableFormat.HorizontalAlignment = RowAlignment.Right;
                 table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
                 table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
                 table.TableFormat.Positioning.VertPosition = 20; // please modify this value if the page paragraph is mingled with the header contents.

                 //add string into the table
                 String[][] data = {
                            new string[] {"Elgin, City of (General Obligation Debt)","Last Revised 10/12/2014"},
                            new string[] {"","TMR # 1447"},
                            new string[] {"Bastrop, Travis Counties",""}
                        };

                 for (int r = 0; r < 3; r++)
                 {
                     TableRow dataRow = table.Rows[r];
                     for (int c = 0; c < 2; c++)
                     {
                         if (c == 0)
                         {
                             Paragraph par = dataRow.Cells[c].AddParagraph();
                             par.AppendText(data[r][c]);
                             par.Format.HorizontalAlignment = HorizontalAlignment.Left;
                            // set the table width to fit the text lenght
                             dataRow.Cells[c].Width = 260;
                         }
                         else
                         {
                             Paragraph par = dataRow.Cells[c].AddParagraph();
                             par.AppendText(data[r][c]);
                             par.Format.HorizontalAlignment = HorizontalAlignment.Right;
                             //set the table width to fit the text lenght
                             dataRow.Cells[c].Width = 260;
                         }
                     }
                 }

                 //add the page numbers to the bottom right cell in the header
                 Paragraph footerParagraphRight = table.Rows[2].Cells[1].Paragraphs[0];
                 footerParagraphRight.AppendField("page number", FieldType.FieldPage);
                 footerParagraphRight.AppendText(" of ");
                 footerParagraphRight.AppendField("number of pages", FieldType.FieldNumPages);
                 footerParagraphRight.Format.HorizontalAlignment = HorizontalAlignment.Right;

             }

You can contact our sales team (sales@e-iceblue.com) to get temporary license for one month to remove the warning information. In addition, there is nothing in your file (SpireDemo1.cs).

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue May 26, 2015 8:42 am

Hello,

Have you tried the code ? Does it fulfill your needs ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Tue May 26, 2015 1:56 pm

Betsy, I am sorry I did not include the cs file.
I have attached it. Could you look at that code please?

And, I would like the page numbers to be in the upper right of the page header on the last line, cell 3,3.

Thank you.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Wed May 27, 2015 2:29 am

Hello,

Thanks for sharing.
I have attached the code, please download it and have a try.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu May 28, 2015 8:03 am

Hello,

Have you tried the code? Has your issue been resolved ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu May 28, 2015 10:21 pm

I am having trouble getting my header to all fit with the logo.

See attached program.cs and logo file.

Thank you for your help.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Fri May 29, 2015 8:23 am

Hello,

Thanks for sharing.
I have attached the code modified. If I miss something, please let me know.

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Mon Jun 01, 2015 10:12 pm

Betsy, from your response, I can tell you have not tried my new code. I have refactored the code and the code you sent me was old. Please run my code and note that I included the image. Note how the header text is "over" the logo.

Thank you.

terrence@mactexas.com
 
Posts: 95
Joined: Tue May 19, 2015 8:09 pm

Tue Jun 02, 2015 1:49 am

Hello,

Thanks for your feedback.
I had tried your code and modified the code, please run the code and see the result. I had noted in your result document that the logo is too small, so I changed the table’s size and position to fit the logo. About adding string into the table, I use the old code because I think it is far more succinct.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc