We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

PdfDocument.Merge leaves source files locked?

Thread ID:

Created:

Updated:

Platform:

Replies:

79124 Jan 22,2009 01:02 AM UTC Feb 18,2009 07:00 AM UTC WinForms 6
loading
Tags: PDF
Alexei Tolkachev NO LONGER WITH COMPANY
Asked On January 22, 2009 01:02 AM UTC

I am using the code very close to the sample provided to merge a number of files into one:

Dim paths As String()
Dim targetPath as string
' populate paths
Dim PDFdoc As PdfDocument = PdfDocument.Merge(paths)
PDFdoc.Save("c:\1.pdf")

The code works as expected in that it creates a file c:\1.pdf that contains all pages from the source files. However, it also appears to leave all source files locked making them impossible to delete programmatically or manually (in Windows Explorer). The files can only be deleted after the process containing the code is killed.

Is there any reason for this and any additional code to release the source files after successful merge?

I do not set the PdfDocument object to Nothing explicitly. Also, there appears to be a Close() method, including one override Close(completely as Boolean) that I would be tempted to execute after Save, but there is no explicit documentation for these methods that I was able to found.

Amy insight will be appreciated, as well as a link to the API reference for the Essential PDF libraries.


Alexei Tolkachev NO LONGER WITH COMPANY
Replied On January 22, 2009 02:05 AM UTC

While waiting for response I tested executing Close (with and without boolean parameter) on PDFDoc as well as setting PDFDoc to Nothing. This does not release the merged source files. I think the reason I have no control over their release is because my Merge is executed (as per sample code) without an instance, as a shared method. And I think the code behind it relies on the garbage collector to release the handles on the files that were open.

I wonder if I should rewrite my code using the overload of Merge that uses PdfLoadedDocument objects rather than file paths. That way I would have more control over the release of the files/handles.

Either that or a quick code fix from the developers would be nice.

Thanks!




Bhuvaneswari P [Syncfusion]
Replied On January 22, 2009 07:07 AM UTC

Hi,

Thank you for your interest in Syncfusion products.

Please use the Close (true) to close the file completely and now you can delete the file.

Difference for Close and Close (true) method:

Doc.Close() -> Closes the document. Releases all common resources.
Doc.Close(true) -->if set to true the document should be disposed completely.

Please refers to the below code snippet to merge and close the document completely:

1. Method 1:
Dim s() As String = { "..\..\Original.pdf", "..\..\Appended.pdf" }
Dim doc1 As PdfDocument = PdfDocument.Merge(s)
doc1.Save("sample.pdf")
doc.Close(True)


2. Method 1:
Dim doc As PdfLoadedDocument = New PdfLoadedDocument("..\..\Original.pdf")
Dim doc1 As PdfLoadedDocument = New PdfLoadedDocument("..\..\Appended.pdf")
doc.ImportPage(doc1, 0)
doc1.Save("sample.pdf")
doc.Close(True)
doc1.Close(True)


Please refers to the below link for Online class reference:
http://help.syncfusion.com/cr_71/-1-12-index.aspx

If you still able to reproduce the issue, please provide your PDF documents to reproduce the issue in our end. This would be helpful for us to reproduce and investigate more on this issue.

Best Regards,
Bhuvana



Alexei Tolkachev NO LONGER WITH COMPANY
Replied On January 22, 2009 10:58 PM UTC

Thank you for getting back to me.

I think the reason you cannot replicate the behavior because is that you are letting the test program terminate before attempting to access the documents that were involved in merge.

In Method 1, add code after doc.Close(True) (should actually be doc1.Close(True))

System.IO.File.Delete("..\..\Appended.pdf")

It will error out with an error "File is open by another application"

Alternatively, as you step through the code in Method 1 in the debugger, after you execute doc1.Close, but before the program ends, attempt to delete or rename the files in Windows Explorer. Again, you should fail.

Method 2 works, but is said to be less efficient than merge.

I got the following code:

Dim docTarget As PdfLoadedDocument = New PdfLoadedDocument(targetFile)
Dim docAppend As PdfLoadedDocument = New PdfLoadedDocument(appendFile)
Dim PDFdoc As PdfLoadedDocument = PdfDocument.Merge(docTarget, docAppend)
Dim tempFile As String = "c:\temp\" + Guid.NewGuid.ToString
PDFdoc.Save(tempFile)
PDFdoc.Close(True)
'docTarget.Close(True) ' this file gets closed in the previous call and if trying to close explicitly, causes an error
docAppend.Close(True)
File.Delete(targetFile)
File.Move(tempFile, targetFile)

This code merges two files at a time, appending appendFile to an existing targetFile.

Since the Merge method with an array of file names is advertised as the most efficient, I would like to be able to use it without the source documents getting locked.

Thanks!


Alexei Tolkachev NO LONGER WITH COMPANY
Replied On January 22, 2009 11:39 PM UTC

Actually, I realized that if I am reducing my multi-file merge to a series of two file merges, my code would be greatly simplified as follows:

Dim docTarget As PdfLoadedDocument = New PdfLoadedDocument(targetFile)
Dim docAppend As PdfLoadedDocument = New PdfLoadedDocument(appendFile)

docTarget.Append(docAppend)
docTarget.Save()
docTarget.Close(True)
docAppend.Close(True)

This does not address my initial concern about the PdfDocument.Merge(String()) leaving files locked. I have solved my business need using an alternative method, but would like to know that the "direct" method would also work.

Thanks!




Bhuvaneswari P [Syncfusion]
Replied On January 23, 2009 09:48 AM UTC

Hi,

The issue "After merging the documents unable to delete the merged files when we use Merge method" reported by you is suspected to be a defect. We have forwarded this to our Development Team for further analysis. We will update you with our Development Team’s response in two business days.

Thank you for your patience.

Best Regards,
Bhuvana



Bhuvaneswari P [Syncfusion]
Replied On February 18, 2009 07:00 AM UTC

Hi,

Thank you for your patience.

The issue "After merging the documents unable to delete the merged files when we use merge method" has been fixed in the weekly development build version 7.2.0.4. Please download the weekly development build from the below link:

http://www.syncfusion.com/downloads/product/build.aspx


Please try this and let us know if this helps you to resolve the issue.

Best Regards,
Bhuvana


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;