PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 18, 2019 8:29 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sat Jan 19, 2019 5:48 pm 
Offline

Joined: Sat Jan 19, 2019 5:41 pm
Posts: 4
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!


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 19, 2019 5:55 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 477
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 19, 2019 7:57 pm 
Offline

Joined: Sat Jan 19, 2019 5:41 pm
Posts: 4
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? :?


Last edited by lerxx on Sun Jan 20, 2019 8:52 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sat Jan 19, 2019 11:40 pm 
Offline

Joined: Sat Jan 19, 2019 5:41 pm
Posts: 4
I have reviewed my code and have still problems with the "pdfdocument" outputdocument and NullReferenceExceptions.

Image


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 20, 2019 10:30 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 477
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 20, 2019 10:45 am 
Offline

Joined: Sat Jan 19, 2019 5:41 pm
Posts: 4
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!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group