Unable to append page in one PDf document to the end of another PDF document

I am running the following code to merge two documents. What I want to do is append documentToMerge to the end of fileName.  ​Both are PDF documents.  When I run this code, I get the following error...

The process cannot access the file [fileName] because it is being used by another process.

which happens on this line...

using FileStream outputStream = new FileStream(fileName, FileMode.Open, FileAccess.Write);


            using PdfDocument finalDocument = new PdfDocument();

            // Put both document into a stream
            using FileStream firstStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            using FileStream secondStream = new FileStream(documentToMerge, FileMode.Open, FileAccess.Read);

            //Create a PDF stream for merging
            Stream[] streams = { firstStream, secondStream };

            //Merge PDF documents
            PdfDocumentBase.Merge(finalDocument, streams);

            //Save the document into a stream
            using FileStream outputStream = new FileStream(fileName, FileMode.Open, FileAccess.Write);
            finalDocument.Save(outputStream);


I am running Syncfusion version 19.4.0.48.


I understand that the outputStream is the same file as firstStream but shouldn't firstStream already be closed by the time I open it a second time?  I tried putting brackets in the appropriate places (before the last using FileStream) to ensure the FileStreams were closed but I get the same error.

Any idea what I am doing wrong and what I can do to resolve this issue?


3 Replies

JT Jeyalakshmi Thangamarippandian Syncfusion Team February 27, 2024 12:46 PM UTC

Hi,

We were able to reproduce the reported issue with the provided details on our end. To address this, we recommend setting EnableMemoryOptimization to true and disposing of the first stream. We have adjusted the code accordingly and included a snippet for your reference.

using PdfDocument finalDocument = new PdfDocument();

 

// Put both document into a stream

using FileStream firstStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

using FileStream secondStream = new FileStream(documentToMerge, FileMode.Open, FileAccess.Read);

 

//Create a PDF stream for merging

Stream[] streams = { firstStream, secondStream };

finalDocument.EnableMemoryOptimization = true;

//Merge PDF documents

PdfDocumentBase.Merge(finalDocument, streams);

  

firstStream.Dispose();

//Save the document into a stream

using FileStream outputStream = new FileStream(fileName, FileMode.Open, FileAccess.Write);

finalDocument.Save(outputStream);

Please test it on your end and let us know if you need further assistance.

 Regards,

Jeyalakshmi T



GE Gene replied to Jeyalakshmi Thangamarippandian February 27, 2024 03:21 PM UTC

This fixed the problem.  Thanks!



JT Jeyalakshmi Thangamarippandian Syncfusion Team February 28, 2024 05:01 AM UTC

We are glad the issue has been resolved at your end. Please get back to us if you need any other assistance.


Loader.
Up arrow icon