问题
I have a problem with using Itext pdf. The problem is, after an option is selected and the pointer is focused on another element of pdf, the text in TextField is somehow cut off or some letters are floating over. What would be the solution? The image that shows what's going on

@Override
public void writePdfElement(RankingQuestionDTO input, Document document, PdfWriter writer, PdfPTable baseTable) {
try{
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
document.add(new Paragraph(input.getText(), stylesService.getHeaderFont()));
//Add rows with selectors
PdfFormField selectGroup = PdfFormField.createTextField(writer, true, false, 10);
selectGroup.setFieldName(String.format("%s", input.getUuid()));
ArrayList<RankingAnswerDTO> possibleAnswers = input.getPossibleAnswers();
for(int i = 0; i <input.getPossibleAnswers().size(); i++) {
cell = new PdfPCell();
cell.setPhrase(getPolishTablePhrase(input.getText()));
cell.setPadding(stylesService.getPaddingCell());
table.addCell(cell);
cell = new PdfPCell();
cell.setPadding(stylesService.getPaddingCell());
cell.setCellEvent(new SelectCellEvent(String.format("%s",i), selectGroup, writer, stylesService,
possibleAnswers));
cell.setMinimumHeight(stylesService.getMinimumHeight());
table.addCell(cell);
}
baseTable.addCell(table);
document.add(baseTable);
writer.addAnnotation(selectGroup);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
Rectangle rect = stylesService.getSelectFiledRectangle(position, sizeOfRect);
// define the select box
TextField tf = new TextField(writer, rect, name);
try {
tf.setFont(BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, true));
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
tf.setBackgroundColor(stylesService.getBackgroundColor());
tf.setBorderColor(stylesService.getBorderColor());
tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
tf.setBorderColor(BaseColor.GRAY);
tf.setBorderWidth(stylesService.getFloatBorderWidth());
tf.setFontSize(stylesService.getFieldFloatFont());
tf.setChoices(select);
tf.setChoiceExports(ranks);
tf.setAlignment(Element.ALIGN_CENTER);
tf.setOptions(TextField.MULTILINE);
tf.setRotation(0);
// add the select box as a field
try {
selectGroup.addKid(tf.getComboField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
The problem was with font setting. Following approach works fine
private void initializeBaseFont() {
try {
baseFont = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
}
Instead of
tf.setFont(BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, true));
回答1:
Please take a look at the ComboBoxItems example:
This is the cell event implementation:
class SelectCellEvent implements PdfPCellEvent {
protected PdfFormField selectGroup;
protected String name;
protected String[] exports;
protected String[] options;
protected BaseFont font;
public SelectCellEvent(PdfFormField selectGroup, String name, String[] exports, String[] options) throws DocumentException, IOException {
this.selectGroup = selectGroup;
this.name = name;
this.exports = exports;
this.options = options;
font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
font.setSubset(false);
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
TextField tf = new TextField(writer, position, name);
tf.setFont(font);
tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
tf.setBorderColor(BaseColor.GRAY);
tf.setChoiceExports(exports);
tf.setChoices(options);
tf.setAlignment(Element.ALIGN_CENTER);
try {
selectGroup.addKid(tf.getComboField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
This is the PDF creation process:
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle pagesize = PageSize.LETTER;
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
//Add rows with selectors
PdfFormField selectGroup = PdfFormField.createEmpty(writer);
selectGroup.setFieldName("myCombos");
String[] options = {"Choose first option", "Choose second option", "Choose third option"};
String[] exports = {"option1", "option2", "option3"};
table.addCell("Combobox:");
cell = new PdfPCell();
cell.setCellEvent(new SelectCellEvent(selectGroup, "combo1", exports, options));
cell.setMinimumHeight(20);
table.addCell(cell);
document.add(table);
writer.addAnnotation(selectGroup);
document.close();
}
There are a number of things I don't understand in your code.
What is selectGroup?
It seems that you have several combo boxes (in my example I have only one) that are kids of a text field named selectGroup
. Why is selectGroup
a text field? I don't see you defining its dimensions anywhere?
I have made the assumption that you wanted to create a parent field, e.g. myCombos
and then a number of kids combo1
, combo2
,... so that you have myCombos.combo1
, myCombos.combo2
, etc...
If that is the case, please use the createEmpty()
method instead of the createTextField()
method.
Why do you want to embed the font?
This doesn't make sense:
BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, true)
You are using BaseFont.TIMES_ROMAN
which is a Standard Type 1 font. Standard Type 1 fonts are never embedded by iText, because they are supposed to be present in every reader. Hence the true
parameter for the createFont()
method will be ignored.
What causes the problem?
When you define the font, iText will only use a very small part of the font data. More specifically: only the info that is needed to create the appearance of the characters that are used "Choose first option". The "c" and "d" are missing in this case. Hence, when Adobe Reader has to render the word second, it will have problems with those missing characters.
You can avoid this by adding:
font.setSubset(false);
Or by using a completely different font.
来源:https://stackoverflow.com/questions/28236902/itext-combobox-width-of-selected-option-issue