Hey there,
I just started using this library. So far, it's been very simple to use, and the samples make learning quite easy.
One issue that I've noticed is that it doesn't seem to want to release the memory it uses. I understand that it holds the entire document in memory while creating, but I can't seem to find any way to release the memory, even after I'm done with the PdfDocument object. Calling Dispose(), either directly or via using statements, doesn't seem to work. Here's my code. It starts with a list of PictureViewModels (a custom class that holds information about images). For each image, it creates page and adds the image. GetNewPage(PdfDocument doc) is a method that gets a new page and sets the height and width of the page based upon some private properties.
Code:
IEnumerable<PictureViewModel> mediaList = GetMedia();
using (PdfDocument doc = new PdfDocument())
{
foreach (PictureViewModel pic in mediaList)
{
PdfPage page = GetNewPage(doc);;
double scaleX = page.Width / pic.ImageWidth;
double scaleY = page.Height / pic.ImageHeight;
double scale = scaleX < scaleY ? scaleX : scaleY;
using (XImage img = XImage.FromFile(med.LocalFilePath))
{
using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
gfx.DrawImage(img, 0, 0, pic.ImageWidth * scale, pic.ImageHeight * scale);
}
}
}
doc.Save(@"C:\TestReport.pdf");
Process.Start(@"C:\TestReport.pdf");
}
GC.Collect();
Is there anything that I'm missing, or does this library never release used memory? Thanks for any help you might be able to offer.
Edit:
I just realized that I never mentioned how I knew it wasn't releasing memory. In the Task Manager, my program normally operates at around 80,000K Memory (Private Working Set). When I have my program use PDFSharp to create a report, the memory usage goes up and never comes back down. Each new PDF generated makes the memory usage go up. The larger the pdf file made, the more it goes up. If I ask it to generate a report with 500 images (resulting in a 250MB PDF), the memory usage jumps by 800,000K. The memory usage never goes back down.
Thanks again,
-Scott