Flattening Causes Truncation and Missalignment

In newest version of the Aspose PDF library (24.9), when filling fillable fields and then calling the flattening module, the fillable fields are truncated and miss-aligned.

In the current ancient version we use (8.6.0) this issue does not occur.

Please advise as to why this is occurring.

1 Like

@dwdogtags

It seems that you are experiencing issues with truncation and misalignment of fillable fields when flattening PDFs using the latest version of the Aspose PDF library (24.9). This problem does not occur in the older version (8.6.0) that you are currently using.

While I don’t have specific information on why this issue is occurring in the newer version, it is possible that changes in the library’s handling of form fields or rendering could be affecting the output.

To flatten all fields in a PDF document using Aspose PDF, you can use the following code snippet:

import com.aspose.pdf.facades.Form;

public class FlattenAllFieldsInExistingPDFFile {
    public static void main(String[] args) {
        // Open the document
        Form pdfForm = new Form();
        // Bind the source PDF file
        pdfForm.bindPdf("input.pdf");
        // Flatten fields
        pdfForm.flattenAllFields();
        // Save the output
        pdfForm.save("output.pdf");
    }
}

If the issue persists, I recommend checking the release notes or documentation for version 24.9 to see if there are any known issues or changes related to form field flattening. Additionally, you might want to reach out to Aspose support for further assistance, as they can provide more detailed insights into the changes made in the library.

If you find that the problem is critical, you may consider reverting to the older version until a fix is provided in a future release.

Sources:
[1]: FlattenAllFieldsInExistingPDFFile.java

This is with a restful api written in C#. I have seen this same topic posted on the internet and still nothing is working as the oldest version.

Please advise.

@dwdogtags

Can you please share your sample source and output PDF files for our reference along with the sample code snippet that you are using? We will test the scenario in our environment and address it accordingly.

I have attached the PDF with blank fillable fields.

PSMICProxy.pdf

I have also attached what it look like when I run it through the latest version using code very similar to what is used with the version 8.6.

This is the template:
PSMICProxy.pdf (190.5 KB)

Version24.9
Flattened PDF
Flattened.pdf (163.8 KB)

Version24.9
UnFlattened PDF
NotFlattened.pdf (166.2 KB)

Version 8.6 Flattened using the same template and the same flattening method:
OlderVersionFlattened.pdf (163.4 KB)

Below is the code in the core version using 24.9. The code used in 8.6 in dotnet 4.8 is identical.

    public static baseResponse AssembleDocumentRequest(DocumentAssemblyRequest docGenRequest)
    {
        Aspose.Pdf.License license = new Aspose.Pdf.License();
        license.SetLicense("Aspose.PDF.NET.lic");
        Aspose.Pdf.Forms.Field.FitIntoRectangle = true; -- I have tried this as true and false;
        baseResponse returnModel = new baseResponse();
        using (Aspose.Pdf.Document generatedDoc = new Aspose.Pdf.Document())
        {
            docGenRequest.DocumentData.ForEach(d =>
            {
                Aspose.Pdf.Document doc = null;
                if (d.AssemblyType == DocumentAssemblerLibrary.Models.Enums.AssemblyType.ByteArray)
                {
                    doc = new Aspose.Pdf.Document(new MemoryStream(d.DataBytes()));
                }
                else if (d.AssemblyType == DocumentAssemblerLibrary.Models.Enums.AssemblyType.File)
                {
                    doc = new Aspose.Pdf.Document(d.data);
                }
                if (d.FillableFields.Count > 0)
                {
                    doc = new Aspose.Pdf.Document(PopulateFillableFields(doc, d));
                }
                generatedDoc.Pages.Add(doc.Pages);
            });

            using (MemoryStream msOutput = new MemoryStream())
            {
                generatedDoc.Save("test.pdf");
                generatedDoc.Save(msOutput);
                returnModel.DocumentData.DocumentFormat = DocumentAssemblerLibrary.Models.Enums.DocumentFormat.PDF;
                returnModel.DocumentData.data = Convert.ToBase64String(msOutput.ToArray());
            }

        }
        return returnModel;
    }
	
    private static MemoryStream PopulateFillableFields(Aspose.Pdf.Document doc, DocumentModel d)
    {
        MemoryStream msReturn = new MemoryStream();
        Aspose.Pdf.License license = new Aspose.Pdf.License();
        license.SetLicense("Aspose.PDF.NET.lic");
        bool boolIsError = false;
        StringBuilder strB = new StringBuilder();
        Aspose.Pdf.Facades.Form pdfEdit = new Aspose.Pdf.Facades.Form(doc);
        List<string> pdfFields = [.. pdfEdit.FieldNames];
        pdfFields.Where(val => !d.FillableFields.AsEnumerable()
                                             .Any(dr => dr.Key == val))
                                             .ToList()
                                             .ForEach(sErr =>
                                             {
                                                 boolIsError = true;
                                                 strB.AppendLine("No corresponding value was found in the datatable for value " + sErr + "\n");
                                             });

        if (!boolIsError)
        {
		
			//Below are two different versions of the code I have tried to use that produced the same result.
		
			//1.  This is similar to the original code and causes the truncation.
            //d.FillableFields.ToList().ForEach(f =>
            //{
            //    pdfEdit.FillField(f.Key, f.Value, true);
            //});

            //pdfEdit.FlattenAllFields();

			//2.  I also tried this version which was the same result as the code above.
            foreach (Aspose.Pdf.Forms.Field item in doc.Form.Fields)
            {
                if (d.FillableFields.Keys.Contains(item.FullName))
                {
                    item.Value = d.FillableFields[item.FullName];
                    item.Flatten();
                }
            }

            doc.Save(msReturn);

        }
        else
        {
            logger.Error(strB.ToString());
        }
        return msReturn;
    }
}

@dwdogtags

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-58235

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.

Any chance on getting an updated of when this will be researched. I am moving forward with using the older version that works with out fillable templates, but it would be preferable to use dot net core 8 vs .net framework 4.8.

@dwdogtags

We are afraid that the ticket hasn’t been resolved yet. It is logged under free support model and will be prioritized on first come first serve basis. As soon as we have some updates about its resolution, we will inform you. Please spare us some time. We are sorry for the inconvenience.