Hello,

I am trying to right align a free text annotation using PDFBox 3.0.1 but
the annotation only becomes left aligned in read mode in the final pdf.
However, if I tried to edit it using acrobat reader, edit mode shows the
right alignment. I think this is because the edit mode renders RC content.
Can you help me to figure out the issue?

package com.folia.pdf.export.lib;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.interactive.annotation.*;
import org.apache.pdfbox.pdmodel.interactive.form.PDVariableText;

import java.io.IOException;

public class TextAnnotation {

    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {
            // Add a new page
            PDPage page = new PDPage();
            document.addPage(page);

            // Define the rectangle area where the annotation will appear
            PDRectangle rect = new PDRectangle(100, 600, 200, 50);

            // Create a FreeText annotation
            PDAnnotationFreeText freeText = new PDAnnotationFreeText();
            freeText.setRectangle(rect);
            String content = "Your styled text here";
            freeText.setContents(content);

            freeText.setDefaultAppearance("/Helv 20 Tf 1 0 0 rg");

            String richContent = "<body
xmlns=\"http://www.w3.org/1999/xhtml\";>" +
                    "<p
style=\"font-family:Arial;font-size:20pt;color:#FF0000;text-align:right;\">"
+
                    content + "</p></body>";
            freeText.setRichContents(richContent);

            freeText.setQ(PDVariableText.QUADDING_RIGHT);
            freeText.getCOSObject().setNeedToBeUpdated(true);

            freeText.constructAppearances();

            page.getAnnotations().add(freeText);

            document.save("output.pdf");
            System.out.println("FreeText annotation created successfully.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Thanks
Dimuthu
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to