TextFragment should not show all the text

Hi,

we would like to place a Textbox on a PDF with some size. The text inside the Textbox can be larger or have more line than actually the size allow it. With my implementation the Textbox shows the correct size with its content, but an additional page is produced with the conntent which did not had space in the original Textbox. How can I prevent of happening this.
In the attachment you can see how it creates a second page.

public class AnnotationTextBoxExample {

static String inputFile = "/home/igb/sandbox/git/micros/msc-dcpcs/testdata/annotationen/Original_Grid.pdf";
static String outputFile = "/home/igb/sandbox/git/micros/msc-dcpcs/testdata/annotationen/testAnnotationen_output.pdf";

public static final String F_TOP = "F_TOP";
public static final String F_LEFT = "F_LEFT";
public static final String F_HEIGHT = "F_HEIGHT";
public static final String F_WIDTH = "F_WIDTH";
public static final String F_CLASSNAME = "F_CLASSNAME";

public static void main(String[] args) throws UnsupportedEncodingException, DecoderException {
    // open document
    com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(inputFile);
    
        Page pdfPage = pdfDocument.getPages().get_Item(Integer.parseInt(1));
        pdfPage.getPageInfo().setMargin(new MarginInfo(0, 0, 0, 0));
       TextFragment textFragment = createTextFragment(pdfPage);
       pdfPage.getParagraphs().add(createFloatingBox2(textFragment));           
            
    // save updated PDF file
    pdfDocument.save(outputFile);
}

private static TextFragment createTextFragment(Map<String, String> annotationen, Page pdfPage) {

   String text = "Text-Anno3 (4x4) (R12;C1)\r\n-F_Type: Mono\r\n-F_Size: 12\r\n\r\n"This string should overflow vertically the box (7newlines)\r\n-newline1\r\n-newline2\r\n-newline3\r\n-newline4\r\n-newline5\r\n-newline6\r\n-newline7"

        TextFragment textFragment = new TextFragment(text);                 
        return textFragment;
}

private static FloatingBox createFloatingBox2(TextFragment textFragment) {
    FloatingBox box = new FloatingBox();
    box.setTop(200.0);
    box.setLeft(200.0);
    box.setHeight(100.0);
    box.setWidth(100.0);

    box.setHorizontalAlignment(HorizontalAlignment.Left);
    box.setVerticalAlignment(VerticalAlignment.Top);

    textFragment.setMargin(new MarginInfo(0, 0, 0, 0));
    box.getParagraphs().add(textFragment);
    return box;
}

testAnnotationen_output.pdf (88.0 KB)

@igor.berchtold

It seems like the issue is related to following lines of code:

box.setHeight(100.0);
box.setWidth(100.0);

The height/width of the box seems to be smaller than the content size. Please try to remove these lines and let us know in case you face some issue.

Hi,
thank you for the response. Actually our requirements are that the box is fix in size but the contents can be larger and so the contents which does not have space should not be shown, is this possible to do?

best regards,

Igor

@igor.berchtold

In your first post, you mentioned that you wanted to add a text box. From your shared code snippet, we assumed that you are referring to a floating box as a text box. Would you however confirm if you only want to use a floating box or your requirements can also be met by using an actual Form Field (TextBox)? We will further proceed to assist you accordingly.

@asad.ali
I tried the TextBoxField and the result looks good to me, I can set with Rectangle the exact position of the TextBoxField. Also I can set readOnly(true), scrollable(false), multiline(true), set a border and color and also a BackgroundColor. For the Text I can set a color and I can set a FontName and FontSize.

But what I miss is to set a FontStyle like Italic or Bold and Text Underline or Text Strikethrough, this is missing which actually I do have using TextFragment.

But with the TextFragment I can not place the Text on Page on the correct position and size or at least I do not know how. TextFragemnt adapts to the text I put inside and does not clip it.

so the question is can I set to the TextBoxField the FontStyles?

best regards,

Igor

@igor.berchtold

You may please try following example if it helps achieving your requirements. Please let us know in case you need further information.

Hi @asad.ali,

I had a look, but I was not able to find the functionality for setting the FontStyle like Italic or to underline / strikethrough the text in a TextBoxField. Using FormFieldFacade I was not able to find this needed functionality. Maybe some clarification would help me further.

best regards,
Igor

@igor.berchtold

Using FormFieldFacade, you can set custom font as well as its size. However, setting font style as bold or italic is not supported and we need to investigate whether it is possible or not as per Adobe Standards.

Furthermore, we have logged an investigation ticket as PDFJAVA-39957 in our issue tracking system. We will further check it in details and investigate how your actual requirements can be met using the API. As soon as the ticket is resolved, we will update you within this forum thread. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hi,
thank you for the response. So I will wait for the solution of the ticket.

thank you

@igor.berchtold

Please use 21.9 version of the API and the following option to truncate the contents of the Floating box, as per box size:

box.setNeedRepeating(false); 

Hi Ali, do you have any news about the PDFJAVA-39957 ? I still search for a possibility to use a TextBoxField and able to set Italic, Bold or Underline for the text.
best regards,

Igor

@igor.berchtold

We are afraid that the feasibility to implement font styles in TextBox fields could not get investigated. We need to perform a detailed investigation for this requirement. Could you please share a sample PDF that already has such styles in the textbox fields? We will create a new ticket along with the file to investigate this feature further.

annotated.pdf (178.3 KB)
On page two you can see such an TextBox we use.
Actually for creating the textBox we use the following code:
double llx = annotationen.getLeft() * 72 + getCorrMarginLeft();
double lly = hight - annotationen.getTop() * 72 - annotationen.getHeight() * 72 - getCorrHeight();
double urx = annotationen.getLeft() * 72 + (annotationen.getWidth() * 72) + getCorrWidth();
double ury = hight - annotationen.getTop() * 72 + getCorrMarginBottom();
Rectangle r = new Rectangle(llx, lly, urx, ury);

        Matrix matrix = pdfPage.getRotationMatrix();

        if (rotateFactor == 3) {
            r = new Rectangle(lly, matrix.getE()- llx, lly+r.getHeight(), matrix.getE() - llx-r.getWidth());
        }

        TextBoxField textBoxField1 = new TextBoxField(pdfPage, r);

        if (rotateFactor > 0) {
            textBoxField1.getCharacteristics().setRotate(rotateFactor);
        }

        textBoxField1.setValue(withQuotes);
        DefaultAppearance defaultAppearance = new DefaultAppearance();
        defaultAppearance.setFontName(getFontName(annotationen.getFontName()));
        defaultAppearance.setFontSize(annotationen.getFontSize());
        defaultAppearance.setTextColor(annotationen.getForecolor());
        
        textBoxField1.setDefaultAppearance(defaultAppearance);
        textBoxField1.setReadOnly(true);
        textBoxField1.setScrollable(false);
        textBoxField1.setMultiline(true);
        textBoxField1.setTextVerticalAlignment(VerticalAlignment.Top);
        textBoxField1.setHorizontalAlignment(HorizontalAlignment.Left);
        if (annotationen.getHasborder().equals(Boolean.TRUE)) {
            Border border = new Border(textBoxField1);
            border.setWidth(annotationen.getBorderWidth());
            border.setStyle(BorderStyle.Solid);
            textBoxField1.setBorder(border);
            textBoxField1.getCharacteristics().setBorder(Color.fromRgb(annotationen.getBorderColor()));
        }
        configure(textBoxField1, annotationen);
        return textBoxField1;

@igor.berchtold

Another ticket as PDFJAVA-41365 has been opened in our issue tracking system to investigate the provided file and required functionality. We will inform you as soon as it is resolved. Please spare us some time.

We are sorry for the inconvenience.

@igor.berchtold

TextBoxField doesn’t support formatting functionality. RichTextBoxField should be used instead.

Example:

Document doc = new Document();
doc.getPages().add();

RichTextBoxField rt = new RichTextBoxField(doc.getPages().get_Item(1), new Rectangle(50, 700, 200, 800));
rt.getCharacteristics().setBackground(Color.getCyan());

DefaultAppearance defaultAppearance = new DefaultAppearance();
rt.setDefaultAppearance(defaultAppearance);
rt.setReadOnly(true);
rt.setScrollable(false);
rt.setMultiline(true);
rt.setTextVerticalAlignment(VerticalAlignment.Top);
rt.setHorizontalAlignment(HorizontalAlignment.Left);

rt.setPartialName("rt");
rt.setRichTextValue("<?xml version=\"1.0\"?>\n" +
                "<body xfa:APIVersion=\"Acroform:2.7.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">\n" +
                "<p dir=\"ltr\" style=\"margin-top:0pt;margin-bottom:8pt;line-height:12.95pt;font-family:Calibri;font-size:11pt;font-style:italic\">Italic\n" +
                "<span style=\"font-style:normal\"> and </span>\n" +
                "<span style=\"font-weight:bold;font-style:normal\">Bold\n</span>\n" +
                "<span style=\"color:#8866ff;font-weight:bold;font-style:normal;text-decoration:underline\">Bold Underlined\n</span>" +
                "<span style=\"font-weight:bold;font-style:normal;text-decoration:line-through\">Bold Strikethrough\n</span>\n" +
                "</p>\n" +
                "</body>");
rt.setMultiline(true);
doc.getForm().add(rt);
doc.save(dataDir+"out_RichTextBoxField_example.pdf"); 

out_RichTextBoxField_example.pdf (11.7 KB)