Note that there are some explanatory texts on larger screens.

plurals
  1. PODocument.NewPage giving object referance not set to an instance of an object
    primarykey
    data
    text
    <p>I'm new to creating pdfs, and successfully had one working but no footer, so I've been looking around and based myself on <a href="http://www.codeproject.com/Tips/573907/Generating-PDF-using-ItextSharp-with-Footer-in-Csh" rel="nofollow">this</a></p> <p>This is the code I have of it:</p> <pre><code>string nome = "MapaAnual" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "" + DateTime.Now.Day.ToString() + ".pdf"; string outputFile = "C:\\Users\\sies4578\\Documents\\Visual Studio 2012\\Projects\\ULSM_Equipamentos\\REL\\" + nome.ToString(); try { //Create a standard .Net FileStream for the file, setting various flags //FileStream fs = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); System.IO.FileStream fs = new System.IO.FileStream(outputFile + DateTime.Now.ToString("ddMMyyHHmmss") + ".pdf", System.IO.FileMode.OpenOrCreate); //Create a new PDF document setting the size to A4 Document doc = new Document(PageSize.A4.Rotate()); //Bind the PDF document to the FileStream using an iTextSharp PdfWriter PdfWriter w = PdfWriter.GetInstance(doc, fs); w.PageEvent = new MyPageEventHandler(); //Open the document for writing doc.Open(); for (int i = 0; i &lt; listaServicos.Count; i++) { doc.NewPage(); //where it goes to the catch giving that error #region Calibracoes //Code that doesn't matter #endregion } doc.Close(); Process.Start(outputFile); return "PDF criado com sucesso"; } catch (Exception ex) { Console.WriteLine("An error ocurred, the PDF-document could not be created."); return ex.Message; } </code></pre> <p>I doubt it's related but as asked here is the code of MyPageEventHandler:</p> <pre><code> public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper { // This is the contentbyte object of the writer PdfContentByte cb; // we will put the final number of pages in a template PdfTemplate template; // this is the BaseFont we are going to use for the header / footer BaseFont bf = null; // This keeps track of the creation time DateTime PrintTime = DateTime.Now; protected iTextSharp.text.Font footer { get { // create a basecolor to use for the footer iTextSharp.text.Font, if needed. BaseColor grey = new BaseColor(128, 128, 128); iTextSharp.text.Font font = iTextSharp.text.FontFactory.GetFont("Arial", 9, iTextSharp.text.Font.NORMAL, grey); return font; } } public override void OnStartPage(PdfWriter writer, Document doc) { //Inicio Cabeçalho Logo //Inicio Cabeçalho Logo PdfPTable cabecalho = new PdfPTable(3); cabecalho.DefaultCell.Border = 0; cabecalho.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; cabecalho.TotalWidth = 700; float[] width1 = { 150.0F, 550.0F, 550.0F }; cabecalho.SetWidths(width1); cabecalho.LockedWidth = true; iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance("C:\\ULSMatosinhos\\Software\\ULS-Matosinhos\\Images\\logopro.png"); cabecalho.AddCell(im); iTextSharp.text.Font cabeca = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("arial", 14, iTextSharp.text.Font.BOLDITALIC, new BaseColor(23, 181, 150))); PdfPCell linha = new PdfPCell(new Phrase("PLANO DE CALIBRAÇÕES - " + anoCalibracao.ToString(), cabeca)); linha.Border = 0; linha.HorizontalAlignment = 1; linha.VerticalAlignment = Element.ALIGN_MIDDLE; cabecalho.AddCell(linha); iTextSharp.text.Font cd = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)); PdfPCell codigoDoc = new PdfPCell(new Phrase(" 591_00_SIE_1191", cd)); codigoDoc.Border = 0; codigoDoc.HorizontalAlignment = 1; codigoDoc.VerticalAlignment = Element.ALIGN_MIDDLE; cabecalho.AddCell(codigoDoc); doc.Add(cabecalho); } public override void OnEndPage(PdfWriter writer, Document document) { base.OnEndPage(writer, document); int pageN = writer.PageNumber; String text = "Page " + pageN + " of "; float len = bf.GetWidthPoint(text, 8); iTextSharp.text.Rectangle pageSize = document.PageSize; cb.SetRGBColorFill(100, 100, 100); cb.BeginText(); cb.SetFontAndSize(bf, 8); cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30)); cb.ShowText(text); cb.EndText(); cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30)); cb.BeginText(); cb.SetFontAndSize(bf, 8); cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Printed On " + PrintTime.ToString(), pageSize.GetRight(40), pageSize.GetBottom(30), 0); cb.EndText(); } public override void OnCloseDocument(PdfWriter writer, Document document) { base.OnCloseDocument(writer, document); template.BeginText(); template.SetFontAndSize(bf, 8); template.SetTextMatrix(0, 0); template.ShowText("" + (writer.PageNumber - 1)); template.EndText(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload