PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jan 27, 2023 3:18 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Mar 03, 2022 6:41 pm 
Offline

Joined: Thu Mar 03, 2022 5:46 pm
Posts: 2
I am trying to combine two PDF files into one and then open the file in my pdf reader. However, after creating the file I'm not able to open it. I get an error when trying to open the file that the file is already open or in use. This happens whether I prompt it to open programmatically or go to the folder and open it that way. It will not open until my program is closed, and then it opens just fine. I tried adding lines to .Close(), .Dispose(), or set the variable to Nothing, but it doesn't change anything. The file is still in use as long as the program is open. How can I get it to release the file so I can open it? See my code below, thanks.

Code:
        Dim PDF1 As String = "C:\Test\pdf1.pdf"
        Dim PDF2 As String = "C:\Test\pdf2.pdf"
        Dim combinedPDF As String = "C:\Test\combinedpdf.pdf"
        Dim outputDocument As PdfDocument = Nw PdfDocument()

        If File.Exists(PDF1) Then
            Dim inputDocument As PdfDocument = PdfReader.Open(PDF1, PdfDocumentOpenMode.Import)

            Dim PDFPageCount As Integer = inputDocument.PageCount
            Dim count As Integer = 0
            While count < PDFPageCount
                Dim page As PdfPage = inputDocument.Pages(count)
                outputDocument.AddPage(page)
                count += 1
            End While
        End If

        If File.Exists(PDF2) Then
            Dim inputDocument As PdfDocument = PdfReader.Open(PDF2, PdfDocumentOpenMode.Import)

            Dim PDFPageCount As Integer = inputDocument.PageCount
            Dim count As Integer = 0
            While count < PDFPageCount
                Dim page As PdfPage = inputDocument.Pages(count)
                outputDocument.AddPage(page)
                count += 1
            End While
        End If

        outputDocument.Save(combinedPDF)

        outputDocument.Close()
        outputDocument.Dispose()
        outputDocument = Nothing

        Process.Start(combinedPDF)


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 03, 2022 6:51 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 757
Location: CCAA
We don't have this issue with our C# or VB samples.
See also:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

You can use PdfDocument.Save() to write the PDF into a MemoryStream and then handle all the file IO in your own code.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 03, 2022 10:00 pm 
Offline

Joined: Thu Mar 03, 2022 5:46 pm
Posts: 2
I'm not sure why my code wasn't working, but thank you for pointing out the stream option, that route seems to have done the trick without locking down the file.


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: Google [Bot] and 26 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