首页 文章

iText7中的PdfPCellEvent等价物是什么?

提问于
浏览
0

我试图从白皮书“digitalsignatures20130304 Bruno Lowagie的白皮书”[第54页]中运行一些样本 . 我找不到PdfPCellEvent,PdfContentByte的iText7等价物 . 请指导我 .

1 回答

  • 0

    iText 7不是iText 5的克隆版,带有一些重构的包或类名,它有一个新的架构 . 因此,并不总是有1:1的对应类,但还有其他方法可以达到相同的效果 .

    有助于通过iText人员查看样本的端口;在github上,您可以在i7js-signatures项目中找到移植到iText 7的白皮书中的许多样本 .

    例如,您提到了Digital Signatures for PDF documents白皮书的第54页 . 考虑到您给出的关键字以及该页面上只有一个示例的事实,我假设您要翻译此方法和帮助程序类:

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

    您可以在类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 而不是事件,并且字段构造和定位不再分离,它们发生在代码中的相同位置 .

相关问题