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.
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);
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;
}
}
}
}
….
table.TableFormat.HorizontalAlignment = RowAlignment.Right;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
….
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;
}