We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get DefaultAppearance of TextBoxField

NullPointerException is thrown when getting default appearance of TextBoxField. As per customer, it is because no page or document is set to the textboxfield, but I can not figure how to circumvent the problem.
[Java]

com.aspose.pdf.Document doc = new Document();
   doc.getPages().add();
    
   com.aspose.pdf.Table tab = new com.aspose.pdf.Table();
    
       TextState textState = new TextState("Arial");
   textState.setFontSize(14);
   tab.setDefaultCellTextState(textState);
    
   int poznamkaWidth = 165;
    
   //Set with column widths of the table
   tab.setColumnWidths("70 115 60 80 " + poznamkaWidth);
   //tab.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
    
   //Set default cell border using BorderInfo object
   tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));
    
   com.aspose.pdf.MarginInfo marginTable = new com.aspose.pdf.MarginInfo(0,0,0,16);
   tab.setMargin(marginTable);
    
   //Set table border using another customized BorderInfo object
   tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));
   //Create MarginInfo object and set its left, bottom, right and top margins
   com.aspose.pdf.MarginInfo marginInfoCell = new com.aspose.pdf.MarginInfo(2,2,2,2);
   //Set the default cell padding to the MarginInfo object
   tab.setDefaultCellPadding(marginInfoCell);
    
   //Create rows in the table and then cells in the rows
   com.aspose.pdf.Row row1 = tab.getRows().add();
   row1.setFixedRowHeight(10);
   addCellToRow(row1, "A", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);
   addCellToRow(row1, "B", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);
   addCellToRow(row1, "Číslo", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);
   addCellToRow(row1, "D", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);
   addCellToRow(row1, "Poznámka", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);
    
   ;
   for (int i = 0; i< 20; i++)
   {
       com.aspose.pdf.Row row2 = tab.getRows().add();
       row2.setFixedRowHeight(10);
       com.aspose.pdf.Cell c = row2.getCells().add("a111-5454");
   row2.getCells().add("Mudr. Jan Novák Maximilan Superman" + i);
   row2.getCells().add("c" + i);
   row2.getCells().add().getParagraphs().add(createRadioTable(doc.getPages().get_Item(1), i));

   TextBoxField tbf = new TextBoxField();

   tbf.setMargin(new MarginInfo(0,4,0,0));
//DefaultAppearance tbfDefaultAppearance = new DefaultAppearance();
//tbfDefaultAppearance.setFontSize(fontSizeTable);
//tbf.setDefaultAppearance(tbfDefaultAppearance);
   tbf.setPartialName("poznamka_" + i);
   tbf.setWidth(poznamkaWidth);
   tbf.setHeight(10);
   tbf.setMultiline(true);
   row2.getCells().add().getParagraphs().add(tbf);
   tbf.getDefaultAppearance().setFontSize(14); //<--exception!!!
   }
    
   //Add the table in paragraphs collection of the desired section
   doc.getPages().get_Item(1).getParagraphs().add(tab);
   //tbf.getDefaultAppearance().setFontSize(fontSizeTable);
   doc.save("c:/pdftest/Table_in_PDF_2.pdf");

private static void addCellToRow(com.aspose.pdf.Row row, String text, int fontStyle, int horizontalAlignment) {
    TextFragment tf = new TextFragment(text);
    tf.getTextState().setFont(FontRepository.findFont("Arial"));
    tf.getTextState().setFontStyle(fontStyle);
    tf.getTextState().setHorizontalAlignment(horizontalAlignment);
    row.getCells().add().getParagraphs().add(tf);
}
 
private static com.aspose.pdf.Table createRadioTable(Page page, int rowNumber) {
       com.aspose.pdf.Table table = new com.aspose.pdf.Table();
    table.setColumnWidths("35 35");
    com.aspose.pdf.Row r1 = table.getRows().add();
    com.aspose.pdf.Cell c1 = r1.getCells().add();
    com.aspose.pdf.Cell c2 = r1.getCells().add();
 
    table.setMargin(new MarginInfo(4,0,6,0));
 
    RadioButtonField rf = new RadioButtonField(page);
    rf.setName("radio" + rowNumber);
    page.getDocument().getForm().add(rf, 1);
    RadioButtonOptionField opt1 = new RadioButtonOptionField();
    RadioButtonOptionField opt2 = new RadioButtonOptionField();
 
    opt1.setOptionName("rbAno_" +rowNumber);
    opt2.setOptionName("rbNe_" +rowNumber);
 
    opt1.setWidth(8);
    opt1.setHeight(8);
    opt2.setWidth(8);
    opt2.setHeight(8);
 
    rf.add(opt1);
    rf.add(opt2);
 
    TextFragment anoTf = new TextFragment("Ano");
    anoTf.getTextState().setFont(FontRepository.findFont("Arial"));
    //anoTf.getTextState().setFont(FontRepository.findFont(fontName));
    anoTf.getTextState().setFontSize(8);
    anoTf.getTextState().setFontStyle(FontStyles.Regular);
 
    TextFragment neTf = new TextFragment("Ne");
    neTf.getTextState().setFont(FontRepository.findFont("Arial"));
    neTf.getTextState().setFontSize(8);
    neTf.getTextState().setFontStyle(FontStyles.Regular);
 
    opt1.setCaption(anoTf);
    opt2.setCaption(neTf);
 
    c1.getParagraphs().add(opt1);
    c2.getParagraphs().add(opt2);
 
    return table;
 
}

This Topic is created by codewarior using the Email to Topic plugin.

@opeterka,

Thanks for your patience.

We have further investigated earlier reported issue PDFJAVA-36753 and in order to resolve this problem, and in order to configure defaultAppearance, we need to connect the TextBoxField with the Document instance. Please try using the constructor with IDocument parameter instead default constructor:

TextBoxField tbf = new TextBoxField(doc);

In case you encounter any issue, please feel free to contact.