PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Fri May 16, 2025 4:19 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue May 06, 2025 8:12 pm 
Offline

Joined: Fri May 02, 2025 6:46 pm
Posts: 2
The following straight forward code opens two PDF files and merges the contents into a new file. This code only works under 6.1.1 if I comment out the two Close method calls (pdfDoc.Close() and pdfCoversheet.Close() otherwise an exception is thrown with error The document cannot be modified. at PdfSharp.Pdf.PdfDocument.Close(). The code worked uncommented under 1.50.5147.
Code:
Dim combinedPdfPath As String = Path.Combine(g_WorkFolder, Path.GetFileNameWithoutExtension(pdfPath) + "_cs.pdf")
If File.Exists(combinedPdfPath) Then
   File.Delete(combinedPdfPath)
End If
Using pdfCoversheet As PdfSharp.Pdf.PdfDocument = PdfReader.Open(coverSheetPath, PdfDocumentOpenMode.Import)
   Using pdfDoc As PdfSharp.Pdf.PdfDocument = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Import)
      Using pdfCombined As New PdfDocument()
         pdfCombined.AddPage(pdfCoversheet.Pages(0))
         For i As Integer = 0 To pdfDoc.PageCount - 1
            pdfCombined.AddPage(pdfDoc.Pages(i))
         Next
         pdfCombined.Save(combinedPdfPath)
         'pdfDoc.Close()
      End Using
   End Using
   'pdfCoversheet.Close()
End Using
Return combinedPdfPath


Top
 Profile  
Reply with quote  
PostPosted: Thu May 08, 2025 3:13 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1044
Location: CCAA
Hi!

This should work with version 6.2.0 Preview 3.

And BTW: The point of using "using" is you no longer have to call "Close" explicitly.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu May 08, 2025 5:40 pm 
Offline

Joined: Fri May 02, 2025 6:46 pm
Posts: 2
TH-Soft wrote:
Hi!
And BTW: The point of using "using" is you no longer have to call "Close" explicitly.


Yes, but I thought you might like to know that previously working code broke between versions. Maintaining code is something all developers have to do and they are not always the person who wrote the original code.


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: No registered users and 66 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