Hi all
I want to create headers and footers in a PDF Document.
The issue here is I can't say which height has the template because it depends on the user input.
So I tried to create a template with a dummy height, draw my stuff and keep track of the used height. Then I tried to set determined height to the template before I put it on the document.Template.Top.
But the all the drawings are gone.
What can I do to fix this issue.
Sincerly
Daniel Hüttenberger
A simplified code:
var doc = new PdfDocument();
doc.AddPage();
var template = new PdfPageTemplate(0,0, doc.Pages[0].GetClientSize().Width, 10f);
// ... drawing onto the template graphics. Measuring the height of each drawn string.
template.Height = 26f; // assuming it is the measured height
doc.Template.Footer = template;
// Saving and all the stuff
The footer in this example is there but with no text.
First, we need to define and assign the header and footer details to the document. The page sizes will be adjusted based on the header and footer areas. If we add the header and footer later, the page sizes will be inconsistent. Therefore, we recommend measuring the size of the content for the header and footer first, and then setting up the header and footer according to the measured size to avoid this issue.
Thank you for your response.
So creating the the header before adding any Page?
Example:
var doc =newPdfDocument();
var template= new PdfPageTemplate(0,0, 0,10f);
// ... drawing onto the template graphics. Measuring the height of each drawn string.
template.Height=26f;// assuming it is the measured height
doc.Template.Footer=template;
doc.AddPage();
// Saving and all the stuff
I don't understand why it works if I set the height (suggesting a height) in the PdfPageTemplage Constructor.
But if I change the height before it is set onto the document, it is not working.
That's not logical to me.
I also cannot believe that everybody nows all the information which is going into the the templates. So that they can exactly estimate the height?
Hei Daniel
I had the exact same problem and managed to create a workaround for myself. Here's what I did:
I created a header template with a fixed height that is much larger than needed. Then, I added my dynamic content and made sure to always know what the highest Y-point is in the header template.
At the end, I created a new header template, this time with the correct height, and copied the graphics from the header template with the fixed height.
To add content after the header, meaning not in the header template but on the page, you can use the GET property of the header.
It's not an elegant solution, but it works.
Note its also possible with PdfPageTemplateElement.
namespace Comatic.Lehmann.CustomerPortal.Application.Services;
public class SamplePrinter
{
public void DocumentSample()
{
var doc = new PdfDocument();
var page = doc.Pages.Add();
var myTexts = new List<string>()
{
"Text 1",
"Text 2",
"Text 3",
};
// hader 1
var header = PrintDynamicHeightHeader(doc, myTexts);
page.Graphics.DrawPdfTemplate(header, new PointF());
// or hader 2
doc.Template.Top = header;
var graphics = page.Graphics;
// add first content to page with help of the header height
var pageContentText =
new PdfTextElement("My first page text")
.Draw(doc.Pages[doc.PageCount - 1],
new PointF(0, header.Height));
}
private PdfPageTemplateElement PrintDynamicHeightHeader(PdfDocumentpdfDocument, List<string?> headerContent)
{
var highestYPointInHader = 0;
// important: the height of the header template is dynamic, this value is never relevant because
// at the end a copy of the header template is drawn with the correct height (dynamic height)
var bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 500);
var fixHeightHeaderTemplate = new PdfTemplate(bounds);
// text styles
var headerTextFont = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
// NOTE: Add here your content and make sure you know the height of the content
// SAMPLE: with texts as content
// print header content
var lastLinePrintYPoint = 0;
foreach (var line in headerContent)
{
if (!string.IsNullOrEmpty(line))
{
fixHeightHeaderTemplate.Graphics.DrawString(line, headerTextFont,
PdfBrushes.Black, new PointF(0, lastLinePrintYPoint));
lastLinePrintYPoint += 10;
}
}
// update highest point after add content to header template
highestYPointInHader = lastLinePrintYPoint;
// set dynamic the correct height of the header template,
// take the graphic of the header template and draw it to a new template with the correct height
var dynamicHeightHeaderTemplate =
new PdfPageTemplateElement(new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width,
highestYPointInHader + 5));
dynamicHeightHeaderTemplate.Graphics.DrawPdfTemplate(fixHeightHeaderTemplate, new PointF(0, 0));
// outside the function use for drawing outside the header:
// dynamicHeightHeaderTemplate.Height;
return dynamicHeightHeaderTemplate;
}
}
As we said earlier, we don't have direct support to add a dynamic header in a PDF document. However, as a workaround we have prepared a sample to achieve the dynamic header. Kindly try this on your end and let us know the details.
Code snippet:
static void DynamicHeader() //Get dynamic header and assign it to the document.
//Draw the text. //Add a new page //Save the document. File.WriteAllBytes("DynamicHeader.pdf", stream.ToArray()); //Create font. float y = 0; var myTexts = new List<string>() //Draw the dynamic text in the template return headerTemplate; |