We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

TextBox setFitIntoRectangle

Hi,


I would like to to turn off automatic changing of font size of a text (value) in a <span style=“background-color: rgb(228, 228, 255); font-family: “Courier New”;”>TextBoxField. I found method <span style=“background-color: rgb(228, 228, 255); font-family: “Courier New”;”>setFitIntoRectangle , but changign the value using this method does change the behaviour.

Could somebody kindly give me a hint, how this should work.
Thanks.

Hi,


Thanks for contacting support.

Can you please share some further details regarding the issue which you are facing i.e. the workflow in which you are observing font size change issue and also please share the code snippet and resultant PDF file, which can help us in understanding and replicating the issue in our environment. We are sorry for this inconvenience.
Thanks for the reply ...

Here is the code. Please note, that it is the code form your pdf-java-examples projects (just not using the input.pdf, but creating new one and adding page):

public static void addFormFieldInPDFDocument() {
// Open a document
Document pdfDocument = new Document();
pdfDocument.getPages().add();
// Create a field
TextBoxField textBoxField1 = new TextBoxField(pdfDocument.getPages().get_Item(1), new Rectangle(100, 200, 300, 300));
// Set the field name
textBoxField1.setPartialName("textbox1");
// Set the field value
textBoxField1.setValue("Text Box");
// Create a border object
Border border = new Border(textBoxField1);
// Set the border width
border.setWidth(5);
// Set the border dash style
border.setDash(new Dash(1, 1));
// Set the field border
textBoxField1.setBorder(border);
// Add the field to the document
pdfDocument.getForm().add(textBoxField1, 1);
// Save the modified PDF
pdfDocument.save("outputTextBoxField.pdf");
}

I am using apose SDK 17.2.

The problem is that that, when the the text in textbox field does not fit anymore to the textbox field (user types the text), it changes its size (its getting smaller and smaller). The questions are:

How do I prevent the text to change the font size and let it scroll horizontally when it does not fit anymore (setScrollable, setFitIntoRectangle does not work for me ... maybe I am using it wrong ... please example)?

How do I let the text scroll vertically when it does not fit anymore?

How do I let the textbox change its height (or width) when it does not fit anymore?

One more question regarding the textbox. Why I can set border as integer only? The border is to thick to my liking, when I use 1. (Your tables let me set border of a table or cell as float.) Or is there any other way?

Btw. note that in the example you provide you set some border, but no border is rendered. The getCharacteristics().setBorder(java.awt.Color.BLACK); is missing in the example!





opeterka:
Thanks for the reply …

Here is the code. Please note, that it is the code form your pdf-java-examples projects (just not using the input.pdf, but creating new one and adding page):

public static void addFormFieldInPDFDocument() {
// Open a document
Document pdfDocument = new Document();
pdfDocument.getPages().add();
// Create a field
TextBoxField textBoxField1 = new TextBoxField(pdfDocument.getPages().get_Item(1), new Rectangle(100, 200, 300, 300));
// Set the field name
textBoxField1.setPartialName(“textbox1”);
// Set the field value
textBoxField1.setValue(“Text Box”);
// Create a border object
Border border = new Border(textBoxField1);
// Set the border width
border.setWidth(5);
// Set the border dash style
border.setDash(new Dash(1, 1));
// Set the field border
textBoxField1.setBorder(border);
// Add the field to the document
pdfDocument.getForm().add(textBoxField1, 1);
// Save the modified PDF
pdfDocument.save(“outputTextBoxField.pdf”);
}

I am using apose SDK 17.2.

The problem is that that, when the the text in textbox field does not fit anymore to the textbox field (user types the text), it changes its size (its getting smaller and smaller).
Hi,

Thanks for sharing the details.

I have tested the scenario and have managed to reproduce the same problem. Please note that its a default behavior when displaying the PDF file and the field tries to accommodate the complete content inside it. However for the sake of further investigation, I have logged it as PDFJAVA-36739 in our issue tracking system. We will further look into the details of this problem and will keep you updated with our findings. We are sorry for this inconvenience.

Hi.


ok if this is the defualt behaviour, how can it be changed? Please see my questions in the previous post. I have not witnessed this behaviour in any pdf so far. In any case, the customer will not like this and I need to change it (by showing scrollbar for multiline, by auto changing the heigth of the field etc.

Thanks.

opeterka:
Hi.

ok if this is the defualt behaviour, how can it be changed? Please see my questions in the previous post. I have not witnessed this behaviour in any pdf so far. In any case, the customer will not like this and I need to change it (by showing scrollbar for multiline, by auto changing the heigth of the field etc.
Hi,

Thanks for sharing the details.

In order to prevent the TextField from changing the font size, please, set the font size:
textBoxField1.getDefaultAppearance().setFontSize(20);

And in case you want the text to scroll vertically when it does not fit anymore, in addition to option above, please use the option:
textBoxField1.setMultiline(true);

Furthermore, please note that the textbox can not change its height (or width) in standard PDF form. For this purpose, dynamic forms (XFA) should be used.

opeterka:
One more question regarding the textbox. Why I can set border as integer only? The border is to thick to my liking, when I use 1. (Your tables let me set border of a table or cell as float.) Or is there any other way?
Hi,

Thanks for sharing the details.

I am afraid the above stated feature is currently not supported. For the sake of correction, I have logged it as PDFJAVA-36746 in our issue tracking system. We will further look into the details of this requirement and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

opeterka:
Btw. note that in the example you provide you set some border, but no border is rendered. The getCharacteristics().setBorder(java.awt.Color.BLACK); is missing in the example!
During my testing, I am unable to notice any issue and color information for TextBoxField is properly being set. For your reference, I have also attached the output generated using following code snippet.

[Java]

// Open a document<o:p></o:p>

com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document();

pdfDocument.getPages().add();

// Create a field

TextBoxField textBoxField1 = new TextBoxField(pdfDocument.getPages().get_Item(1), new Rectangle(100, 200, 300, 300));

// Set the field name

textBoxField1.setPartialName("textbox1");

// Set the field value

textBoxField1.setValue("Text Box");

textBoxField1.getDefaultAppearance().setFontSize(20);

textBoxField1.setMultiline(true);

// Create a border object

com.aspose.pdf.Border border = new com.aspose.pdf.Border(textBoxField1);

// Set the border width

border.setWidth(5);

// Set the border dash style

border.setDash(new Dash(1, 1));

textBoxField1.getCharacteristics().setBorder(java.awt.Color.BLUE);

// Set the field border

textBoxField1.setBorder(border);

// Add the field to the document

pdfDocument.getForm().add(textBoxField1, 1);

// Save the modified PDF

pdfDocument.save("c:/pdftest/outputTextBoxField.pdf");


Thanks.

The proposed solution does not work when the textfield is in the table:

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”> private void addTable(Page pdfPage) {
Table tab = new Table();
TextState textState = new TextState(fontName);
textState.setFontSize(fontSizeTable);
tab.setDefaultCellTextState(textState);
int poznamkaWidth = 165;
//Set with column widths of the table
tab.setColumnWidths("70 115 60 80 " + poznamkaWidth);
//tab.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);
//Set default cell border using BorderInfo object
tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));
com.aspose.pdf.MarginInfo marginTable = new com.aspose.pdf.MarginInfo(0,0,0,16);
tab.setMargin(marginTable);
//Set table border using another customized BorderInfo object
tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));
//Create MarginInfo object and set its left, bottom, right and top margins
com.aspose.pdf.MarginInfo marginInfoCell = new com.aspose.pdf.MarginInfo(2,2,2,2);
//Set the default cell padding to the MarginInfo object
tab.setDefaultCellPadding(marginInfoCell);
//Create rows in the table and then cells in the rows
com.aspose.pdf.Row row1 = tab.getRows().add();
row1.setFixedRowHeight(rowHeight);
addCellToRow(row1, “A”, FontStyles.Bold, HorizontalAlignment.Center);
addCellToRow(row1, “B”, FontStyles.Bold, HorizontalAlignment.Center);
addCellToRow(row1, “Číslo”, FontStyles.Bold, HorizontalAlignment.Center);
addCellToRow(row1, “D”, FontStyles.Bold, HorizontalAlignment.Center);
addCellToRow(row1, “Poznámka”, FontStyles.Bold, HorizontalAlignment.Center);
;
for (int i = 0; i< numberOfRows; i++)
{
com.aspose.pdf.Row row2 = tab.getRows().add();
row2.setFixedRowHeight(rowHeight);
Cell c = row2.getCells().add(“a111-5454”);
row2.getCells().add(“Mudr. Jan Novák Maximilan Superman” + i);
row2.getCells().add(“c” + i);
row2.getCells().add().getParagraphs().add(createRadioTable(pdfPage, i));
TextBoxField tbf = new TextBoxField();
tbf.setMargin(new MarginInfo(0,4,0,0));
// DefaultAppearance tbfDefaultAppearance = new DefaultAppearance();
// tbfDefaultAppearance.setFontSize(fontSizeTable);
// tbf.setDefaultAppearance(tbfDefaultAppearance);
tbf.setPartialName(“poznamka_” + i);
tbf.setWidth(poznamkaWidth);
tbf.setHeight(rowHeight);
tbf.setMultiline(true);
row2.getCells().add().getParagraphs().add(tbf);
tbf.getDefaultAppearance().setFontSize(fontSizeTable); //<–exception!!!
}
//Add the table in paragraphs collection of the desired section
pdfPage.getParagraphs().add(tab);
//tbf.getDefaultAppearance().setFontSize(fontSizeTable);
}

NullPointerException is thrown when getting default appearance. How can i go around this? I guess, it is because no page or document is set to the textboxfield, but I can not figure how to circumvent the problem.

In regard of the example for border. In your example on github there is missing the line textBoxField1.getCharacteristics().setBorder(java.awt.Color.BLUE); and that is the problem why it does not work correctly … just letting you know, that’s all.

Hi,


Thanks for sharing the details.

I have tried replicating the issue but I am afraid the addCellToRow(…) and createRadioTable(…) methods are not defined. Can you please share the complete code snippet, so that we can again try replicating the issue in our environment. We are sorry for this inconvenience.

[Java]

com.aspose.pdf.Document doc = new Document();<o:p></o:p>

doc.getPages().add();

com.aspose.pdf.Table tab = new com.aspose.pdf.Table();

TextState textState = new TextState("Arial");

textState.setFontSize(14);

tab.setDefaultCellTextState(textState);

int poznamkaWidth = 165;

//Set with column widths of the table

tab.setColumnWidths("70 115 60 80 " + poznamkaWidth);

//tab.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);

//Set default cell border using BorderInfo object

tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));

com.aspose.pdf.MarginInfo marginTable = new com.aspose.pdf.MarginInfo(0,0,0,16);

tab.setMargin(marginTable);

//Set table border using another customized BorderInfo object

tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));

//Create MarginInfo object and set its left, bottom, right and top margins

com.aspose.pdf.MarginInfo marginInfoCell = new com.aspose.pdf.MarginInfo(2,2,2,2);

//Set the default cell padding to the MarginInfo object

tab.setDefaultCellPadding(marginInfoCell);

//Create rows in the table and then cells in the rows

com.aspose.pdf.Row row1 = tab.getRows().add();

row1.setFixedRowHeight(10);

addCellToRow(row1, "A", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "B", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "Číslo", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "D", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "Poznámka", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

;

for (int i = 0; i< 20; i++)

{

com.aspose.pdf.Row row2 = tab.getRows().add();

row2.setFixedRowHeight(10);

com.aspose.pdf.Cell c = row2.getCells().add("a111-5454");

row2.getCells().add("Mudr. Jan Novák Maximilan Superman" + i);

row2.getCells().add("c" + i);

row2.getCells().add().getParagraphs().add(createRadioTable(pdfPage, i));

TextBoxField tbf = new TextBoxField();

tbf.setMargin(new MarginInfo(0,4,0,0));

// DefaultAppearance tbfDefaultAppearance = new DefaultAppearance();

// tbfDefaultAppearance.setFontSize(fontSizeTable);

// tbf.setDefaultAppearance(tbfDefaultAppearance);

tbf.setPartialName("poznamka_" + i);

tbf.setWidth(poznamkaWidth);

tbf.setHeight(10);

tbf.setMultiline(true);

row2.getCells().add().getParagraphs().add(tbf);

tbf.getDefaultAppearance().setFontSize(14); //<--exception!!!

}

//Add the table in paragraphs collection of the desired section

doc.getPages().get_Item(1).getParagraphs().add(tab);

//tbf.getDefaultAppearance().setFontSize(fontSizeTable);

doc.save(“c:/pdftest/Table_in_PDF.pdf”);

opeterka:
In regard of the example for border. In your example on github there is missing the line textBoxField1.getCharacteristics().setBorder(java.awt.Color.BLUE); and that is the problem why it does not work correctly ... just letting you know, that's all.
Hi,

Can you please share the example link, so that we can update the information accordingly.

Hi,

here it is:

private void addCellToRow(Row row, String text, int fontStyle, int horizontalAlignment) {
TextFragment tf = new TextFragment(text);
tf.getTextState().setFont(FontRepository.findFont(fontName));
tf.getTextState().setFontStyle(fontStyle);
tf.getTextState().setHorizontalAlignment(horizontalAlignment);
row.getCells().add().getParagraphs().add(tf);
}

private Table createRadioTable(Page page, int rowNumber) {
Table table = new Table();
table.setColumnWidths(“35 35”);
Row r1 = table.getRows().add();
Cell c1 = r1.getCells().add();
Cell c2 = r1.getCells().add();

table.setMargin(new MarginInfo(4,0,6,0));

RadioButtonField rf = new RadioButtonField(page);
rf.setName(“radio” + rowNumber);
page.getDocument().getForm().add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();

opt1.setOptionName(“rbAno_” +rowNumber);
opt2.setOptionName(“rbNe_” +rowNumber);

opt1.setWidth(8);
opt1.setHeight(8);
opt2.setWidth(8);
opt2.setHeight(8);

rf.add(opt1);
rf.add(opt2);

TextFragment anoTf = new TextFragment(“Ano”);
anoTf.getTextState().setFont(FontRepository.findFont(fontName));
[//anoTf.getTextState](https://anotf.gettextstate/)().setFont(FontRepository.findFont(fontName));
anoTf.getTextState().setFontSize(8);
anoTf.getTextState().setFontStyle(FontStyles.Regular);

TextFragment neTf = new TextFragment(“Ne”);
neTf.getTextState().setFont(FontRepository.findFont(fontName));
neTf.getTextState().setFontSize(8);
neTf.getTextState().setFontStyle(FontStyles.Regular);

opt1.setCaption(anoTf);
opt2.setCaption(neTf);

c1.getParagraphs().add(opt1);
c2.getParagraphs().add(opt2);

return table;
}

Thanks

Hi,


Thanks for sharing the details.

During an initial attempt, the code executed without any exception but I am afraid the resultant file is not being generated. We are further investigating the scenario and will keep you updated with our findings.

[Java]

com.aspose.pdf.Document doc = new Document();<o:p></o:p>

doc.getPages().add();

com.aspose.pdf.Table tab = new com.aspose.pdf.Table();

TextState textState = new TextState("Arial");

textState.setFontSize(14);

tab.setDefaultCellTextState(textState);

int poznamkaWidth = 165;

//Set with column widths of the table

tab.setColumnWidths("70 115 60 80 " + poznamkaWidth);

//tab.setColumnAdjustment(ColumnAdjustment.AutoFitToContent);

//Set default cell border using BorderInfo object

tab.setDefaultCellBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));

com.aspose.pdf.MarginInfo marginTable = new com.aspose.pdf.MarginInfo(0,0,0,16);

tab.setMargin(marginTable);

//Set table border using another customized BorderInfo object

tab.setBorder(new com.aspose.pdf.BorderInfo(com.aspose.pdf.BorderSide.All, 0.5F));

//Create MarginInfo object and set its left, bottom, right and top margins

com.aspose.pdf.MarginInfo marginInfoCell = new com.aspose.pdf.MarginInfo(2,2,2,2);

//Set the default cell padding to the MarginInfo object

tab.setDefaultCellPadding(marginInfoCell);

//Create rows in the table and then cells in the rows

com.aspose.pdf.Row row1 = tab.getRows().add();

row1.setFixedRowHeight(10);

addCellToRow(row1, "A", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "B", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "Číslo", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "D", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

addCellToRow(row1, "Poznámka", FontStyles.Bold, com.aspose.pdf.HorizontalAlignment.Center);

;

for (int i = 0; i< 20; i++)

{

com.aspose.pdf.Row row2 = tab.getRows().add();

row2.setFixedRowHeight(10);

com.aspose.pdf.Cell c = row2.getCells().add("a111-5454");

row2.getCells().add("Mudr. Jan Novák Maximilan Superman" + i);

row2.getCells().add("c" + i);

row2.getCells().add().getParagraphs().add(createRadioTable(doc.getPages().get_Item(1), i));

TextBoxField tbf = new TextBoxField();

tbf.setMargin(new MarginInfo(0,4,0,0));

// DefaultAppearance tbfDefaultAppearance = new DefaultAppearance();

// tbfDefaultAppearance.setFontSize(fontSizeTable);

// tbf.setDefaultAppearance(tbfDefaultAppearance);

tbf.setPartialName("poznamka_" + i);

tbf.setWidth(poznamkaWidth);

tbf.setHeight(10);

tbf.setMultiline(true);

row2.getCells().add().getParagraphs().add(tbf);

tbf.getDefaultAppearance().setFontSize(14); //<--exception!!!

}

//Add the table in paragraphs collection of the desired section

doc.getPages().get_Item(1).getParagraphs().add(tab);

//tbf.getDefaultAppearance().setFontSize(fontSizeTable);

doc.save("c:/pdftest/Table_in_PDF_2.pdf");

private static void addCellToRow(com.aspose.pdf.Row row, String text, int fontStyle, int horizontalAlignment) {

TextFragment tf = new TextFragment(text);

tf.getTextState().setFont(FontRepository.findFont("Arial"));

tf.getTextState().setFontStyle(fontStyle);

tf.getTextState().setHorizontalAlignment(horizontalAlignment);

row.getCells().add().getParagraphs().add(tf);

}

private static com.aspose.pdf.Table createRadioTable(Page page, int rowNumber) {

com.aspose.pdf.Table table = new com.aspose.pdf.Table();

table.setColumnWidths("35 35");

com.aspose.pdf.Row r1 = table.getRows().add();

com.aspose.pdf.Cell c1 = r1.getCells().add();

com.aspose.pdf.Cell c2 = r1.getCells().add();

table.setMargin(new MarginInfo(4,0,6,0));

RadioButtonField rf = new RadioButtonField(page);

rf.setName("radio" + rowNumber);

page.getDocument().getForm().add(rf, 1);

RadioButtonOptionField opt1 = new RadioButtonOptionField();

RadioButtonOptionField opt2 = new RadioButtonOptionField();

opt1.setOptionName("rbAno_" +rowNumber);

opt2.setOptionName("rbNe_" +rowNumber);

opt1.setWidth(8);

opt1.setHeight(8);

opt2.setWidth(8);

opt2.setHeight(8);

rf.add(opt1);

rf.add(opt2);

TextFragment anoTf = new TextFragment("Ano");

anoTf.getTextState().setFont(FontRepository.findFont("Arial"));

//anoTf.getTextState().setFont(FontRepository.findFont(fontName));

anoTf.getTextState().setFontSize(8);

anoTf.getTextState().setFontStyle(FontStyles.Regular);

TextFragment neTf = new TextFragment("Ne");

neTf.getTextState().setFont(FontRepository.findFont("Arial"));

neTf.getTextState().setFontSize(8);

neTf.getTextState().setFontStyle(FontStyles.Regular);

opt1.setCaption(anoTf);

opt2.setCaption(neTf);

c1.getParagraphs().add(opt1);

c2.getParagraphs().add(opt2);

return table;

}

opeterka:
NullPointerException is thrown when getting default appearance. How can i go around this? I guess, it is because no page or document is set to the textboxfield, but I can not figure how to circumvent the problem.
Hi,

Thanks for using our API’s.

I have tested the scenario and have managed to reproduce above stated problem. For the sake of correction, I have logged it as PDFJAVA-36753 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.