Not flattening all fields on pdf via .NET

I have a project that is filling pdfs with data, combining, then saving them all asynchronously. I am having an issue where sometimes a few of the documents in the packets are not getting flattened. I am calling FlattenAllFields() on the pdf form just before saving it to a new memory stream. I am not seeing any errors and the pdf gets saved and opens fine. Its just the fields are still editable and usually missing most of the data I was trying to fill with.
Aspose.pdf 24.3.0

Code:

public MemoryStream BuildPdfTemplate(Dictionary<string, string> dict, Stream stream)
{
if (dict == null) return new MemoryStream();

 var pdf = GetPdf(stream);
 MapPdfDataToFields(dict, pdf);
 var memStream = SavePdf(pdf);
 return memStream;

}
public Form GetPdf(Stream stream)
{
var pdf = new Form(stream);
return pdf;
}
public void MapPdfDataToFields(Dictionary<string, string> dict, Form pdf)
{
if (dict == null) return;

foreach (var fieldName in pdf.FieldNames)
{
    if (dict.TryGetValue(fieldName, out string? val)) { pdf.FillField(fieldName, val); }
}

}
public MemoryStream SavePdf(Form pdf)
{
var memStream = new MemoryStream();
pdf.FlattenAllFields();
pdf.Save(memStream);
memStream.Seek(0, SeekOrigin.Begin);
return memStream;
}

@wheart

It seems that you are encountering an issue where some fields in your PDF documents are not being flattened properly, even after calling the FlattenAllFields() method. Here are a few suggestions to help you troubleshoot and resolve this issue:

  1. Ensure All Fields Are Flattened: Instead of using FlattenAllFields(), you can iterate through each field in the form and flatten them individually. This can sometimes yield better results, especially if there are specific fields that are not being processed correctly. Here’s how you can modify your SavePdf method:

    public MemoryStream SavePdf(Form pdf)
    {
        var memStream = new MemoryStream();
        // Flatten each field individually
        foreach (var field in pdf.FieldNames)
        {
            pdf.FlattenField(field);
        }
        pdf.Save(memStream);
        memStream.Seek(0, SeekOrigin.Begin);
        return memStream;
    }
    
  2. Check for Field Types: Ensure that the fields you are trying to flatten are indeed flattenable. Some field types may not support flattening, which could lead to them remaining editable.

  3. Review Asynchronous Operations: Since you mentioned that you are saving documents asynchronously, ensure that there are no race conditions or timing issues that might affect the flattening process. It might be helpful to log the state of the fields before and after flattening to see if they are being modified as expected.

  4. Test with a Simple PDF: To isolate the issue, try testing your flattening logic with a simple PDF that contains only a few fields. This can help determine if the problem is with the specific PDFs you are using or with the flattening logic itself.

  5. Check for Errors: Although you mentioned not seeing any errors, consider adding error handling or logging to capture any exceptions that might occur during the flattening process.

By implementing these suggestions, you should be able to identify the cause of the issue and ensure that all fields are flattened correctly in your PDF documents. If the problem persists, consider providing more details about the specific types of fields that are not being flattened for further assistance.

I had considered doing a field by field flatting. So that will probably be something that I will add to my code.
One question I have, is there a way to check a pdf after to see if the form fields are flatted or not?
Also, are there examples or standards as to dependency injecting code that contains these libraries?
thanks again

@wheart

One question I have, is there a way to check a pdf after to see if the form fields are flatted or not?

You can check whether the PDF still has form fields in it after flattening the document. Also, please try using 24.11 version of the API which is the latest one. In case issue still persists, please share a problematic PDF document after filling it. You can save it without flattening and leave that part for us to reproduce the issue. We will test the scenario in our environment and address it accordingly.

I am not sure if this error is related to the issue Im having or not? I did upgrade to the latest Aspose.pdf 24.11.1 I have only received this error once and unable to replicate the issue. A side note: I am loading a special font into the FontSettings.DefaultInstance.

Code to set font
public bool SetFontsDirectory(string fontsDirectory)
{
if (!String.IsNullOrEmpty(fontsDirectory) && Path.Exists(fontsDirectory))
{
var defaultInstance = FontSettings.DefaultInstance;
var originalFontSources = defaultInstance.GetFontsSources();
var folderFontSource = new FolderFontSource(fontsDirectory, true);
FontSourceBase[] updatedFontSources = { originalFontSources[0], folderFontSource };
defaultInstance.SetFontsSources(updatedFontSources);
return originalFontSources.Length < defaultInstance.GetFontsSources().Length;
}
return false;
}

Error I received:
System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.)
—> System.NullReferenceException: Object reference not set to an instance of an object.
at #=zJwqu50vy5wDKfVyRNrHRjOepUHVl3rJCECnPsFKDDAY1ROJBCA==.#=zzGEUQzWJKsgm()
at Aspose.Pdf.Operators.SelectFont.#=z6Ausj8A=(#=zJwqu50vy5wDKfVyRNrHRjOepUHVl3rJCECnPsFKDDAY1ROJBCA== #=znMN8bOM=)
at Aspose.Pdf.Operators.SelectFont…ctor(Int32 #=znFbXgWg=, #=zJwqu50vy5wDKfVyRNrHRjOepUHVl3rJCECnPsFKDDAY1ROJBCA== #=znMN8bOM=)
at #=zJwqu50vy5wDKfVyRNrHRjOepUHVl3rJCECnPsFKDDAY1ROJBCA==.#=zoWP9NhA=(Int32 #=znFbXgWg=)
at Aspose.Pdf.Operator.#=zvmcWD3Q=(Int32 #=znFbXgWg=, #=zdOrWpSJLM0t5en9ceP8ZZdrTyzT7Z94kCw1XngdoGawM #=znMN8bOM=)
at Aspose.Pdf.OperatorCollection.#=zJLFX3Wf1RoFu()
at Aspose.Pdf.OperatorCollection.#=zTrdKe1JVa0Pd()
at Aspose.Pdf.OperatorCollection.#=zWe_SUlwFPCCA()
at Aspose.Pdf.Annotations.Annotation.Flatten()
at Aspose.Pdf.Facades.Form.FlattenField(String fieldName)
at PdfBuilder.MapPdfDataToFields(Dictionary`2 dict, Form pdf) …

@wheart

Its hard to determine the issue cause without replicating it in our environment. May be it is related to some specific PDF file. If you can provide some steps to reproduce the issue in our environment, we will further proceed with the investigation.

I have isolated the documents and running in only a console app on the latest 24.11.0 version its giving strange behavior. It cuts off the top of the document on the page. Idk if something is wrong with the fillable pdf or ? When I run this with multiple documents being combined I dont always get errors but tend to do get serialization errors and a message about :Trailer not found
I have zipped up my solution that can be run locally.
The /Lobpacket/RequestPacketData.cs is allowing 2 documents to be included in the packet. Sometimes when I only allow the Acord25 it will give an error and not even create a packet.

Instead I have attached the fillable pdf.
Acord25-DocBuilder.pdf (456.4 KB)

Also how do I share a large zip ? I would rather it not be public.

I have been able to recreate the problem with the zipped .net project. It will occur every 3-4 times if you run it in a row. Running locally on a Win 11, i7 with 32gb ram. I do see the cpu/ram go over 70% during runtime.
I get lots of different errors:
BuildDocument Field Dict:Acord25-DocBuilder.pdf Message: Trailer not found

DocBuilder.Logic.DocBuilders.DocFieldBuilder[0]
BuildDocument Field Dict:Acord25-DocBuilder.pdf Message: Serialization error.

  Exception thrown: 'System.ArgumentException' in Aspose.PDF.dll

Exception thrown: ‘System.ArgumentException’ in Aspose.PDF.dll

Unhandled exception. System.Exception: Unable to merge pdfs
—> System.ArgumentException: Exception occured during the processing file: 3
—> Aspose.Pdf.InvalidPdfFileFormatException: Trailer not found

Project is .net 8 with Aspose.PDF 24.11.0
LoadTest-Fails.zip (903.4 KB)

@wheart

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58834

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.