Hello alacroce,
Thanks for your inquiry.
For requirement one. Please refer to the below code to draw table in the header and footer.
- Code: Select all
//create new PDF document
PdfDocument doc = new PdfDocument();
//set margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
//apply template in PDF document
SetDocumentTemplate1(doc, PdfPageSize.A4, margin);
//add two pages in PDF
PdfPageBase page = doc.Pages.Add();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Italic));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
page.Canvas.DrawString("This is page1",font,PdfBrushes.Brown,new PointF());
PdfPageBase page2 = doc.Pages.Add();
page2.Canvas.DrawString("This is page2", font, PdfBrushes.Brown, new PointF());
doc.SaveToFile("TableHeaderFooter.pdf");
protected void SetDocumentTemplate1(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
//set a top page template
PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
footerSpace.Foreground = true;
doc.Template.Bottom = footerSpace;
String[] data
= {
"PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
"900;Dive kayak;24;16;1356.75;3999.95",
"912;Underwater Diver Vehicle;5;3;504;1680",
"1313;Regulator System;165;216;117.5;250",
"1314;Second Stage Regulator;98;88;124.1;365",
};
String[][] dataSource
= new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
dataSource[i] = data[i].Split(';');
}
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.DataSource = dataSource;
foreach (PdfColumn column in table.Columns)
{
column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}
//draw table in the header
table.Draw(topSpace.Graphics);
//draw table in the footer
table.Draw(footerSpace.Graphics);}
For requirement two, sorry it impossible to get the header and footer from an existing pdf because there's no concept of header and footer in a pdf and everything is drawn on the cavas.
For requirement three, if this is a new pdf, you could draw the headers for every page respectively excluding the first two pages. Below is the code for your reference.
- Code: Select all
//create new PDF document
PdfDocument doc = new PdfDocument();
//set margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(0.635f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Left = unitCvtr.ConvertUnits(0.635f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 9f, FontStyle.Regular));
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
PdfBrush brush = PdfBrushes.Red;
PointF location = new PointF(0, 100);
page.Canvas.DrawString("This is the first page", font1, brush, location);
PdfPageBase page2 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
page2.Canvas.DrawString("This is the second page", font1, brush, location);
PdfPageBase page3 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
page3.Canvas.DrawString("This is the third page", font1, brush, location);
AddHeaderforPage(page3);
AddFooterforPage(page3);
PdfPageBase page4 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
page4.Canvas.DrawString("This is the fourth page", font1, brush, location);
AddHeaderforPage(page4);
AddFooterforPage(page4);
doc.SaveToFile(@"headerfooter.pdf");
private void AddHeaderforPage(PdfPageBase page)
{
String[] data
= {
"PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
"900;Dive kayak;24;16;1356.75;3999.95",
"912;Underwater Diver Vehicle;5;3;504;1680",
"1313;Regulator System;165;216;117.5;250",
"1314;Second Stage Regulator;98;88;124.1;365",
};
String[][] dataSource
= new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
dataSource[i] = data[i].Split(';');
}
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.DataSource = dataSource;
foreach (PdfColumn column in table.Columns)
{
column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}
//draw table in the header
table.Draw(page,new PointF());
}
private void AddFooterforPage(PdfPageBase page)
{
String[] data
= {
"PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
"900;Dive kayak;24;16;1356.75;3999.95",
"912;Underwater Diver Vehicle;5;3;504;1680",
"1313;Regulator System;165;216;117.5;250",
"1314;Second Stage Regulator;98;88;124.1;365",
};
String[][] dataSource
= new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
dataSource[i] = data[i].Split(';');
}
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.DataSource = dataSource;
foreach (PdfColumn column in table.Columns)
{
column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
}
//draw table in the footer
table.Draw(page,new PointF(20,650));
}
Sincerely,
Jane
E-iceblue support team