I would like to know how to create TextField in PDFBOX. (itext to pdfbox migration)

Viewed 31

Previously, textboxes were created using iText to give names, and then JavaScript was used to hide text.

I'm currently working on changing it to PDFBox. When working with PDPageContentStream, JS is unable to obtain the field name of TextField.

Is there a good way?

iText Source

        Rectangle rectangle = new Rectangle(pageSize);
        rectangle.setBackgroundColor(Color.BLACK);
        TextField text = new TextField(writer, rectangle, "text" + pageIndex);
        text.setWriter(writer);
        text.setTextColor(Color.WHITE);
        text.setFont(baseFont);
        text.setFontSize(100);
        text.setBackgroundColor(Color.BLACK);
        text.setOptions(TextField.READ_ONLY);
        text.setText("NOT PRINT");
        text.setAlignment(Element.ALIGN_CENTER);
        writer.addAnnotation(text.getTextField());

pdfBox Source (1)

            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true, true);
            contentStream.addRect(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
            contentStream.setNonStrokingColor(Color.black);
            contentStream.fill();
                contentStream.beginText();
                contentStream.setTextMatrix(Matrix.getTranslateInstance(0f,0f));
                contentStream.newLineAtOffset(x, y);
                contentStream.showText("NOT PRINT");
                contentStream.endText();

pdfBox Source (2)

        for (int i = 0 ; i < document.getNumberOfPages() ; i++) {
            PDPage page = document.getPage(i);
            PDAcroForm acroForm = new PDAcroForm(document);
            document.getDocumentCatalog().setAcroForm(acroForm);
            
            PDResources resources = new PDResources();
            acroForm.setDefaultResources(resources);
            String fontName = resources.add(font).getName();

            String defaultAppearance = "/" + fontName + " 120 Tf 1 0 0 1 260 254 rg";
            PDTextField tf = new PDTextField(acroForm);
            tf.setDefaultAppearance(defaultAppearance);
            tf.setAlternateFieldName("text"+i);
            tf.setPartialName("text"+i);
            tf.setDefaultAppearance("text"+i);
            tf.setValue("not print");
            acroForm.getFields().add(tf);
            PDAnnotationWidget widget = tf.getWidgets().get(0);
            widget.setPrinted(true);
            acroForm.setNeedAppearances(true);
            widget.setAnnotationName("text"+i);
            page.getAnnotations().add(widget);
}

JavaScript

            javascript.append("var found = false;");
            javascript.append("    if(this.URL.indexOf('" + pdf_accept_url + "') == 0){");
            javascript.append("        found = true;");
            javascript.append("}");
            javascript.append("if (found == true) {"); 
            javascript.append("    var len = " + pageCount + ";");
            javascript.append("    for (var i=0; i<len; i++) {");
            if (existPrintPermission) {
                // 1 – Field Hidden in Viewer and in Printed Document
                javascript.append("        this.getField('text'+i).display = 1;");
            } else {
                // 3 – Field Hidden in Viewer, Visible on Printed Document
                javascript.append("        this.getField('text'+i).display = 3;");
            }
            javascript.append("    }");
            javascript.append("}");

If you use Pdfbox source1, I failed to get the contentStream value from js.

When using Pdfbox source2, I succeeded in getting the value of the text field in alert, but, it does not appear on the actual screen.

0 Answers
Related