I am trying to set the font and size of the page numbering of a generated word document.
I am unable to set the font and size of the text for the "numbering" elements.
(Example of how I want it to look)
Company Name | ||
Unauthorized use or reproduction is prohibited | Page 1 of 1 |
I tried the "numbering" as IWTextRange so I could set the font and size (like I did with the contents of cell 2), but they come out on different lines (example below) and would not align right. I had to add the "numbering" elements as an IWParagraph to keep them all on one line.
So at this point its choice between font and size or alignment and wrapping.
(Example of what it looks like when "numbering" elements are an IWTextRange)
Company Name | ||
Unauthorized use or reproduction is prohibited | Page 1 of 1 |
(Example of what it looks like when "numbering" elements are an IWParagraph)
Company Name | ||
Unauthorized use or reproduction is prohibited | Page 1 of 1 |
Below is my code.
private void AddFooter(IWSection section)
{
// Add a new table to the header
IWTable table = section.HeadersFooters.Footer.AddTable();
// Set no border
table.TableFormat.Borders.BorderType = BorderStyle.None;
// Set border line for top
table.TableFormat.Borders.Top.BorderType = BorderStyle.Single;
// Inserting table with 2 rows and 3 columns
table.ResetCells(2, 3);
#region Owner and Usage
// Add text to row 1, cell 2
IWTextRange textRange = table[0, 1].AddParagraph().AppendText($"Property of {Application.AppSettings("CompanyName")}");
AlignTableCell(table[0, 1], HorizontalAlignment.Center);
SetHeaderFooterFont(textRange);
// Add text to row 2, cell 2
textRange = table[1, 1].AddParagraph().AppendText("Unauthorized use or reproduction is prohibited");
AlignTableCell(table[1, 1], HorizontalAlignment.Center);
SetHeaderFooterFont(textRange);
#endregion
#region Page Numbering
// Add text to row 2, cell 3
IWParagraph paragraph = table[1, 2].AddParagraph();
paragraph.AppendText("Page ");
AlignTableCell(table[1, 2], HorizontalAlignment.Right, VerticalAlignment.Bottom);
WField field = (WField)paragraph.AppendField("Page", FieldType.FieldPage);
AlignTableCell(table[1, 2], HorizontalAlignment.Right, VerticalAlignment.Bottom);
paragraph.AppendText(" of ");
AlignTableCell(table[1, 2], HorizontalAlignment.Right, VerticalAlignment.Bottom);
field = (WField)paragraph.AppendField("NumPages", FieldType.FieldNumPages);
AlignTableCell(table[1, 2], HorizontalAlignment.Right, VerticalAlignment.Bottom);
#region Page Number Settings
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageStartingNumber = 1;
section.PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
#endregion Page Number Settings
#endregion
}
private void AlignTableCell(WTableCell cell, HorizontalAlignment hAlign, VerticalAlignment vAlign = VerticalAlignment.Top)
{
cell.CellFormat.VerticalAlignment = vAlign;
cell.Paragraphs[0].ParagraphFormat.HorizontalAlignment = hAlign;
}
private void SetHeaderFooterFont(IWTextRange textRange)
{
textRange.CharacterFormat.FontName = "Open Sans";
textRange.CharacterFormat.FontSize = 8;
textRange.CharacterFormat.CharacterSpacing = .5f;
}
Thanks for any assistance you can offer,
Matt
Thanks,
That did the trick.