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?