我正在尝试在我打开的PDF中的特定位置添加一个表格。 我创建了一个表并添加了一些单元格,然后我使用了writeSelectedRows。 当我只使用短语时,此代码有效,但当我使用“MyCellField”添加事件时,它不起作用。
这是我的代码:
PdfReader pdfReader = new PdfReader("/storage/emulated/0/Documents/fiche_chantier.pdf");
//Create time stamp
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "Prevision_");
if (!pdfFolder.exists()) {
pdfFolder.mkdir();
}
File myFile = new File(pdfFolder + timeStamp + ".pdf");
OutputStream output = new FileOutputStream(myFile);
PdfStamper pdfStamper = new PdfStamper(pdfReader, output);
PdfContentByte canvas = pdfStamper.getOverContent(1);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(new float[]{PageSize.A5.getWidth()});
table.setWidthPercentage(100.0f);
table.setLockedWidth(true);
MyCellField nomProprietaire = new MyCellField("nomProprietaire");
PdfPCell cellNomProprietaire = new PdfPCell();
cellNomProprietaire.setCellEvent(nomProprietaire);
Phrase nomDonneurOrdre = new Phrase("My Text Here");
PdfPCell cellNomDonneurOrdre = new PdfPCell(nomDonneurOrdre);
Phrase adresseDonneurOrdre = new Phrase("My Address Here");
PdfPCell cellAdresseDonneurOrdre = new PdfPCell(adresseDonneurOrdre);
//table.addCell(cellNomProprietaire);
table.addCell(cellNomDonneurOrdre);
table.addCell(cellAdresseDonneurOrdre);
/*table.addCell(cellNomProprietaire);
table.addCell(cellNomProprietaire);*/
table.writeSelectedRows(0, -1, 155, 610, canvas);
pdfStamper.close();
和“MyCellField”类:
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle, fieldname);
try {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
此代码有效,但是当我添加table.addCell(cellNomProprietaire)行时,应用程序崩溃了。 你有好主意吗 ? 谢谢, Corentin
EDIT1:堆栈跟踪
FATAL EXCEPTION: main
Process: com.piveteau.pivforet, PID: 12900
java.lang.RuntimeException: Unsupported in this context. Use PdfStamper.addAnnotation()
at com.itextpdf.text.pdf.PdfStamperImp.addAnnotation(PdfStamperImp.java:1312)
at com.piveteau.pivforet.tools.SelfNoteFragment$MyCellField.cellLayout(SelfNoteFragment.java:67)
at com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:599)
at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:829)
at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:962)
at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:908)
at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:887)
at com.piveteau.pivforet.tools.SelfNoteFragment.createPdf(SelfNoteFragment.java:176)
at com.piveteau.pivforet.tools.SelfNoteFragment.onActivityCreated(SelfNoteFragment.java:95)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
这是你想要的吗?我不知道如何获得Stack跟踪。
答案 0 :(得分:0)
按照Bruno Lowagie的评论,我创建了一个实例变量pdfStamper,我在MyCellField类中使用它:
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle, fieldname);
try {
final PdfFormField field = textField.getTextField();
pdfStamper.addAnnotation(field, 1);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
这对我有用,谢谢你的帮助!