locked
ITextSharp PdfWriter nullReferenceexception RRS feed

  • Question

  • User-1651858287 posted
    FileStream fs = new FileStream(Path + FileName + ".pdf"FileMode.Create, FileAccess.Write, FileShare.None);
     
    Rectangle rec2 = new Rectangle(PageSize.A4);
     
    
    Document doc = new Document(rec2, 10, 10, 10, 10);
     
    PdfWriter writer = PdfWriter.GetInstance(docfs); 
    //System.NullReferenceException: object reference not set to an instance of an object.

    Help please
    Friday, June 19, 2020 6:45 PM

All replies

  • User-2054057000 posted

    FileStream fs = new FileStream(Path + FileName + ".pdf"FileMode.Create, FileAccess.Write, FileShare.None);
     
    Rectangle rec2 = new Rectangle(PageSize.A4);
     
    
    Document doc = new Document(rec2, 10, 10, 10, 10);
     
    PdfWriter writer = PdfWriter.GetInstance(docfs); 
    //System.NullReferenceException: object reference not set to an instance of an object.
    Help please

    Looks like their is some problem in doc object. There is a tutorial on iTextSharp that may be of your help.

    Saturday, June 20, 2020 2:55 AM
  • User1535942433 posted

    Hi sweetSteal,

    Accroding to your description and codes,I suggest you could debug and check if the doc and fs value is null.

    If you don't catch any exceptions,this NullReferenceException can very well be one that is already caught and handled inside iTextSharp, so you don't care about it.

    You could change the following setting to verify:

    Debug -> Exceptions -> Remove the checkbox in the column "Thrown" of "Common Language Runtime Exceptions".

    Best regards,

    Yijing Sun

    Monday, June 22, 2020 7:45 AM
  • User-1651858287 posted

    I am not sure what happened the program was working without problems both on the localhost and on the server.

    now its breaking there with a NullReferenceException 
    PdfWriter
     writer = PdfWriter.GetInstance(docfs);
    when I click the step over over on the degug
    its goes there with another NullReferenceException 
    doc
    .Close();
    then create the documnet as normal.
    However on the server its just crashes.

    Monday, June 22, 2020 3:17 PM
  • User1535942433 posted

    Hi sweetSteal,

    Accroding to your description,I couldn't reproduce your problems.I create  a test and it works fine.

    Could you post full codes to us?It will help us to solve your problems.

    My codes like this:

     string Path = @"C:\Users\yijings\Desktop\";
                string FileName = "1";
                FileStream fs = new FileStream(Path + FileName + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None);
                Rectangle rec2 = new Rectangle(PageSize.A4);
    
    
                Document doc = new Document(rec2, 10, 10, 10, 10);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.AddAuthor("Micke Blomquist");
                doc.AddCreator("Sample application using iTextSharp");
                doc.AddKeywords("PDF tutorial education");
                doc.AddSubject("Document subject - Describing the steps creating a PDF document");
                doc.AddTitle("The document title - PDF creation using iTextSharp");
                doc.Open();
                doc.Add(new Paragraph("Hello World!"));
                doc.Close();
                writer.Close();
                fs.Close();

    More details,you could refer to  below article:

    https://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/

    Best regards,

    Yijing Sun

    Tuesday, June 23, 2020 2:45 AM
  • User-1324246109 posted

    Turn on Just My Code in your debugging settings. That is what fixed it for me.

    Monday, August 3, 2020 4:40 PM
  • User-1017271162 posted

    I faced various time with the same problem. I solved by a function that try several instantiation of a PdfWrite.

    public iTextSharp.text.pdf.PdfWriter   PdfWriter_GetInstance(iTextSharp.text.Document document , System.IO.FileStream filestrm )
    {
      iTextSharp.text.pdf.PdfWriter pdfwr = null;
               
      for (int repeat = 0; repeat < 6; repeat ++)
      {
        try
        {
          pdfwr = iTextSharp.text.pdf.PdfWriter.GetInstance(document, filestrm); /
          break; //created, then exit loop
        }
        catch // instantiation of PdfWriter failed, then pause
        {
          System.Threading.Thread.Sleep(300); 
        }
      }
      if ( pdfwr== null) 
      {
        throw new Exception("iTextSharp PdfWriter was not instantiated");
      }
                
      return pdfwr ;
    }
    



    Monday, November 9, 2020 1:24 PM