Hi,
I am using MergeDocument object to create the dynamic pdf. The process I am following is as below..
Open a PDF template using MergeDocument, fill in the fields inside it dynaically and finally save the document using DRAW method at perticular location.
The above works fine. Now my requorement is as follows..
I am creating a mergedocument as was explained above and saving in a file say "TEST.PDF". Now I am looping through n-number of templates, and all dynamic PDF's generated wanted to append at the same location and on the same file "TEST.PDF"
I am using following code for the same....this is just a small piece of code..creating of mergedocument code is not attahced here
If File.Exists(l_Str_FilePath) Then
MergeDoc.Draw(l_Str_FilePath)
Else
MergeDoc.Append(l_Str_FilePath)
MergeDoc.Draw(l_Str_FilePath)
End If
The above code says file is in use by another process.
Can you please help me in giving the proper solution?
Append document to existing PDF and store in the same file
Re: Append document to existing PDF and store in the same file
Support · May 1, 2007Hello,
Our product does not allow you to draw directly to the same file that you are merging or appending. 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.
There is however a way around this but keep in mind you will loose some efficiency by doing it this way (not drastic inefficiency but rather a slight decrease).
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 file path to your original file because the file is no longer being used by any other processes.
The code for this work around would be something like this:
'Create a filestream out of your file
Dim fs As FileStream = New FileStream("C:\Temp\HelloWorld.pdf",FileMode.Open)
Dim contents() As Byte = New Byte( fs.Length ) {}
'Get the contents of the filestream
fs.Read(contents, 0, CType(fs.Length, Integer))
'Close the filestream
fs.Close()
Dim pdf As PdfDocument = New PdfDocument(contents)
Dim document As MergeDocument = New MergeDocument(pdf)
'Add any content here you would like
Dim page As Page = document.Pages(0)
page.Elements.Add(New Circle(0, 0, 100, Color.Blue))
'Save the file to the same location as you started with
document.Draw("C:\Temp\HelloWorld.pdf")
Thanks,
ceTe Software Support Team
Re: Append document to existing PDF and store in the same file
Forum member · May 1, 2007Thanks for your quick help.
I am facing another problem, listed as below...
I am having 4 files which has dynamically populated fields values in all following PDF file.
The file names are..
file_1.pdf
file_2.pdf
file_3.pdf
file_4.pdf
I am using following code to append all 4 files into 1 "FILE.PDF" file
Dim MyDocument As MergeDocument = New MergeDocument("file_1.pdf")
MyDocument.Append("file_2.pdf")
MyDocument.Append("file_3.pdf")
MyDocument.Append("file_4.pdf")
MyDocument.Draw("FILE.PDF" )
The file is getting appended into single file called FILE.PDF but I am loosing the dynamically populated data in a new file.
Can you please help me in getting the solution for this?
Thank you
Re: Append document to existing PDF and store in the same file
Support · May 1, 2007So are the form fields already filled in (filled in while saved to disk) and you loose the values when you merge them together? The only reason this would happen is if you are specifying a MergeOption to remove the form fields. Make sure you are not doing that.
Otherwise are you trying to fill in the form fields individually and then merge them all together (for example you fill in file1, 2,3 and 4 then you try to merge file1, 2, 3 and 4)? If this is the case then you will actually just want to change the order in which you do these. In other words, first merge the documents together (all to one MergeDocument) then fill in all the form fields in the different pages (but still working from the same MergeDocument). This way you should have no problem.
If either of those methods was not a correct interpretation of your issue, or you continue to have problems, feel free to let us know.
Thanks,
ceTe Software Support Team