1

I have a method in my program, that uses PDFShap to export the users project to a pdf. At first I prepared all the data and then exported it to a pdf. This lead to an OutOfMemory exception when there was a lot of data. Then I changed the method, so that every time one page is filled, it is imediatly exported. But now I get a lot of PDFSharp.Font objects that are kept in memory. I tried to do the export in a new Thread, which I was not able to do. I tried calling the Dispose method of all PDFShap objects, setting them to null and to create them all using a statement. Nothing worked. How can I dispose these PDFShap.Font objects completely?

private static void AddPageToPdf(string goalfile, PrinterPage page)
    {

        using (PdfDocument doc = File.Exists(goalfile) ? PdfReader.Open(goalfile, PdfDocumentOpenMode.Modify) : new PdfDocument(goalfile))
        {
            string temp = goalfile.Substring(0, goalfile.LastIndexOf("\\")) + "\\temp.pdf";
            int pagenumber = doc.PageCount + 1;
            FixedDocument fixedDoc = new FixedDocument();

            fixedDoc.DocumentPaginator.PageSize = page.GetSize();

            // Refresh the page content
            page.Page = pagenumber;
            page.MaxPages = pagenumber;
            page.Width = fixedDoc.DocumentPaginator.PageSize.Width;
            page.Height = fixedDoc.DocumentPaginator.PageSize.Height;

            // Create a new document page
            PageContent pageContent = new PageContent();
            FixedPage fixedPage = new FixedPage();

            // Initialize the new document page
            fixedPage.Background = Brushes.White;
            fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width;
            fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height;

            // Insert the current page to the document
            fixedPage.Children.Add(page);
            ((IAddChild)pageContent).AddChild(fixedPage);
            fixedDoc.Pages.Add(pageContent);


            // Save the content to a xps file as base structure for the pdf
            MemoryStream lMemoryStream = new MemoryStream();
            Package package = Package.Open(lMemoryStream, FileMode.Create);
            XpsDocument xpsdoc = new XpsDocument(package);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsdoc);
            writer.Write(fixedDoc);
            xpsdoc.Close();
            package.Close();

            // Convert the xps document into a pdf document and save it
            using (var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream))
            {
                PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, temp, 0);
                lMemoryStream.Close();
                pdfXpsDoc.Close();
                using (PdfDocument singlePage = PdfReader.Open(temp, PdfDocumentOpenMode.Import))
                {
                    PdfPage pdfPage = singlePage.Pages[0];
                    doc.AddPage(pdfPage);
                    doc.Close();
                    doc.Save(goalfile);
                    //try to clean up memory
                    File.Delete(temp);
                    singlePage.Dispose();
                    doc.Dispose();
                    
                    lMemoryStream = null;
                    package = null;
                    writer = null;
                    pdfPage = null;
                }
            }
        }
        GC.Collect();
    }

Since VS doesn't provide a wa to export a snapshot from the diagnostic tool (at least none that I could find) I made a screenshot of the snapshot table: PDFSharp objects after first export

This is a screenshot after i exported the same project a second time PDFSharp objects after second export

4
  • The screen shot shows 64 MiB of memory usage. That shouldn't be an issue nowadays. PDFsharp keeps fonts in memory, but each font should be loaded only once and the memory footprint does not grow after creating the first document, assuming all documents use the same fonts. Commented Apr 15, 2020 at 13:32
  • Are you using PDFsharp 1.50 or newer? Commented Apr 15, 2020 at 13:33
  • as far as i understand it, the inclusive size is important. After the first screenshot, there are somewhere aound 285 MB (which is is roughly the same amount i can observer in the Task Manager). The second screenshot i just added shows a snapshot after i exported the same poject a second time and the size of the PDFSharp objects doubled pretty much exactly. Commented Apr 16, 2020 at 8:43
  • I'm using PDFSharp-WPF 1.32.2608.0 and PDFSharp.XPS 1.0.0.0 Commented Apr 16, 2020 at 8:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.