Hi Lisa,
Great! Your code snippet leads me to the solution.
1. Create document and add all pages but set the margins so that we have space for the header/footer.
2. Save and reload the document
(resets all margins to zero), so i can put new elements on any place.
This is even better in terms of freedom then using the
PdfPageTemplateElement.
Thank's for your support.
- Code: Select all
string headerText = "Page: ";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 15f));
SizeF size = font.MeasureString(headerText);
System.IO.Stream stream = new System.IO.MemoryStream();
pdfDoc.SaveToStream(stream);
PdfDocument pdfDocNew = new PdfDocument();
pdfDocNew.LoadFromStream(stream);
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
pdfDocNew.Pages[i].Canvas.DrawString(headerText + (i+1) , font, PdfBrushes.RoyalBlue, 10,10);
}
Lisa.Li wrote:Hello,
Thanks for your inquiry.
Please refer to the following sample code to add Header and Footer when the page orientation changes between pages. If there is any question, please provide your testing file as well as your desired effect of output, then we will look into it and guide you accordingly. You could send your file to us via email(
[email protected]).
- Code: Select all
static void Main(string[] args)
{
PdfDocument existingPdf = new PdfDocument();
existingPdf.LoadFromFile(@"test.pdf");
//create a new PDF file
PdfDocument newPdf = new PdfDocument();
PdfMargins margins = new PdfMargins(90, 72, 90, 72);
foreach (PdfPageBase page in existingPdf.Pages)
{
PdfPageBase newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
newPdf.Template.Top = CreateHeaderTemplate(newPdf,page, margins);
newPdf.Template.Bottom = CreateFooterTemplate(newPdf,page, margins);
newPdf.Template.Left = new PdfPageTemplateElement(margins.Left, newPdf.PageSettings.Size.Height);
newPdf.Template.Right = new PdfPageTemplateElement(margins.Right, newPdf.PageSettings.Size.Height);
newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(-margins.Left, -margins.Top));
}
newPdf.SaveToFile("result.pdf");
}
//create template of header
static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
{
SizeF pageSize = page.Size;
PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
headerSpace.Foreground = false;
float x = margins.Left;
float y = 0;
//draw picture in headerSpace
PdfImage headerImage = PdfImage.FromFile(@"logo.png");
float width = headerImage.Width / 3;
float height = headerImage.Height / 3;
headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);
//draw line in headerSpace
PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);
return headerSpace;
}
//create template of footer
static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
{
SizeF pageSize = page.Size;
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
footerSpace.Foreground = false;
float x = margins.Left;
float y = 0;
//draw line in footerSpace
PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
//draw text in footerSpace
y = y + 5;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String footerText = "test";
footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format);
return footerSpace;
}
Sincerely,
Lisa
E-iceblue support team