Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Tue Mar 15, 2016 2:24 pm
I am trying to draw a table and add it to a PDF file being created. However, because the amount of data in the dataset, the table spans multiple pages. I am trying to draw a footer on every page of the file, but after the first page, the subsequent pages created do not have a footer, because the Page.Canvas property is null and I get an object reference not set to an instance of an object error when it tries to draw the footer. Is there a way to force Sprire to generate a footer on every page of the file even when the pages are generated by a table.DataSource that spans multiple pages?
-

cbaxter
-
- Posts: 2
- Joined: Tue Mar 15, 2016 2:18 pm
Wed Mar 16, 2016 3:05 am
Hi,
Thanks for your posting.
Please use Template function to implement it.
A demo showing Template function:
http://www.e-iceblue.com/Tutorials/Spir ... B.NET.htmlSample code:
- Code: Select all
static void AddFooter()
{
PdfDocument doc = new PdfDocument();
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(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
PdfSection section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = new PdfMargins(0);
PdfNewPage page = section.Pages.Add();
//Draw table on page
DrawPage(page);
doc.SaveToFile("Template.pdf");
System.Diagnostics.Process.Start("Template.pdf");
}
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
PdfPageTemplateElement leftSpace
= new PdfPageTemplateElement(margin.Left, pageSize.Height);
doc.Template.Left = leftSpace;
PdfPageTemplateElement topSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Regular));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
PdfPen pen = new PdfPen(Color.Black, 0.75f);
PdfPageTemplateElement rightSpace
= new PdfPageTemplateElement(margin.Right, pageSize.Height);
doc.Template.Right = rightSpace;
PdfPageTemplateElement bottomSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
bottomSpace.Foreground = true;
doc.Template.Bottom = bottomSpace;
//draw footer label
float y = font.Height + 1;
bottomSpace.Graphics.SetTransparency(0.5f);
bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.AutomaticFields
= new PdfAutomaticField[] { pageNumber, pageCount };
pageNumberLabel.Brush = PdfBrushes.Black;
pageNumberLabel.Font = font;
pageNumberLabel.StringFormat = format;
pageNumberLabel.Text = "page {0} of {1}";
pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);
}
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3011
- Joined: Wed Jun 27, 2012 8:50 am
Thu Mar 17, 2016 7:20 am
Hi,
Has your issue been resolved?
Thanks for your feedback.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3011
- Joined: Wed Jun 27, 2012 8:50 am
Thu Mar 31, 2016 7:16 pm
I'm not seeing in your example where you are actually creating and adding the table to the page.
You have the lines:
- Code: Select all
PdfNewPage page = section.Pages.Add();
//Draw table on page
DrawPage(page);
but you're not actually creating a PdfTable instance here.
My code is basically
- Code: Select all
var table = new PdfTable();
table.Style = ........
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat {....};
table.Draw(page, new PointF(0,y), tablelayout);
DrawFooter(page, margin)
Could you please provide an example where you are actually creating a PdfTable instance, then adding it to the page with the footer that will appear on every page when the table spans more than one page. Thanks.
-

cbaxter
-
- Posts: 2
- Joined: Tue Mar 15, 2016 2:18 pm
Fri Apr 01, 2016 3:18 am
Hi,
Thanks for your further posting.
Here is my whole sample code. Hope it helps.
- Code: Select all
static void AddFooter()
{
PdfDocument doc = new PdfDocument();
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(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
PdfSection section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = new PdfMargins(0);
PdfNewPage page = section.Pages.Add();
//Draw table on page
DrawPage(page);
doc.SaveToFile("Template.pdf");
System.Diagnostics.Process.Start("Template.pdf");
}
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
PdfPageTemplateElement leftSpace
= new PdfPageTemplateElement(margin.Left, pageSize.Height);
doc.Template.Left = leftSpace;
PdfPageTemplateElement topSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Regular));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
PdfPen pen = new PdfPen(Color.Black, 0.75f);
PdfPageTemplateElement rightSpace
= new PdfPageTemplateElement(margin.Right, pageSize.Height);
doc.Template.Right = rightSpace;
PdfPageTemplateElement bottomSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
bottomSpace.Foreground = true;
doc.Template.Bottom = bottomSpace;
//draw footer label
float y = font.Height + 1;
bottomSpace.Graphics.SetTransparency(0.5f);
bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.AutomaticFields
= new PdfAutomaticField[] { pageNumber, pageCount };
pageNumberLabel.Brush = PdfBrushes.Black;
pageNumberLabel.Font = font;
pageNumberLabel.StringFormat = format;
pageNumberLabel.Text = "page {0} of {1}";
pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);
}
static void DrawPage(PdfPageBase page)
{
float y = 10;
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
y = y + font1.MeasureString("Part List", format1).Height;
y = y + 5;
//create data table
PdfTable table = new PdfTable();
table.Style.CellPadding = 1;
table.Style.BorderPen = new PdfPen(brush1, 0.75f);
table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f), true);
table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold), true);
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.ShowHeader = true;
table.Style.RepeatHeader = true;
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\demo.mdb";
OleDbCommand command = new OleDbCommand();
command.CommandText
= " select * from parts ";
command.Connection = conn;
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
dataTable.Columns.RemoveAt(1);
table.DataSourceType = PdfTableDataSourceType.TableDirect;
table.DataSource = dataTable;
}
}
float width
= page.Canvas.ClientSize.Width
- (table.Columns.Count + 1) * table.Style.BorderPen.Width;
for (int i = 0; i < table.Columns.Count; i++)
{
if (i == 1)
{
table.Columns[i].Width = width * 0.4f * width;
table.Columns[i].StringFormat
= new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
}
else
{
table.Columns[i].Width = width * 0.12f * width;
table.Columns[i].StringFormat
= new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
}
}
table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout);
PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat();
tableLayout.Break = PdfLayoutBreakType.FitElement;
tableLayout.Layout = PdfLayoutType.Paginate;
PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);
y = result.Bounds.Bottom + 5;
PdfBrush brush2 = PdfBrushes.Gray;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
result.Page.Canvas.DrawString(String.Format("* All {0} parts in the list", table.Rows.Count),
font2, brush2, 5, y);
}
static void table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
if (args.RowIndex < 0)
{
//header
return;
}
if (args.RowIndex % 3 == 0)
{
args.CellStyle.BackgroundBrush = PdfBrushes.LightYellow;
}
else
{
args.CellStyle.BackgroundBrush = PdfBrushes.SkyBlue;
}
}
Welcome to write to us again for further problems.
Best Regards,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3011
- Joined: Wed Jun 27, 2012 8:50 am