Setting multi-line text to form fields in PDFBox

1.5k views Asked by At

I'm using PDFBox to fill form fields in a pdf using below code:

            PDField nameField = form.getField("name");
            if(null != nameField){
                nameField.setValue(data.get("name")); // data is a hashmap
                nameField.setReadonly(true);
            }

The problem is, if the text is long it doesn't split to multiple lines, even though I have enabled the "multi-line" option for the field in the pdf. Do I have to do anything from the code as well to enable this?

Thanks.

1

There are 1 answers

1
Mamadou Massaly On

Remember

Setting the ressources for the fonts to be used into the TextField.

Associating the ressources with the PDAccroform of the PDDocument.

Getting a widget for the PDTextField.

Getting a rectangle for the Widget.

Setting the width and the height of the rectangle of the widget.

It would solve it. In my case, I have a height of 20 for a non multiline text and another of 80 for a multiline textfield.You can see them being the last argument of the PDRectangle constructor. The PDRectangle class is used to specify the position and the dimension of the widget that sets it's rectangle to it. The texfield widget will appear as specified by the PDRectangle.

public static PDTextField addTextField(PDDocument pdDoc,PDAcroForm pda,String value,
        String default_value,Boolean multiline,float txtfieldsyposition,float pagesheight)
{
    
    int page = (int) (txtfieldsyposition/pagesheight);
    
    if(page+1> pdDoc.getNumberOfPages()) 
    {
        ensurePageCapacity(pdDoc,page+1);//add 1 page to doc if needed 
    }
    PDTextField pdtff = new PDTextField(pda);

    PDFont font = new PDType1Font(FontName.TIMES_ROMAN);
    String appearance = "/TIMES 10 Tf 0 0 0 rg";
    
    try 
    {
        PDFont font_ = new PDType1Font(FontName.HELVETICA);
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font_);
        resources.put(COSName.getPDFName("TIMES"), font);
        pda.setDefaultResources(resources);
            
        org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget widget = pdtff.getWidgets().get(0);
        
        PDRectangle rect = null;
        if(!multiline)
            rect = new PDRectangle(80, (pagesheight - (txtfieldsyposition % pagesheight)), 450, 20);
        else
            rect = new PDRectangle(80,(pagesheight-(txtfieldsyposition%pagesheight)),450,80);
        
        PDPage pd_page = pdDoc.getPage(page);
        System.out.println(pd_page.getBBox().getHeight());
        widget.setRectangle(rect);
        widget.setPage(pd_page);
        
        PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance.setBorderColour(new PDColor(new float[]{0,0,0}, PDDeviceRGB.INSTANCE));
        fieldAppearance.setBackground(new PDColor(new float[]{255,255,255}, PDDeviceRGB.INSTANCE));
        widget.setAppearanceCharacteristics(fieldAppearance);
        widget.setPrinted(true);
        pd_page.getAnnotations().add(widget);
        System.out.println("before appearance " +pdtff.getDefaultAppearance());
        pdtff.setDefaultAppearance(appearance);
        System.out.println("after appearance "+pdtff.getDefaultAppearance());
        
        if(multiline)
        {   
            pdtff.setMultiline(true);
        }
        
        pdtff.setDefaultValue("");
        pdtff.setValue(value.replaceAll("\u202F"," "));
        pdtff.setPartialName( page +""+(int)txtfieldsyposition);
                        
    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch(IllegalArgumentException e) 
    {
        e.printStackTrace();
    }
    
    
    
    return pdtff;
    
}