PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Jul 20, 2024 11:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jun 08, 2011 7:53 pm 
Offline

Joined: Sat Mar 26, 2011 2:24 am
Posts: 6
I need to convert a bunch of multipage TIFFs into PDF. I found some code on CodeProject that uses PDFsharp to do it. It works great for small files but I have to convert some huge files containing hundreds of color pages. The code I have throws out of memory exception probably because I am trying to load all source pages into memory before generating output PDF. The code looks like this:

Code:
                PdfDocument doc = new PdfDocument();
                Image myimage = Image.FromFile(source);

                System.Drawing.Imaging.FrameDimension oFDimension = new System.Drawing.Imaging.FrameDimension(myimage.FrameDimensionsList[0]);
                int iCount = myimage.GetFrameCount(oFDimension) - 1;

                for (int index = 0; index <= (iCount); index++)
                {
                    PdfPage page = doc.AddPage();
                    page.Size = PageSize.Letter;
                    page.Orientation = PageOrientation.Portrait;
                    XGraphics xgr = XGraphics.FromPdfPage(page);
                    myimage.SelectActiveFrame(oFDimension, index);
                    XImage img = XImage.FromGdiPlusImage(myimage);
                    xgr.DrawImage(img, 0, 0);

                }
                doc.Save(destinaton);
                doc.Close();


How can I modify it to ingest one page at a time, add its contents to the output PDF and then cleaning as much memory as possible before ingesting the next page?

thanks much!


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 09, 2011 7:19 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
PDFsharp was designed to keep everything in memory.

So use a 64 bit operating system, make sure your application runs in 64 bit mode and use a computer with a lot of RAM.
Try both PDFsharp builds (WPF and GDI+) as this could make a difference with respect to system resources.

See also here:
viewtopic.php?p=4666#p4666

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Sun Aug 28, 2011 3:38 pm 
Offline

Joined: Sun Aug 28, 2011 2:23 pm
Posts: 2
ilyaz wrote:
The code I have throws out of memory exception probably because I am trying to load all source pages into memory before generating output PDF.

Not because you load all the images but because pdfsharp hoggs the memory.

ilyaz wrote:
How can I modify it to ingest one page at a time, add its contents to the output PDF and then cleaning as much memory as possible before ingesting the next page?

Just save the pdf after adding an image and reopen it on the next iteration like this:
Code:
Image myimage = Image.FromFile(source);

System.Drawing.Imaging.FrameDimension oFDimension = new System.Drawing.Imaging.FrameDimension(myimage.FrameDimensionsList[0]);
int iCount = myimage.GetFrameCount(oFDimension) - 1;

for (int index = 0; index <= (iCount); index++)
{
   PdfDocument doc = default(PdfDocument);
   if index == 0) {
      doc = new PdfDocument();
   } else {
      doc = PdfSharp.Pdf.IO.PdfReader.Open(destinaton, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);
   }

   PdfPage page = doc.AddPage();
   page.Size = PageSize.Letter;
   page.Orientation = PageOrientation.Portrait;
   XGraphics xgr = XGraphics.FromPdfPage(page);
   myimage.SelectActiveFrame(oFDimension, index);
   XImage img = XImage.FromGdiPlusImage(myimage);
   xgr.DrawImage(img, 0, 0);
   doc.Save(destinaton);
}



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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 47 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