Hi,
I’m trying to convert html
to pdf
document. Once I have created Document
object I want to set header and footer, but if header content is higher then page margin then the header will override the page content (footer is cut of by the end of the page). I have been reading some other tickets (e.g. ticket) and understood this is not supported, could you please confirm this? Are there any plans to implement the functionality to move content of the page down in this case to avoid overlapping? On other hand there is this ticket where ticket PDFNEWNET-38635 is created to address, I think this issue, but I’m not sure about this?
Also, I have understood this should work for adding header/footer while creating a new page (ticket), but I didn’t have luck with this either. Here is one of the examples I have tried:
Document document = new Document();
for (int i = 0; i < 3; i++)
{
Page page = document.Pages.Add();
HeaderFooter header = new HeaderFooter();
page.Header = header;
header.Margin = new MarginInfo(0, 0, 0, 20);
header.Paragraphs.Add(new TextFragment(
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
"Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n"));
Table table = new Table();
table.ColumnWidths = "20% 20% 20% 20% 20%";
table.BackgroundColor = Color.White;
for (int k = 0; k < 200; k++)
{
var row = table.Rows.Add();
for (int j = 0; j < 5; j++)
{
var cell = row.Cells.Add();
TextFragment text = new TextFragment("Engine");
cell.Paragraphs.Add(text);
}
}
page.Paragraphs.Add(table);
HeaderFooter footer = new HeaderFooter();
footer.Paragraphs.Add(new TextFragment(
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
"Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n"));
page.Footer = footer;
}
document.Save(@"C:\resultant.pdf");
Here is one not-working example using OnBeforePageGenerate
:
public static void Run()
{
Document doc = new Document();
Page page = doc.Pages.Add();
page.PageInfo.Height = PageSize.A4.Height;
page.PageInfo.Width = PageSize.A4.Width;
page.PageInfo.Margin = new MarginInfo(60, 60, 60, 60);
page.OnBeforePageGenerate += Page_OnBeforePageGenerate;
Table table = new Table();
table.ColumnWidths = "20% 20% 20% 20% 20%";
for (int i = 0; i < 200; i++)
{
var row = table.Rows.Add();
for (int j = 0; j < 5; j++)
{
var cell = row.Cells.Add();
TextFragment text = new TextFragment("Engine");
cell.Paragraphs.Add(text);
}
}
page.Paragraphs.Add(table);
doc.Save(@"C:\testheaderfooter.pdf");
}
private static void Page_OnBeforePageGenerate(Page page)
{
HeaderFooter footer = new HeaderFooter();
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
footer.Paragraphs.Add(new HtmlFragment("footer last page "));
page.Footer = footer;
HeaderFooter header = new HeaderFooter();
header.Paragraphs.Add(new HtmlFragment("header page first line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
header.Paragraphs.Add(new HtmlFragment("header page second line"));
page.Header = header;
}
Is there any way to solve this issue? Maybe embedding header/footer in html
document.
I have already tried this, but header
element is not processed as header but as page content.
Is there I way to get header height once it’s set so page margins could be adjusted?
The version of the library I’m using is 17.12; visual studio 2015 on win 10.
Sorry on so many questions and big post. I would appreciate any help with this issue. Thank you!
Kind regards,
Drazen