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.

Wed Apr 02, 2025 2:36 pm

Hi, I'm a paid customer
java 17 spirepdf version 11.3.5

I have problems flattening a pdf document containing multiple PdfRadioButtonListItem

Check this code, based on your example at https://github.com/eiceblue/Spire.PDF-f ... World.java

Code: Select all
  public static void main(String[] args) throws Exception{
    PdfDocument doc = new PdfDocument();
  //Create one page
    PdfPageBase page = doc.getPages().add();
   
    //Create font
    PdfFont font = new PdfFont(PdfFontFamily.Times_Roman, 12f, EnumSet.of(PdfFontStyle.Regular));

    //Create a pdf brush
    PdfBrush brush = PdfBrushes.getBlack();

    float x = 80;
    float y = 350;
    float tempX = 0;

    //Create a pdf radio button field
    PdfRadioButtonListField radioButton = new PdfRadioButtonListField(page, "RadioButton");
    radioButton.setRequired(true);
    //Add items into radio button list
    for (int i = 0; i < 2; i ++)
    {
        PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem();
        fieldItem.setBorderWidth(0.75f);
        fieldItem.setBounds(new Rectangle2D.Float(x, y, 15, 15));
        radioButton.getItems().add(fieldItem);
        tempX = x + 20;
        page.getCanvas().drawString("Item" + i, font, brush, tempX, y);
        x = tempX + 100;
    }

    //Add in form
    doc.getForm().getFields().add(radioButton);

    String output="result.pdf";

    doc.saveToFile(output);
   
    doc.close();
    PdfDocument doc1 = new PdfDocument();
   
    doc1.loadFromFile("result.pdf");
   
    doc1.getForm().isFlatten(true);
   
    doc1.saveToFile("result-flatten.pdf");
   
}


The last saveToFile after the flatting causes this error:

Code: Select all
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.spire.pdf.PdfPageBase.spr┃∭()" because the return value of "com.spire.pdf.fields.PdfField.getPage()" is null
   at com.spire.pdf.widget.PdfFormWidget.spr▔∭(Unknown Source)
   at com.spire.pdf.widget.PdfFormWidget.spr╕∮(Unknown Source)
   at com.spire.pdf.widget.PdfFormFieldWidgetCollection.spr┭∭(Unknown Source)
   at com.spire.pdf.fields.PdfFieldCollection.remove(Unknown Source)
   at com.spire.pdf.widget.PdfFormWidget.spr┟∬(Unknown Source)
   at com.spire.pdf.packages.spraee.spr╸∮(Unknown Source)
   at com.spire.pdf.packages.spryuo.spr╸∮(Unknown Source)
   at com.spire.pdf.packages.sprpye.spr┙⌭(Unknown Source)
   at com.spire.pdf.packages.sprpye.spr‣⌮(Unknown Source)
   at com.spire.pdf.packages.sprmlo.spr“╽(Unknown Source)
   at com.spire.pdf.packages.sprmlo.spr®╽(Unknown Source)
   at com.spire.pdf.packages.sprmlo.spr”╽(Unknown Source)
   at com.spire.pdf.packages.sprmlo.spr┹╼(Unknown Source)
   at com.spire.pdf.packages.spradp.spr▓§(Unknown Source)
   at com.spire.pdf.PdfDocumentBase.save(Unknown Source)
   at com.spire.pdf.PdfDocument.saveToFile(Unknown Source)

donspa72
 
Posts: 7
Joined: Wed Dec 11, 2024 1:30 pm

Thu Apr 03, 2025 8:39 am

Hello,

Thanks for your inquiry.
I have reproduced your issue and logged this issue to our tracking system with the ticket SPIREPDF-7457. Our development team will investigate further and fix it. I will notify you once it is fixed. Sorry for the inconvenience.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 732
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF