iText7 中的 PdfPCellEvent 等价物是什么?
What are the PdfPCellEvent equivalent in iText7?
我正在尝试 运行 白皮书 "digitalsignatures20130304 A White Paper by Bruno Lowagie" [第 54 页] 中的一些样本。我找不到 PdfPCellEvent、PdfContentByte 的 iText7 等效项。
请指导我。
iText 7 不是 iText 5 的克隆,带有一些重构包或 class 名称,它有一个新的架构。因此,并不总是有一个 1:1 对应 class 但还有其他方法可以达到相同的效果。
iText的人看看samples的端口很有帮助;在 github 上,您可以在 i7js-signatures 项目中找到移植到 iText 7 的白皮书中的许多示例。
例如,您提到了 Digital Signatures for PDF documents 白皮书的第 54 页。考虑到您提供的关键字以及该页面上只有一个示例这一事实,我假设您想翻译此方法和助手 class:
protected PdfPCell createSignatureFieldCell(PdfWriter writer, String name) {
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(50);
PdfFormField field = PdfFormField.createSignature(writer);
field.setFieldName(name);
field.setFlags(PdfAnnotation.FLAGS_PRINT);
cell.setCellEvent(new MySignatureFieldEvent(field));
return cell;
}
public class MySignatureFieldEvent implements PdfPCellEvent {
public PdfFormField field;
public MySignatureFieldEvent(PdfFormField field) {
this.field = field;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
field.setPage();
field.setWidget(position, PdfAnnotation.HIGHLIGHT_INVERT);
writer.addAnnotation(field);
}
}
您在 class C2_11_SignatureWorkflow:
中找到此端口
protected Cell createSignatureFieldCell(String name) {
Cell cell = new Cell();
cell.setHeight(50);
cell.setNextRenderer(new SignatureFieldCellRenderer(cell, name));
return cell;
}
class SignatureFieldCellRenderer extends CellRenderer {
public String name;
public SignatureFieldCellRenderer(Cell modelElement, String name) {
super(modelElement);
this.name = name;
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
PdfFormField field = PdfFormField.createSignature(drawContext.getDocument(), getOccupiedAreaBBox());
field.setFieldName(name);
field.getWidgets().get(0).setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
field.getWidgets().get(0).setFlags(PdfAnnotation.PRINT);
PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(field);
}
}
如您所见,两者之间存在某些差异,特别是使用 Renderer
代替事件,并且字段构造和定位不再分开,它们发生在代码。
我正在尝试 运行 白皮书 "digitalsignatures20130304 A White Paper by Bruno Lowagie" [第 54 页] 中的一些样本。我找不到 PdfPCellEvent、PdfContentByte 的 iText7 等效项。 请指导我。
iText 7 不是 iText 5 的克隆,带有一些重构包或 class 名称,它有一个新的架构。因此,并不总是有一个 1:1 对应 class 但还有其他方法可以达到相同的效果。
iText的人看看samples的端口很有帮助;在 github 上,您可以在 i7js-signatures 项目中找到移植到 iText 7 的白皮书中的许多示例。
例如,您提到了 Digital Signatures for PDF documents 白皮书的第 54 页。考虑到您提供的关键字以及该页面上只有一个示例这一事实,我假设您想翻译此方法和助手 class:
protected PdfPCell createSignatureFieldCell(PdfWriter writer, String name) {
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(50);
PdfFormField field = PdfFormField.createSignature(writer);
field.setFieldName(name);
field.setFlags(PdfAnnotation.FLAGS_PRINT);
cell.setCellEvent(new MySignatureFieldEvent(field));
return cell;
}
public class MySignatureFieldEvent implements PdfPCellEvent {
public PdfFormField field;
public MySignatureFieldEvent(PdfFormField field) {
this.field = field;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
field.setPage();
field.setWidget(position, PdfAnnotation.HIGHLIGHT_INVERT);
writer.addAnnotation(field);
}
}
您在 class C2_11_SignatureWorkflow:
中找到此端口protected Cell createSignatureFieldCell(String name) {
Cell cell = new Cell();
cell.setHeight(50);
cell.setNextRenderer(new SignatureFieldCellRenderer(cell, name));
return cell;
}
class SignatureFieldCellRenderer extends CellRenderer {
public String name;
public SignatureFieldCellRenderer(Cell modelElement, String name) {
super(modelElement);
this.name = name;
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
PdfFormField field = PdfFormField.createSignature(drawContext.getDocument(), getOccupiedAreaBBox());
field.setFieldName(name);
field.getWidgets().get(0).setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
field.getWidgets().get(0).setFlags(PdfAnnotation.PRINT);
PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(field);
}
}
如您所见,两者之间存在某些差异,特别是使用 Renderer
代替事件,并且字段构造和定位不再分开,它们发生在代码。