Upgraded to latest Aspose.NET Total- can't work with header/footers anymore

Before upgrading I was handling adding header/footers via memorystreams, now the constructor is deprecated. How can I still provide the same functionality working with streams, not files.

private static MemoryStream SetHeaderAndFooter(MemoryStream pdf, string header, string footer)
{
var outputPdf = new MemoryStream();
try
{
var fileStamp = new PdfFileStamp(pdf,outputPdf);
fileStamp.AddHeader(headerText, 5);
fileStamp.AddFooter(footerText, 10, 0, 0);
fileStamp.Close();

return new MemoryStream(outputPdf.ToArray());
}
catch .....
}

Hi Gustavo,


Thanks for your inquiry. Please check following code snippet, you can save PdfFileStamp to stream as following. Hopefully it will help you to accomplish the task.

ā€¦
ā€¦

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 ?


The facade is not initialized correctly. Please provide pdf document to the process.

Hi Gustavo,


Thanks for your inquiry. I have tested the scenario with a sample PDF document using Aspose.Pdf for .NET 9.8.0 and unable to notice any issue. We will appreciate if you please use latest version for Aspose.Pdf for .NET, if issue persist then please share your complete sample code along with sample PDF document. So we will test the scenario and guide you accordingly.

Best Regards,

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

@MichaelJanke

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.

@MichaelJanke

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.

1 Like

@pjb_maintology

Thanks for posting your suggestions that worked for you. It would really be helpful for others facing similar issue.