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);
}
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?
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?
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.
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.
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,
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;
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.