gpt4 book ai didi

Java Android - iText - 使用 cellEvent writeSelectedRows

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:42 27 4
gpt4 key购买 nike

我正在尝试在打开的 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) 行时,应用程序崩溃。你有好主意吗 ?谢谢你,科朗坦

编辑1:堆栈跟踪

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)

这是你想要的吗?我不确定如何获取堆栈跟踪。

最佳答案

根据 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);
}
}
}

这对我有用,谢谢你的帮助!

关于Java Android - iText - 使用 cellEvent writeSelectedRows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com