Hi Gustavo,
var fileStamp = new PdfFileStamp();<o:p></o:p>
fileStamp.BindPdf(pdf);
fileStamp.AddHeader(headerText, 5);
fileStamp.AddFooter(footerText, 10, 0, 0);
fileStamp.Close();
fileStamp.Save(outputPdf);
return new MemoryStream(outputPdf.ToArray());
Please feel free to contact us for any further assistance.
Best Regards,
I tried your code snippet and now Iām getting the following error when executing fileStamp.Save(outputPdf); line. What do I need to do ?
Hi Gustavo,
Hi all,
I installed apose pdf.net some days ago, so hope to use a valid version, mine is 20.3.0.0
Iām working with footers, so got the code from the example and it works fine.
Anyway, when adding footers to a lot of files, the program uses lots of memory, more than my pc can offer. So I tried several things to close objects. When adding a line after Close():
fileStamp.Close();
fileStamp.Dispose();
I get the errorcode mentioned earlier in this post - āThe facade is not initialized correctly. Please provide pdf document to the processā. Even without closing the fileStamp, the same exception occurs.Itās not about existance of files. Both, input- and output-file are present.
So, could anyone give me a hint, how to release memory after adding a footer line via a fileStamp?
Best regards, Michael
Thanks for contacting support.
Would you kindly try to use recommended Aspose.Pdf DOM approach to add stamps or header/footer in PDF documents. If the mentioned issue still persists, please share complete sample code snippet along with sample PDF document. We will test the scenario in our environment and address it accordingly.
Thanks a lot for your answer. Adding a footer works even faster with dom. Anyway, the memory issue is not really solved. In my program I convert a lot of different file formats to pdf, final action in that part is merging all these files together, while writing the file path as a footer to the documents.
** creating the output document and adding all files in the āworklistā
outputDocument = New Document
For Each workItem In workList
_concatFile(workItem.RelativePath, workItem.PdfFileLong, outputDocument)
Next
** opening the current pdf as a document, adding footer and append to output
Private Sub _concatFile(relativePath As String, inputFilePath As String, ByRef outputDocument As Document)
Dim pdfDocument = New Document(inputFilePath)
_addFooterText(pdfDocument, relativePath)
outputDocument.Pages.Add(pdfDocument.Pages)
End Sub
** adding the footer to the document
Private Sub _addFooterText(pdfDocument As Document, footerText As String)
Try
Dim textStamp As New TextStamp(footerText) With {
.BottomMargin = 6,
.HorizontalAlignment = HorizontalAlignment.Center,
.VerticalAlignment = VerticalAlignment.Bottom
}
With textStamp.TextState
.BackgroundColor = Color.LightGray
.ForegroundColor = Color.DarkBlue
End With
For Each page In pdfDocument.Pages ' Add footer on all pages
page.AddStamp(textStamp)
Next
Catch ex As Exception
RaiseEvent LogEvent("MergePdfFiles:Exception:" & ex.Message)
End Try
End Sub
When merging 219 files with total size of 233 MB, the resulting output file has a size of 108 MB, but RAM went up to 1 GB usage. Number of input files will probably be much more than that, so Iād be happy to see a way to reduce memory consumption.
By the way, if I add pdfDocument.Dispose at the end of routine _concatFile, I canāt save the outputDocument because of āclosed streamā. Not sure, what that meansā¦
Edit: when running this without adding footstamps, memory consumption is the size of the resulting merge file. So adding the footer is responsible.
Thanks for your feedback.
Once a Document is disposed, all allocated resources and opened streams are also get closed with it which is the reason for facing this error.
Regarding memory consumption, would you kindly share some sample PDF files for our reference along with the value of the footer string that you are adding during the concatenation process? We will test the scenario in our environment and address it accordingly.
In case your still looking for a solution, we had a similar issue with excessive memory usage when doing multiple actions on PDFās using Aspose components. We solved it by ensuring each component is disposed then set the variable to null and then doing a forced .NET garbage collection. e.g.:
pdfDocA.Dispose();
pdfDocA = null;
pdfDocB.Dispose();
pdfDocB = null;
GC.collect();
This kept the memory usage to what is expected.
Thanks for posting your suggestions that worked for you. It would really be helpful for others facing similar issue.