当我在文档中添加 PdfPCell 时,Phrase 在 PdfPCell 中不起作用
Phrase doesn't work in PdfPCell when I add PdfPCell in document
我从原始 PDF 创建了一个新的 PDF。
我尝试在 PdfPCell 中添加 Phrase。图书馆说,当你把文本放在 PdfPCell 中时,你必须使用 Phrase。
我需要这样做,因为我需要一个位置和一个单元格中的文本。我有这个代码。
mPdfFileOutPut = new File(Environment.getExternalStorageDirectory(),
outPutFileStringBuilder.toString());
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(mPdfFileOutPut));
document.open();
FileInputStream is = new FileInputStream(mPdfFile);
// Create a reader to extract info
PdfReader pdfReader = new PdfReader(is);
// Get the fields from the reader (read-only!!!)
AcroFields form = pdfReader.getAcroFields();
Set<String> fields = form.getFields().keySet();
PdfPCell cell = new PdfPCell( new Phrase("string"));
for (String key : fields) {
cell.setBackgroundColor(BaseColor.BLUE);
cell.addElement(new Paragraph("ddfdelellelle"));
cell.setLeft(form.getFieldPositions(key).get(0).position.getLeft());
cell.setRight(form.getFieldPositions(key).get(0).position.getRight());
cell.setTop(form.getFieldPositions(key).get(0).position.getTop());
cell.setBottom(form.getFieldPositions(key).get(0).position.getBottom());
document.add(cell);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
有什么想法吗?希望有解决办法。
如果您查看链接到的 API documentation,您会看到它说:PdfPTable 中的一个单元格。 table 单元格需要是 table.
的一部分
但看起来您根本不想使用 table。如果我正确理解您的代码的意图,您想要创建一个新的 PDF 文档,其文本内容位于与输入 PDF 中的表单字段相同的位置。请注意,您没有考虑多页:如果输入表单的字段超过一页,您将在输出文档的一页上添加所有内容。您也没有考虑页面大小。
看看方便的方法 ColumnText.showTextAligned()
添加一个 Phrase
绝对定位。这是一个示例:http://itextpdf.com/examples/iia.php?id=61
如果您正在尝试制作表单的非交互式版本,请继续阅读表单扁平化。
我从原始 PDF 创建了一个新的 PDF。 我尝试在 PdfPCell 中添加 Phrase。图书馆说,当你把文本放在 PdfPCell 中时,你必须使用 Phrase。
我需要这样做,因为我需要一个位置和一个单元格中的文本。我有这个代码。
mPdfFileOutPut = new File(Environment.getExternalStorageDirectory(),
outPutFileStringBuilder.toString());
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(mPdfFileOutPut));
document.open();
FileInputStream is = new FileInputStream(mPdfFile);
// Create a reader to extract info
PdfReader pdfReader = new PdfReader(is);
// Get the fields from the reader (read-only!!!)
AcroFields form = pdfReader.getAcroFields();
Set<String> fields = form.getFields().keySet();
PdfPCell cell = new PdfPCell( new Phrase("string"));
for (String key : fields) {
cell.setBackgroundColor(BaseColor.BLUE);
cell.addElement(new Paragraph("ddfdelellelle"));
cell.setLeft(form.getFieldPositions(key).get(0).position.getLeft());
cell.setRight(form.getFieldPositions(key).get(0).position.getRight());
cell.setTop(form.getFieldPositions(key).get(0).position.getTop());
cell.setBottom(form.getFieldPositions(key).get(0).position.getBottom());
document.add(cell);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
有什么想法吗?希望有解决办法。
如果您查看链接到的 API documentation,您会看到它说:PdfPTable 中的一个单元格。 table 单元格需要是 table.
的一部分但看起来您根本不想使用 table。如果我正确理解您的代码的意图,您想要创建一个新的 PDF 文档,其文本内容位于与输入 PDF 中的表单字段相同的位置。请注意,您没有考虑多页:如果输入表单的字段超过一页,您将在输出文档的一页上添加所有内容。您也没有考虑页面大小。
看看方便的方法 ColumnText.showTextAligned()
添加一个 Phrase
绝对定位。这是一个示例:http://itextpdf.com/examples/iia.php?id=61
如果您正在尝试制作表单的非交互式版本,请继续阅读表单扁平化。