Hello. I am currently evaluating your product suite. I know this seems simple, but I need to be able to (1) create a new blank PDF file and then (2) merge it with several other PDFs that already exist, keeping the original file name created in step #1.
All of the examples given show saving the results to a new/different filename. For example, in this example, how can I save the results to C:\DocumentA.pdf instead of C:\Mydocument.pdf?
[C#]
MergeDocument document = new MergeDocument( @"C:\DocumentA.pdf" );
document.Append( @"C:\DocumentB.pdf", 1, 2 );
document.Append( @"C:\DocumentC.pdf" );
document.Draw( @"C:\MyDocument.pdf" );
If I try to save it to C:\DocumentA.pdf, I get the error: The process cannot access the file 'C:\DocumentA.pdf' because it is being used by another process.
Also, in that code sample, C:\DocumentA.pdf must already exist. I need to create a new, empty PDF with a customized name and then append the other PDFs to it and then save it with that customized name.
Thanks in advance for some direction.
sjl
Merge into Existing PDF
Re: Merge into Existing PDF
Support · Mar 28, 2007Hello,
The reason you are getting the error is because you are trying to write the output pdf file using the same name as the input pdf file while it is being used. The reason for this is because our product, in an effort to be as efficient as possible, only reads in the part of the merged or appended PDF that it needs and then uses the rest of it right at the time it draws out the new file. So when you try to draw a PDF to one of the same files that it is merging or appending the file is still in use.
The work around this is to open the file and read the contents of that file into a byte array and then close that file. Then you can create a PdfDocument out of that byte array and create a MergeDocument out of that PdfDocument. Now when you are going to Draw the file, you can specify the filepath to your original file because the file is no longer being used by any other processes.
The code for this would be something like this:
FileStream fs = new FileStream(@"C:\temp\DocumentA.pdf", FileMode.Open );
byte[] contents = new byte[ fs.Length ];
fs.Read( contents, 0, (int)fs.Length );
fs.Close();
PdfDocument pdf = new PdfDocument( contents );
MergeDocument document = new MergeDocument( pdf );
document.Append( @"C:\DocumentB.pdf", 1, 2 );
document.Append( @"C:\DocumentC.pdf" );
document.Draw( @"C:\temp\DocumentA.pdf" );
You can create a PDF document dynamically and can merge the existing PDF to this PDF and finally save the PDF to the disk. The sample code would be some thing like this.
//Create a Merge Document object
MergeDocument merDoc = new MergeDocument();
//Add dynamic contents, pages etc to this document.
merDoc.Pages.Add(new Page());
//Merge existing PDF documents to this document
merDoc.Append(@"C:\Temp\DocB.pdf");
merDoc.Append(@"C:\Temp\DocC.pdf");
//save the PDF with a name
merDoc.Draw(@"C:\Temp\OutPut.pdf");
Thanks,
ceTe Software Support Team.
Re: Merge into Existing PDF
Forum member · Mar 29, 2007That helps a bunch. Thanks for the quick reply.