HTML with checkbox to PDF

Hello!

I am converting a HTML with some checkboxes to PDF/A using Aspose.Words for Java.
Comparing checkbox.html and checkboxAspose.pdf, you can see some styling issue: checkbox.zip (189.0 KB)

Knowing that Aspose.Words mimics the behavioure of MS Word, I also converted the HTML with MS Word 2016 to PDF (checkboxMsWord.pdf), which produces a better looking pdf.

Is there a way to get a similar looking result using Aspose.Words?

Kind regards

@dvtdaten You are right in this case Aspose.Words and MS Word behaves differently. Aspose.Words reads checkboxes from HTML as checkbox Form Fields, but MS Word reads them as ActiveX checkboxes. That is why there is a difference in rendering to PDF. If you convert your HTML document to DOCX using MS Word and then convert the resulting DOCX to PDF using Aspose.Words, checkboxes will look good.
Unfortunately, there is no way to import checkboxes as ActiveX controls using Aspose.Words. However, maybe in your case you can try setting PreserveFormFields option. In this case checkboxes look better.

Document doc = new Document("C:\\Temp\\in.html");
PdfSaveOptions options = new PdfSaveOptions();
options.setPreserveFormFields(true);
doc.save("C:\\Temp\\out.pdf", options);

Hello Alexey!

My saveoptions are:

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setCompliance(PdfCompliance.PDF_A_1_A);
saveOptions.setOptimizeOutput(true);
saveOptions.setMemoryOptimization(true);

OutlineOptions outlineOptions = saveOptions.getOutlineOptions();
outlineOptions.setExpandedOutlineLevels(9);
outlineOptions.setHeadingsOutlineLevels(9);
 
saveOptions.setDisplayDocTitle(true);
saveOptions.setPreserveFormFields(true);

But the result still looks the same as before.
Are there any other options to get a better result?

@dvtdaten In your code you are using PDF_A_1_A compliance. Form fields are not preserved as form fields in PDF/A. If you need to use this compliance, you can try replacing checkbox formfields with the corresponding Windings symbols:

Document doc = new Document("C:\\Temp\\in.html");

// Replace form field with the corresponding Wingdings characters.
for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_FORM_CHECK_BOX)
    {
        FormField ff = (FormField)f.getEnd().getPreviousSibling();
        Run checkBox = new Run(doc, ff.getChecked() ? "\u00FE" : "\u00A8");
        checkBox.getFont().setName("Wingdings");
        f.getEnd().getParentNode().insertAfter(checkBox, f.getEnd());
        f.remove();
    }
}

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setCompliance(PdfCompliance.PDF_A_1_A);
saveOptions.setOptimizeOutput(true);
saveOptions.setMemoryOptimization(true);

OutlineOptions outlineOptions = saveOptions.getOutlineOptions();
outlineOptions.setExpandedOutlineLevels(9);
outlineOptions.setHeadingsOutlineLevels(9);

saveOptions.setDisplayDocTitle(true);

doc.save("C:\\Temp\\out.pdf", saveOptions);

out.pdf (48.5 KB)

Thank you @alexey.noskov, the result looks better now.

1 Like