Free Support Forum - aspose.com

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