Builder processing

I want to create a custom Questionnaire, but i’m facing the problem to get out to textbox. how could i achieve that ?

@nileshmishra Could you please attach your document here for our reference? We will check it and provide you more information. Unfortunately, it is impossible to analyze the problem using screenshots.

@alexey.noskov - my document is empty file. The screenshot is the output & here is the code for the same.

public void run(String... args) throws Exception {
    //List<Questionnaire> questionnaireList = getQuestionnaireList();
    var document = new Document();
    DocumentBuilder builder = new DocumentBuilder(document);

    for (Questionnaire questionnaire : getQuestionnaireList()) {
        builder.write(questionnaire.getSectionName()); //todo set font size
        builder.write(ControlChar.LINE_BREAK);
        builder.write(ControlChar.LINE_BREAK);

        for (QuestionnaireDetails qDetails : questionnaire.getQuestionnaireDetails()) {
            //write question
            builder.write(qDetails.getQuestion().getDescription());
            builder.write(ControlChar.LINE_BREAK);

            //write answer
            var answer = qDetails.getAnswer();
            if (ShapeTypeEnum.TEXT_BOX == answer.getType()) {
                var textBoxShape = builder.insertShape(ShapeType.TEXT_BOX, 100.0, 100.0);

                var textBox = textBoxShape.getTextBox();
                //textBox.setFitShapeToText(true);
                //textBox.setInternalMarginTop(15.0);
                //textBox.setInternalMarginBottom(15.0);
                //textBox.setInternalMarginLeft(15.0);
                //textBox.setInternalMarginRight(15.0);
                builder.moveTo(textBoxShape.getLastParagraph());
                Font font = builder.getFont();
                //font.setSize(16);
                //font.setColor(new Col);
                //font.setBold(true);
                //font.setName("Arial");
                //font.setUnderline(Underline.DASH);
                //builder.write("Sample text.");

                System.out.println("PdfGeneratorServiceImpl.processQuestionnaire");
                System.out.println(font);

                builder.write(answer.getTextBoxValue());
                //textBox.breakForwardLink(); this gave an exception
                builder.write(ControlChar.LINE_BREAK);

            } else if (ShapeTypeEnum.CHECK_BOX == qDetails.getAnswer().getType()) {
                for (var entry : answer.getCheckBoxValues().entrySet()) {
                    var textBoxShape = builder.insertCheckBox(entry.getKey(), entry.getValue(), 0);
                    builder.write(ControlChar.TAB);
                    builder.write(entry.getKey());
                    builder.write(ControlChar.LINE_BREAK);
                }
            }
            builder.write(ControlChar.LINE_BREAK);
        }
        builder.write(ControlChar.PAGE_BREAK);
        builder.insertHtml("<hr/>");
    }
    document.save("C:\\Doc gen - samples\\test-ques1.pdf", PDF);
}

@nileshmishra As I can see your target format is PDF and your goal is to create an editable textbox in the output PDF. In this case you should either use text input form field or structured document tag:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Legacy form field
builder.insertTextInput("MyTextInput", TextFormFieldType.REGULAR, "", "Some default text", 0);
// SDT
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.INLINE);
builder.writeln();
builder.insertNode(tag);

PdfSaveOptions opt = new PdfSaveOptions();
opt.setPreserveFormFields(true);
doc.save("C:\\Temp\\out.pdf", opt);

out.pdf (25.1 KB)

@alexey.noskov - We required a textbox with value prefilled in and once the value in text box is filled, we want our cursor to move to new line and process additional questionnaire values. we have a list of data and we want to process in the similar way.
If you’ll see my first screenshot, I was not able to exit out of the text box and the processing occurred inside of textbox.

@nileshmishra Thank you for additional information.

You have tow options here: either use a separate instance of DocumentBuilder to edit text inside text box or call DocumentBuilder.moveToDocumentEnd after finishing editing text box.

I tried using the moveToDocumentEnd and it didn’t help.

Sample Code:

builder.write(answer.getTextBoxValue());
builder.write(ControlChar.LINE_BREAK);
builder.moveToDocumentEnd();
//builder = new DocumentBuilder();
//builder.moveTo(document); //todo
// .breakForwardLink();
builder.write("APPPPPP");
builder.write("###########");
builder.write(ControlChar.LINE_BREAK);

Output:

test-ques.pdf (30.0 KB)

@nileshmishra Please try putting an empty Run node after the inserted shape and move to it after building the shape. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape textBoxShape = builder.insertShape(ShapeType.TEXT_BOX, 100.0, 100.0);
// Put an empty Run after the shape.
textBoxShape.getParentNode().insertAfter(new Run(doc), textBoxShape);
builder.moveTo(textBoxShape.getLastParagraph());
builder.write("Some text");

// Move to teh previously inserted run.
builder.moveTo(textBoxShape.getNextSibling());
builder.writeln();
builder.write("APPPPPP");
builder.write("###########");
builder.write(ControlChar.LINE_BREAK);

doc.save("C:\\Temp\\out.docx");

Thanks @alexey.noskov !! This worked :slight_smile:

1 Like

HI @alexey.noskov ,
One additional follow up, is it possible to process this piece of code via builder inside a section using a Range object.

I mean my word document is split into different sections. I want to process this logic inside an range(section). Can we achieve that?

TIA

@nileshmishra You can move document builder to the particular section in your document using DocumentBuider.moveToSection method and then move DocumentBuider within the section.

@alexey.noskov This method requires a integer sectionIndex, I rather have a section object & Range both from aspose lib. Can we navigate via any of these?
If not, how can I get the section index from the section or range?

@nileshmishra You can easily get section index using the following code:

int sectionIndex = doc.getSections().indexOf(section);
1 Like

@alexey.noskov
I’m using below snipet to write a checkbox in word. the checkbox has a default checking style as ‘x’ . Can we replace to something like tick or any other shape?

var doc = new Document();
var builder = new DocumentBuilder();
builder.moveToSection(0);
builder.insertCheckBox("test", true, 0);
builder.write(ControlChar.TAB);
builder.write("Yes");
builder.insertCheckBox("test-false", false, 0);
builder.write(ControlChar.TAB);
builder.write("No");
doc.save("testing.pdf", SaveFormat.PDF);

@nileshmishra You can use Wingdings symbols instead checkboxes. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(0);
builder.getFont().setName("Wingdings");
builder.write("\u00FE");
builder.getFont().setName("Calibri");
builder.write(ControlChar.TAB);
builder.write("Yes");
builder.getFont().setName("Wingdings");
builder.write("\u00A8");
builder.getFont().setName("Calibri");
builder.write(ControlChar.TAB);
builder.write("No");
doc.save("C:\\Temp\\out.pdf", SaveFormat.PDF);

out.pdf (21.5 KB)

1 Like