Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Apr 12, 2024 7:43 am

We have a problem flattening files.
- There is an error when saving such a flatened file. The file is still created, but is still fillable (ie there are still forms).
- If we copy the fltaned PDF into another one, it save withour problem, but is still fillable (ie there are still forms).

Test code :
Code: Select all
       
        //Create a PdfDocument instance and load
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("./data/test_input.pdf");

        //Flatten all the forms in the document
        pdf.getForm().isFlatten(true);

        //copy in another pdf and save the result
        System.out.println("saving duplicate after flattening");
        PdfDocument pdf2 = new PdfDocument();
        pdf2.appendPage(pdf);
        pdf2.saveToFile("./data/test_copy.pdf");

        //Save the result document
        System.out.println("saving initial after flattening");
        pdf.saveToFile("./data/test_output_flattened.pdf");


The result is an error :
Exception in thread "main" java.lang.NullPointerException: while trying to invoke the method com.spire.pdf.graphics.PdfFont.getFontFamily() of a null object loaded from local variable 'a'

OS : Microsoft Windows Server 2016 Datacenter
Java version : version 1.8.0_351 by SAP AG
PDF/Office version :
I:spire.office for java - version 9.1.4 by E-iceblue Co., Ltd.
I:spire.doc for java - version 12.1.10 by E-iceblue Co., Ltd.

Regards

GuiPATRY
 
Posts: 21
Joined: Wed Feb 17, 2021 10:18 am

Fri Apr 12, 2024 8:30 am

Hi,

I tested the Pdf file you provided and reproduced your issue. Meanwhile, I flatten the pdf file with Adobe Acrobat, the result Pdf is same as the generated pdf file by Spire.Pdf. Therefore, I guess the reason of this issue is caused by the structure of you Pdf file, but not a bug of Spire.Pdf.


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Apr 12, 2024 8:33 am

Addtionnal information : If, instead of flattening at the Form level (ie. document.getForm().isFlatten(true);), we flatten all fields, then both the original pdf and the one where the original was copied generate an error when saving.

The code used to flatten all fields is the following :
Code: Select all
   private void flattenAllFields(PdfDocument document) {
      PdfFieldWidget widget = null;
      PdfFormWidget form = (PdfFormWidget) document.getForm();
      PdfFormFieldWidgetCollection fields = form.getFieldsWidget();
      for (int i = 0; i < fields.getCount(); ++i) {
         widget = (PdfFieldWidget) fields.get(i);
                       widget.setFlatten(true);         
      }

GuiPATRY
 
Posts: 21
Joined: Wed Feb 17, 2021 10:18 am

Fri Apr 12, 2024 9:43 am

Hi,

Thanks for your more information.
I tested the new code and reproduced your issue, as shown in the following screenshot, meanwhile, I find that the result pdf file generated by original Pdf file isn’t flatted. I have logged these 2 issues into our bug track system with the ticket number SPIREPDF-6675, our Dev team will investigate and fix it. Once it is resolved. I’ll inform you in time.
test.png


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Apr 12, 2024 12:52 pm

Thank for your help.

A workaround seems to be to use the following code :

document.getForm().setReadOnly(true);

while it's not exactly the same, the fields are no more editable

Regards
Guillaume

GuiPATRY
 
Posts: 21
Joined: Wed Feb 17, 2021 10:18 am

Mon Apr 15, 2024 2:05 am

Hi,

Thanks for your solution.
If there are any updates about the issue SPIREPDF-6675, I’ll inform you in time.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue Apr 16, 2024 6:53 am

It looks like you're facing a challenge with flattening PDF forms using the Spire.PDF library, leading to a NullPointerException. This error might stem from a null object being loaded while accessing the PDFFont.getFontFamily() method.

To tackle this issue effectively, consider the following steps:

1.Verify Form Fields: Double-check whether the PDF file you're loading (test_input.pdf) indeed contains fillable form fields. If there are no form fields present, attempting to flatten them might result in unexpected behavior.

2.Library Initialization: Ensure that the Spire.PDF library is initialized correctly and configured properly within your project. Any misconfigurations or missing initialization steps could lead to errors during form flattening.

3.Compatibility Check: Verify the compatibility between the version of Java you're using and the Spire.PDF library. Incompatibilities between different versions can sometimes cause unexpected errors or behavior.

4.Dependency Management: Make sure that all necessary dependencies for the Spire.PDF library are included in your project and are up-to-date. Missing or outdated dependencies can lead to runtime errors like NullPointerExceptions.

Considering these steps should help you diagnose and address the issue you're facing with flattening PDF forms. Additionally, if you're still encountering difficulties, you might want to explore alternative solutions or tools. Have you considered trying out ZetPDF.com? It offers a robust PDF manipulation solution and could potentially provide a smoother experience for your PDF form flattening needs.

peterFloyd
 
Posts: 1
Joined: Tue Apr 16, 2024 6:36 am

Tue Apr 30, 2024 9:12 am

Abel.He wrote:Hi,

Thanks for your solution.
If there are any updates about the issue SPIREPDF-6675, I’ll inform you in time.

Sincerely
Abel
E-iceblue support team



Hello Guillaume PATRY,

Thank you for your patience.

I'm glad to inform you that SPIREPDF-6675 has been resolved. Welcome to download and test [Spire.PDF for Java Version:10.4.9].
Our website link: https://www.e-iceblue.com/Download/pdf-for-java.html
Code: Select all
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.pdf</artifactId>
    <version>10.4.9</version>
</dependency>


If you have any questions, please let us know.


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF