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

Close() = evil
https://forum.pdfsharp.net/viewtopic.php?f=3&t=1886
Page 1 of 1

Author:  rciemins [ Mon Jan 16, 2012 2:23 pm ]
Post subject:  Close() = evil

Hello,
Migrated from 1.21 to PDFSharp. Version - 1.31.1789.0 and got some surprises :).
One of them...

PdfDocument doc = new PdfDocument(groupedPdfName);
..
foreach (Guid customerId in customerInvoices.Keys)
{
//Opens created PDF file with all invoices for single customer
PdfDocument documentByCustomer = PdfReader.Open(filepath, PdfDocumentOpenMode.Import);

// Ads pages to document which contains invoice data from all customers
foreach (PdfPage page in documentByCustomer.Pages)
{
doc.AddPage(page);
}
documentByCustomer.Close(); //This throws an exception -> Can't save modified file.
documentByCustomer.Dispose(); // This doesn't throw anything.
...
}
foreach (PdfPage page in groupedDocument.Pages)
{
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MFEH = PdfFontEmbedding.Always;
}
doc.Save(pdfPath);
doc.Close(); //Close throws "An item with the same key has already been added". Exception was thrown in Release mode.

Currently the only solution is not to use Close(). Fortunately it works without it. :)

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