We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Saving each page as a document

How do you save each page in a PDFDocument in a separate PDF file to disk? No matter how hard I try, I end up with a "Duplicated" ArgumentException thrown by Syncfusion.Pdf.Implementation.Utilities.SortedListEx`2.Add(TKey key, TValue value).

Here's my code:

PDFDocument document = new PDFDocument();
//Create a page in the document
document.CreatePage();
document.LastPage.Graphics.DrawText(new Point(0, 0), "This is my first page", myfont);

//Create another page
document.CreatePage();
document.LastPage.Graphics.DrawText(new Point(0, 0), "This is my second page", myfont);

//Save each page as a pdf file to disk
int i = 0;
foreach (IPDFPage page in document.Pages)
{
i++;
PDFDocument tempDocument = new PDFDocument();
IPDFPage tempPage = page;
tempDocument.Pages.Add(tempPage);
tempDocument.Save(@"c:\MyFolder\PdfFile" + i.ToString() + ".pdf");
tempDocument.Close();
}

I have tried:
- Saving to a FileStream instead of directly to disk.
- Using for instead of foreach.

No matter what I do, I end up with the "Duplicated" ArgumentException thrown by SortedListEx`2.Add(). What am I doing wrong?

Here's the stack trace from ASP.NET:
Syncfusion.Pdf.Implementation.Utilities.SortedListEx`2.Add(TKey key, TValue value) +216
Syncfusion.Pdf.Implementation.Primitives.PDFDictionary.Add(String name, IPDFSaveable element) +60
Syncfusion.Pdf.Implementation.Objects.Fonts.PDFFont.AddRequiredItems() +64
Syncfusion.Pdf.Implementation.Objects.Fonts.PDFTrueTypeFont.AddRequiredItems() +10
Syncfusion.Pdf.Implementation.Objects.Fonts.PDFFont.Save(PDFWriter writer) +19
Syncfusion.Pdf.Implementation.Objects.PDFArchiveStream.SaveObject(IPDFSaveable obj, PDFReference reference) +220
Syncfusion.Pdf.Implementation.Collections.PDFRootCollection.DoArchiveObject(IPDFSaveable obj, PDFReference reference) +90
Syncfusion.Pdf.Implementation.Collections.PDFRootCollection.SaveIndirectObject(IPDFSaveable obj, PDFWriter writer) +369
Syncfusion.Pdf.Implementation.Collections.PDFRootCollection.Save(PDFWriter writer) +149
Syncfusion.Pdf.PDFDocument.Save(PDFWriter writer) +230
Syncfusion.Pdf.PDFDocument.Save(Stream stream) +83

3 Replies

AJ Ajish Syncfusion Team June 29, 2007 05:17 PM UTC

Hi Gustav,

Thank you for your interest in Essential Pdf.

It is possible to extract pages using Essential Pdf. The following code will take a pdf file and save each page of the pdf file as a seperate document,

using Syncfusion.Pdf;

PDFDocument ldoc = new PDFDocument(txtUrl.Text);

int i = 0;
foreach (IPDFPage page in ldoc.Pages)
{

PDFDocument doc = new PDFDocument();
doc.Pages.Add(page);
i++;
doc.Save("Page" + i.ToString() + ".pdf");

}


Here is a sample for your reference,

Sample: http://websamples.syncfusion.com/samples/PDF.Windows/F63208/main.htm

Sample details:

1) The sample application takes a pdf document as input and saves each page as a seperate document.

Kindly take a look and let me know if you have any questions.

Regards,
Ajish.


GU Gustav July 4, 2007 09:56 AM UTC

I'm sorry, this isn't working for me. The application just freezes when I try to load the PDF and has to be forcibly quit. It might be because of the PDF document I'm trying to load?

Anyhow, I have attached a simple Web application that tries to achieve what I'm discussing. On my development system (Windows XP, IIS, ASP.NET 2) it fails with the "Duplicated" exception I mentioned in my first post.

The first page is created without issues. It's the second page that fails.

You might need to set permissions on the SplitPdfDocuments folder.

FWIW, I'm using assemblies with version 5.102.0.51.

Many thanks for assistance with this.

pdftest3.zip


MW Melba Winshia Syncfusion Team July 4, 2007 12:33 PM UTC

Hi Gustav,

Could you please open a new direct – trac incident with subject line as Forum 63208 - Saving each page as a document? So that it would be possible for us to provide more details regarding this issue.

Thanks,
Melba

Loader.
Live Chat Icon For mobile
Up arrow icon