- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用 Itext pdf 时遇到问题。问题是,选择一个选项并将指针聚焦在 pdf 的另一个元素上后,TextField 中的文本会以某种方式被切断或某些字母浮在上面。解决办法是什么?显示正在发生的情况的图像 代码块是
@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);
}
}
问题出在字体设置上。以下方法效果很好
private void initializeBaseFont() {
try {
baseFont = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
}
而不是
tf.setFont(BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, true));
最佳答案
请查看 ComboBoxItems示例:
这是单元格事件实现:
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);
}
}
}
这是 PDF 创建过程:
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();
}
您的代码中有很多我不明白的地方。
什么是 selectGroup?
您似乎有几个组合框(在我的示例中只有一个),它们是名为 selectGroup
的文本字段的子项。为什么 selectGroup
是一个文本字段?我没有看到你在任何地方定义它的尺寸?
我假设您想要创建一个父字段,例如myCombos
,然后是一些子 combo1
、combo2
...,这样您就有 myCombos.combo1
、myCombos.combo2
等...
如果是这种情况,请使用 createEmpty()
方法而不是 createTextField()
方法。
为什么要嵌入字体?
这没有意义:
BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, true)
您使用的是 BaseFont.TIMES_ROMAN
,它是标准 Type 1 字体。标准 Type 1 字体永远不会被 iText 嵌入,因为它们应该存在于每个阅读器中。因此,createFont()
方法的 true
参数将被忽略。
问题的原因是什么?
当你定义字体时,iText只会使用字体数据的很小一部分。更具体地说:只有创建“选择第一个选项”所使用的角色外观所需的信息。在本例中缺少“c”和“d”。因此,当 Adobe Reader 必须渲染第二个单词时,它会遇到那些丢失字符的问题。
您可以通过添加以下内容来避免这种情况:
font.setSubset(false);
或者使用完全不同的字体。
关于java - Itext组合框,所选选项的宽度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28236902/
iText 文档指出它只包含特定的字体子集,但从未说明它们是什么。有没有人知道 iText 中默认包含哪些字体? (我在网上搜索过,在任何地方都找不到这个字体列表!) 最佳答案 它可能指的是PDF S
我使用固定列宽创建了下表,如下所示, Table headerTable = new Table(new float[]{5,5,5}); headerTable.setWidthPercent(
我正在尝试使用 iText 缩放图像(在新的 PDF 文档上)以使其填充页面宽度而不拉伸(stretch),这样它可能需要几页。 我找到了很多解决方案,但它们都非常复杂,而且我真的不喜欢那样编码。到目
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
如何使用 iText 找到文档中的行的位置? 假设我有一个 PDF 文档中的表格,并且想要阅读其中的内容;我想找到细胞的确切位置。为了做到这一点,我想我可能会找到线条的交点。 最佳答案 我认为您使用
请找到下面的代码。 public class MakingFieldReadOnly implements PdfPCellEvent { /** The resulting PDF. */
在 iText 5 中有一个名为 getVerticalPosition() 的方法,它给出了下一个写入对象在页面上的位置。作为回答这个问题 How to find out the current c
抱歉,如果有类似我的帖子,但我是这个论坛的新手,我还没有找到它。 我有动态调整 TextField 大小取决于文本大小的问题。我填写现有的 PDF - 在 AcroForm 中填写字段: form.s
我正在使用 itext 生成 pdf。因此,当页面内容超出时,它会自动创建一个新页面。我想知道它是否创建了一个新页面。如果是,我想在页面顶部添加一些图像。 List paylist =new List
我的 有问题固定 表格左侧和右侧的边距。 我想删除该边距并使用没有边距或填充的所有工作表。我该怎么办? 我刚刚试过这个,但对我不起作用: cell.setPaddingLeft(0); cell.se
我有 2 行,我想对齐(证明)它们。 我有这个代码: Paragraph p=new Paragraph(ANC,fontFootData); p.setLeading(1, 1);
我有这样的场景。 我有一个生成 PDF 的应用程序,需要对其进行签名。 我们没有用于签署文档的证书,因为它们位于 HSM 中,而我们使用证书的唯一方法是使用 Web 服务。 此网络服务提供两个选项,发
我需要实现一个看起来像图片中的表格,列之间有空间。我试过: cell.setPaddingLeft(10); cell.setMarginLeft(10); extractio
我需要实现一个看起来像图片中的表格,列之间有空间。我试过: cell.setPaddingLeft(10); cell.setMarginLeft(10); extractio
我需要使用 Java 的 iText 库创建一个 PDF 文档。我还需要包括一些复选框,这些复选框根据某些类变量的值打开/关闭。我找到了一些关于交互式表单的示例,但我不需要这种复杂程度:只需将一些复选
我正在开发一个系统,我必须在其中将一些图像添加到现有的 PDF 文档中。 这适用于 iText 5.1.3,但由于某种原因,在包含扫描图像的 PDF 中,它不会添加任何图像。 这是 PDF Docum
我们正在研究信息提取,我们想使用iText。 我们正在探索 iText。根据我们查阅过的文献,iText 是最好的工具。是否可以从 iText 中每行的 pdf 中提取文本?我在与我的相关的 stac
我已经创建了一个带有一些文本字段的 pdf 文档。我可以使用 Adobe 阅读器填充这些文本字段并将这些值保存在该文件中。 我的问题是,我可以使用 iText 以编程方式执行此操作吗?如果可能,请
我正在使用 iText 5 表创建标签(如 Avery 标签)。标签元素的定位需要一些非常严格的公差,以便适合标签上的所有内容。我的问题是标签上有多个区域为 PdfPCells。我需要将文本放入这些区
我是一名优秀的程序员,十分优秀!