PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

handling very large files when converting multipage TIFFs
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1687
Page 1 of 1

Author:  ilyaz [ Wed Jun 08, 2011 7:53 pm ]
Post subject:  handling very large files when converting multipage TIFFs

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!

Author:  Thomas Hoevel [ Thu Jun 09, 2011 7:19 am ]
Post subject:  Re: handling very large files when converting multipage TIFFs

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

Author:  JumpyPDFsharp [ Sun Aug 28, 2011 3:38 pm ]
Post subject:  Re: handling very large files when converting multipage TIFFs

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);
}


Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/