PdfSharp "NullReferenceException"

lerxx

New member
Joined
Jan 19, 2019
Messages
4
Programming Experience
1-3
Hello,

I have the problem that my code (using PdfSharp) is thworing an "NullReferenceException" at the point
outputDocument.AddPage(page);
and I dont know why. You can find a snippet of my code with the important things below:
C#:
public static void CreatePDFx(string path)
        {
                 // Files beinhaltet alle Dateien pro Patientenordner (Bezeichnungen) 

                string[] files = GetFiles(path);

                // Open the output document
                PdfDocument outputDocument = new PdfDocument();
                int numberOfDocs = 0;
                // Iterate files
                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);
                    }
                }
}

 public static void StartWatchers()
        {
            List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
            string[] ward_folders = Directory.GetDirectories(@"C:\OfflineDocuments\");
            watchers.Add(MyWatcherFatory(@"C:\OfflineDocuments\"));
            //Fehlerhaft
            int j = 0;
            foreach (string ward_folder in ward_folders)
            {
                
                j++;
          
                string[] pat_folders = Directory.GetDirectories(ward_folder);
                int i = 0;
                
                foreach (string pat_folder in pat_folders)
                {
                    
                    CreatePDFx(pat_folder);
                    i++;
                }
                foreach (FileSystemWatcher watcher in watchers)
                {
                    watcher.EnableRaisingEvents = true; ;
                    i++;
                }
        
            }
        }

Thanks in advance!
 
Last edited:
Place a breakpoint on that line and confirm that 'outputDocument' is not null. You probably ought to do the same for 'page'. If neither are null, take a look at the stack trace of the exception to see exactly where it is actually thrown.
 
The stack trace shows the following:

PdfSharp.dll!PdfSharp.Pdf.PdfDocument.Catalog.get() Zeile 793 C#
PdfSharp.dll!PdfSharp.Pdf.PdfDocument.AddPage(PdfSharp.Pdf.PdfPage page) Zeile 829 C#
combine_rescue_pdf.exe!combine_rescue_pdf.CombinePDF.CreatePDFx(string path) Zeile 162 C#
combine_rescue_pdf.exe!combine_rescue_pdf.CombinePDF.StartWatchers() Zeile 244 C#
combine_rescue_pdf.exe!combine_rescue_pdf.CombinePDF.OnStart(string[] args) Zeile 307 C#

The exception will be thrown in the function catalog (from PdfSharp):


The following function of PdfSharp calls the catalog:
/// <summary>
/// Adds the specified page to this document. If the page is from an external document,
/// it is imported to this document. In this case the returned page is not the same
/// object as the specified one.
/// </summary>
public PdfPage AddPage(PdfPage page)
{
if (!CanModify)
throw new InvalidOperationException(PSSR.CannotModify);
return Catalog.Pages.Add(page);
}

/// <summary>
/// Gets the PdfCatalog of the current document.
/// </summary>
internal PdfCatalog Catalog
{
get { return _catalog ?? (_catalog = _trailer.Root); }
}
PdfCatalog _catalog; // never changes if once created

Do you have any idea, what I could do?
 
The first thing I would always do in cases like this is isolate the functionality that is causing problems in a new test project and see whether it behaves the same way.
 
That is a good idea! I have testet it and sadly it comes to the same exception.. with this simple programm:
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);
        }
    }
}

Do you have any idea, what I am making wrong? :/ Thanks in advance.
 
I've never used that PdfSharp component and rarely worked with PDFs at all so I'm not sure what the issue might be, but I'd be researching that Catalog property to see what it does and how it is supposed to be initialised. Based on the code, it appears that the '_trailer' field must be null so you'd need to find out why that is.
 
I've never used that PdfSharp component and rarely worked with PDFs at all so I'm not sure what the issue might be, but I'd be researching that Catalog property to see what it does and how it is supposed to be initialised. Based on the code, it appears that the '_trailer' field must be null so you'd need to find out why that is.

Hi jmcilhinney,

thanks for your reply! I have solved the problem by creating a new Pdfsharp.dll. Your idea with "trailer" brought me to the thread: https://stackoverflow.com/questions...when-adding-password-to-pdf/38593235#38593235 but that was already included. But by creating a new DLL now it works.

Have a nice weekend!
 
When I used PdfSharp last time I just added the package with Nuget Package Manager (under Tools menu i VS), no need to get the source code and build the library yourself with that.
 
Back
Top Bottom