我正在尝试使用ITextEvent
为我的PDF添加页眉和页脚我的OnEndPage
方法如下所示。
以下一行:
pdfTab.AddCell(pdfCell3);
抛出错误“对象引用未发送到对象的实例”
如果我不添加第3个单元格,则其他两个单元格会正常添加并且pdf会正确呈现。
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
base.OnEndPage(writer, document);
BaseFont Calibri = BaseFont.CreateFont(@"C:\windows\fonts\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
BaseFont CalibriBold = BaseFont.CreateFont(@"C:\windows\fonts\calibrib.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
Font baseFontSmall = new Font(Calibri, 10, Font.BOLD, Color.BLACK);
Font baseFontNormal = new Font(Calibri, 12, Font.NORMAL, Color.BLACK);
Font baseFontBig = new Font(CalibriBold, 12, Font.BOLD, Color.BLACK);
Phrase p1Header = new Phrase("Sample Header Here", baseFontNormal);
//Create PdfTable object
PdfPTable pdfTab = new PdfPTable(2);
create separate cells to include image logo and 2 separate strings
//Row 1
PdfPCell pdfCell1 = new PdfPCell();
PdfPCell pdfCell2 = new PdfPCell(new Phrase("Doctors name", baseFontNormal));
//row2
PdfPCell pdfCell3 = new PdfPCell(new Phrase("Date and stuff", baseFontNormal));
pdfCell1.Colspan = 1;
pdfCell2.Colspan = 1;
pdfCell3.Colspan = 2;
String text = "Page " + writer.PageNumber + " of ";
//Add paging to footer
{
cb.BeginText();
cb.SetFontAndSize(bf, 10);
cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
float len = bf.GetWidthPoint(text, 12);
cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
}
//set the alignment of all three cells and set border to 0
pdfCell1.HorizontalAlignment = Element.ALIGN_LEFT;
pdfCell2.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfCell3.HorizontalAlignment = Element.ALIGN_RIGHT;
//pdfCell4.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfCell2.VerticalAlignment = Element.ALIGN_TOP;
pdfCell3.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell1.Border = 0;
pdfCell2.Border = 0;
pdfCell3.Border = 0;
//add all three cells into PdfTable
pdfTab.AddCell(pdfCell1);
pdfTab.AddCell(pdfCell2);
pdfTab.AddCell(pdfCell3);
pdfTab.TotalWidth = document.PageSize.Width - 80f;
pdfTab.WidthPercentage = 70;
//call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable
//first param is start row. -1 indicates there is no end row and all the rows to be included to write
//Third and fourth param is x and y position to start writing
pdfTab.WriteSelectedRows(0, -1, 40, document.PageSize.Height - 30, writer.DirectContent);
//set pdfContent value
//Move the pointer and draw line to separate header section from rest of page
cb.MoveTo(40, document.PageSize.Height - 100);
cb.LineTo(document.PageSize.Width - 40, document.PageSize.Height - 100);
cb.Stroke();
//Move the pointer and draw line to separate footer section from rest of page
cb.MoveTo(40, document.PageSize.GetBottom(50));
cb.LineTo(document.PageSize.Width - 40, document.PageSize.GetBottom(50));
cb.Stroke();
}
堆栈跟踪
at iTextSharp.text.pdf.PdfPRow.set_RTL(Boolean value)
at iTextSharp.text.pdf.PdfPTable.AddCell(PdfPCell cell)
at ITextEvents.OnEndPage(PdfWriter writer, Document document) in c:\TFS\PSA\Source Code\App_Code\ITextEvents.cs:line 145
at iTextSharp.text.pdf.PdfDocument.NewPage()
at PhysComp.UI.CreateSummaryReports.WritePdfLine(Tbl_ProvidersRecord NameRecord, PdfContentByte& Cb, BaseFont Font, Int32 FontSize, Int32 Alignment, String text, Int32 xPos, Int32& yindex, Int32 rotation) in c:\TFS\PSA\Source Code\SummaryReport\CreateSummaryReports.aspx.cs:line 346
at PhysComp.UI.CreateSummaryReports.printInvoice(PdfContentByte& Cb, Int32& yindex, Tbl_ProvidersRecord NameRecord, Invoices_By_CheckRecord invoice, Dictionary`2 ChequeAmounts) in c:\TFS\PSA\Source Code\SummaryReport\CreateSummaryReports.aspx.cs:line 295
at PhysComp.UI.CreateSummaryReports.printInvoices(PdfContentByte& Cb, Int32& yindex, Tbl_ProvidersRecord NameRecord, List`1 Invoices, Dictionary`2 ChequeAmounts) in c:\TFS\PSA\Source Code\SummaryReport\CreateSummaryReports.aspx.cs:line 272
at PhysComp.UI.CreateSummaryReports.CreatePDF(Tbl_ProvidersRecord NameRecord, DateTime StartDate, DateTime EndDate) in c:\TFS\PSA\Source Code\SummaryReport\CreateSummaryReports.aspx.cs:line 133