Hi,
When I try to replace a text with a radiobutton via FieldType.Radio I get an error message like this:com.aspose.pdf.facades.AFormEditor.addField(Unknown Source)
com.aspose.pdf.facades.FormEditor.addField(Unknown Source)
com.aspose.pdf.facades.AFormEditor.addField(Unknown Source)
com.aspose.pdf.facades.FormEditor.addField(Unknown Source)
com.inputdata.inputprocess.asposeapp.Main.replaceText(Main.java:254)
com.inputdata.inputprocess.asposeapp.Main.editFields(Main.java:83)
com.inputdata.inputprocess.asposeapp.Main.main(Main.java:61)
at com.aspose.pdf.facades.AFormEditor.addField(Unknown Source)
at com.aspose.pdf.facades.FormEditor.addField(Unknown Source)
at com.aspose.pdf.facades.AFormEditor.addField(Unknown Source)
at com.aspose.pdf.facades.FormEditor.addField(Unknown Source)
at com.inputdata.inputprocess.asposeapp.Main.replaceText(Main.java:254)
at com.inputdata.inputprocess.asposeapp.Main.editFields(Main.java:83)
at com.inputdata.inputprocess.asposeapp.Main.main(Main.java:61)
class com.aspose.pdf.exceptions.InvalidPdfFileFormatException: Incorrect file header
com.aspose.pdf.internal.l10p.l0v.l0h(Unknown Source)
com.aspose.pdf.internal.l10p.l0v.(Unknown Source)
com.aspose.pdf.internal.l10p.l0v.(Unknown Source)
com.aspose.pdf.internal.l7h.lf.lb(Unknown Source)
com.aspose.pdf.internal.l10p.l0t.(Unknown Source)
com.aspose.pdf.internal.l7h.lf.le(Unknown Source)
com.aspose.pdf.internal.l2t.l0p.lI(Unknown Source)
com.aspose.pdf.internal.l2t.l0p.(Unknown Source)
com.aspose.pdf.ADocument.lI(Unknown Source)
com.aspose.pdf.ADocument.(Unknown Source)
com.aspose.pdf.Document.(Unknown Source)
com.inputdata.inputprocess.asposeapp.Main.editFields(Main.java:85)
com.inputdata.inputprocess.asposeapp.Main.main(Main.java:61)
at com.aspose.pdf.internal.l10p.l0v.l0h(Unknown Source)
at com.aspose.pdf.internal.l10p.l0v.(Unknown Source)
at com.aspose.pdf.internal.l10p.l0v.(Unknown Source)
at com.aspose.pdf.internal.l7h.lf.lb(Unknown Source)
at com.aspose.pdf.internal.l10p.l0t.(Unknown Source)
at com.aspose.pdf.internal.l7h.lf.le(Unknown Source)
at com.aspose.pdf.internal.l2t.l0p.lI(Unknown Source)
at com.aspose.pdf.internal.l2t.l0p.(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.ADocument.(Unknown Source)
at com.aspose.pdf.Document.(Unknown Source)
at com.inputdata.inputprocess.asposeapp.Main.editFields(Main.java:85)
at com.inputdata.inputprocess.asposeapp.Main.main(Main.java:61)
Below are the code I use:lev.docx (25.1 KB)
private static String docx="/tmp/lev.docx";
private static String startfile="/tmp/lev.pdf";
public static void main(String[] args) throws IOException {
try {
com.aspose.words.License licenseWords = new com.aspose.words.License();
com.aspose.pdf.License licensePdf = new com.aspose.pdf.License();
String fileTotal="/Users/ronwaatsveen/Aspose.Total.Java.lic";
licenseWords.setLicense(fileTotal);
licensePdf.setLicense(fileTotal);
editFields();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void editFields() {
try {
com.aspose.words.Document docxDocument = new com.aspose.words.Document(docx);
docxDocument.save(startfile);
Document asposedoc1 = new Document(startfile);
String outfile1="/tmp/levUT1.pdf";
replaceText(asposedoc1, "«yes»", startfile, outfile1, "checkbox");
Document asposedoc2 = new Document(outfile1);
String outfile2="/tmp/levUT2.pdf";
replaceText(asposedoc2, "«no»", outfile1, outfile2, "radio");
Document asposedoc3 = new Document(outfile2);
String outfile3="/tmp/levUT3.pdf";
replaceText(asposedoc3, "«description_check»", outfile2, outfile3, "text");
System.out.println("Retfile is "+startfile+" = "+outfile3);
//clean out all codes
Document asposedoc4 = new Document(outfile3);
cleanText(asposedoc4, "«description_check»", outfile3);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String replaceText(Document asposedoc, String code, String innfile, String outfile, String fieldtype) {
String retfile=null;
try {
int i=0;
int fieldType=-1;
if (fieldtype.equalsIgnoreCase(“checkbox”)) {
fieldType=FieldType.CheckBox;
} else if (fieldtype.equalsIgnoreCase(“radio”)) {
fieldType=FieldType.Radio;
} else if (fieldtype.equalsIgnoreCase("text")) {
fieldType=FieldType.Text;
}
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(code.trim());
asposedoc.getPages().accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection2 = textFragmentAbsorber.getTextFragments();
for (TextFragment textFragment : textFragmentCollection2) {
i++;
int page = textFragment.getPage().getNumber();
Rectangle rectangle = textFragment.getRectangle();
//textFragment.setText("");
//asposedoc.save(removedPage);
FormEditor formEditor = new FormEditor(innfile,outfile);
float llx= (float) rectangle.getLLX();
float lly= (float) rectangle.getLLY();
float urx= (float) rectangle.getURX();
float ury= (float) rectangle.getURY();
formEditor.setRadioButtonItemSize(100);
//formEditor.addField(FieldType.Radio, "AddedRadioButtonField", "BBB", 1, llx, lly, urx, ury);
formEditor.addField(fieldType, "code"+i, page, llx, lly, urx, ury);
FormFieldFacade fieldFacade=new FormFieldFacade();
fieldFacade=formEditor.getFacade();
fieldFacade.setBackgroundColor(Color.blue);
fieldFacade.setBorderColor(Color.red);
//formEditor.decorateField("yes");
retfile=outfile;
formEditor.save(outfile);
}
} catch(Exception e) {
e.printStackTrace();
}
return retfile;
}