PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

C# - NullReferenceException in PdfSharp.dll
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3903
Page 1 of 1

Author:  lerxx [ Sat Jan 19, 2019 5:48 pm ]
Post subject:  C# - NullReferenceException in PdfSharp.dll

Hello community,

I have a problem with the PdfSharp.dll and exceptions. I want to merge a few pdfs to one pdf but everytime when I'm using code like this:

Code:
foreach (string file in files)
  {
    // Open the document to import pages from it.
    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
 
    // Iterate pages
    int count = inputDocument.PageCount;
    for (int idx = 0; idx < count; idx++)
    {
      // Get the page from the external document...
      PdfPage page = inputDocument.Pages[idx];
      // ...and add it to the output document.
      outputDocument.AddPage(page);
    }
  }


I am getting an NullReferenceException thrown by calling the function "outputDocument.AddPage(page)" -> "public PdfPage addPage(PdfPage page)" -> "internal PdfCatalog Catalog".

Code:
/// <summary>
    /// Gets the PdfCatalog of the current document.
    /// </summary>
    internal PdfCatalog Catalog
    {
        get { return _catalog ?? (_catalog = _trailer.Root); }
    }


Ausnahme ausgelöst: 'System.NullReferenceException' in PdfSharp.dll ("Object reference not set to an instance of an object.") System.NullReferenceException

Can anyone tell me what I could do? Thanks in advance!

Author:  TH-Soft [ Sat Jan 19, 2019 5:55 pm ]
Post subject:  Re: C# - NullReferenceException in PdfSharp.dll

Hi!

Maybe there's a problem with your PDF, not with your code.
See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

Author:  lerxx [ Sat Jan 19, 2019 7:57 pm ]
Post subject:  Re: C# - NullReferenceException in PdfSharp.dll

Thanks for your fast reply!

Now it seems to work but I have the problem, that the documents are empty (I can't open the "merged" pdf). Do you have an idea why? Thanks in advance.

Code:
 PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();
                int numberOfDocs = 0;
                //IniFile beinhaltet Namen der Dokumente die beruecksichtigt werden sollen
                //string[] specifiedDocuments = documentsSpecifiedInIniFile();
           
                foreach (string file in files)
                {

                    //"-Alle_Dokumente.pdf"
                   
                        if (!file.Contains("-Alle_Dokumente.pdf"))
                        {
                           
                                    numberOfDocs++;
                                    //    // Open the document to import pages from it.
                                    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

                                    //    // Iterate pages
                                    int count = inputDocument.PageCount;
                                    for (int idx = 0; idx < count; idx++)
                                    {
                                        try
                                        {
                                            PdfPage page = inputDocument.Pages[idx];
                                            outputDocument.AddPage(page);

                                        }
                                        catch (NullReferenceException ex)
                                        {
                                            string[] text1 = { "text" };
                                            System.IO.File.WriteAllLines(@"C:\errorAddPage.txt", text1);

                                        }
                                    }
                            }
                  }
         
               
                // Save new file if it has pages and its more than one document
                int pagesoutput = 0;
                    try
                    {
                       
                        pagesoutput = outputDocument.PageCount;
                    }
                    catch (NullReferenceException ex)
                    {
                        //Error
                    }

                    if ( numberOfDocs > 1)
                    {
                        try
                        {
                            outputDocument.Save(fileNameComplete);
                        }
                        catch
                        {
                            using (EventLog eventLog = new EventLog("PDF_Converter"))
                            {
                                eventLog.Source = "PDF_Converter";
                                eventLog.WriteEntry("Error creating file!\n " + fileNameComplete, EventLogEntryType.Information, 101, 1);
                            }
                        }
                    }
                }


I have tested the DLL-file and some new simple code and found out, that it has nothing to do with the pdf files. This simple project:

using PdfSharp.Drawing;

namespace PdfSharp
{
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();

// Create an empty page
PdfSharp.Pdf.PdfPage page = document.AddPage();

// Save the document...
string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
//Process.Start(filename);

}
}
}

throws exactly the same exception by calling the function "AddPage". Do you have any idea what I am doing wrong? :?

Author:  lerxx [ Sat Jan 19, 2019 11:40 pm ]
Post subject:  Re: C# - NullReferenceException in PdfSharp.dll

I have reviewed my code and have still problems with the "pdfdocument" outputdocument and NullReferenceExceptions.

Image

Author:  TH-Soft [ Sun Jan 20, 2019 10:30 am ]
Post subject:  Re: C# - NullReferenceException in PdfSharp.dll

Cannot reproduce.
Quote:
Showing source code snippets in the forum or attaching PDF files without code will often be insufficient to replicate the problem.

viewtopic.php?f=2&t=832

Author:  lerxx [ Sun Jan 20, 2019 10:45 am ]
Post subject:  Re: C# - NullReferenceException in PdfSharp.dll

TH-Soft wrote:
Cannot reproduce.
Quote:
Showing source code snippets in the forum or attaching PDF files without code will often be insufficient to replicate the problem.

viewtopic.php?f=2&t=832


Hi, I have solved the problem by creating a new Pdfsharp.dll. Now it works, thanks for your help!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/