首页 文章

iTextsharp PdfPTable writeselectedrows

提问于
浏览
0

为什么我不能将包含 PdfFormFieldPdfPTable 写入现有PDF? iTextSharp告诉我

在此上下文中不受支持 . 使用PdfStamper.addAnnotation() .

Private Sub RR(PdfFileName As String)
    Dim reader As New PdfReader(PdfFileName)
    Dim RandomFileName As String = System.IO.Path.GetRandomFileName
    Dim OutputPdf As String = System.IO.Path.GetDirectoryName(PdfFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(RandomFileName) & ".pdf"

    Dim stamper As New PdfStamper(reader, New FileStream(OutputPdf, FileMode.Create))
    Dim table As New PdfPTable(1)
    Dim PageMargin As Single = 20
    Dim tbCell As New PdfPCell()

    table.TotalWidth = (reader.GetPageSize(1).Right - reader.GetPageSize(1).Left) - 20
    Dim PDfFormField As PdfFormField = CreateTextField(stamper.Writer, "TestField", 0, 0)
    tbCell = New PdfPCell With {.CellEvent = New iTextSharp.text.pdf.events.FieldPositioningEvents(stamper.Writer, PDfFormField), .MinimumHeight = 10, .BorderWidth = 0.1}

    With table
        .AddCell(tbCell)
    End With

    table.WriteSelectedRows(0, -1, PageMargin, Bottom + table.TotalHeight + PageMargin, stamper.GetOverContent(1))

    stamper.Close()
    reader.Close()

End Sub

1 回答

  • 0

    问题

    iTextSharp类 FieldPositioningEvents 只能在将 PdfWriterPdfDocument 实例结合使用时从头开始创建新PDF,因为它使用了冲压上下文中不支持的 writer.addAnnotation(field) / fieldWriter.addAnnotation(cellField) 调用 .

    解决方案

    要解决此限制,只需创建自己的单元格事件侦听器,该侦听器使用 addAnnotation 重载,并在 PdfStamper 中显示页码 .

    (遗憾的是,我在本世纪尚未完成任何VB编码;但我希望,C#中的示例也可以提供帮助 . )

    例如 . 以下是从原始 FieldPositioningEvents 代码派生的事件监听器,但仅限于单元格事件并适用于 PdfStamper

    public class StampingFieldPositioningEvents : IPdfPCellEvent
    {
        /** Keeps the form field that is to be positioned in a cellLayout event. */
        protected PdfFormField cellField = null;
    
        /** The PdfStamper to use when a field has to added in a cell event. */
        protected PdfStamper fieldStamper = null;
    
        /** Some extra padding that will be taken into account when defining the widget. */
        public float padding;
    
        /** The page on which the field is added */
        int fieldPage = 0;
    
        /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
        public StampingFieldPositioningEvents(PdfStamper stamper, PdfFormField field, int page)
        {
            this.cellField = field;
            this.fieldStamper = stamper;
            this.fieldPage = page;
        }
    
        /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
        public StampingFieldPositioningEvents(PdfStamper stamper, String text, int page)
        {
            this.fieldStamper = stamper;
            TextField tf = new TextField(stamper.Writer, new Rectangle(0, 0), text);
            tf.FontSize = 14;
            cellField = tf.GetTextField();
            this.fieldPage = page;
        }
    
        /** The padding to set. */
        virtual public float Padding
        {
            set
            {
                padding = value;
            }
            get
            {
                return padding;
            }
        }
    
        /** IPdfPCellEvent implementation */
        virtual public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases)
        {
            cellField.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding)));
            fieldStamper.AddAnnotation(cellField, fieldPage);
        }
    }
    

    它的使用方式类似于OP的原始代码:

    PdfReader reader = new PdfReader(SOURCE);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(DESTINATION, FileMode.Create));
    
    int pageMargin = 20;
    PdfPTable table = new PdfPTable(1);
    table.TotalWidth = reader.GetPageSize(1).Width - 40;
    
    PdfFormField formField = PdfFormField.CreateTextField(stamper.Writer, false, false, 50);
    formField.SetWidget(new Rectangle(0, 0), null);
    formField.FieldName = "TestField";
    PdfPCell tbCell = new PdfPCell
    {
        CellEvent = new StampingFieldPositioningEvents(stamper, formField, 1),
        MinimumHeight = 10,
        BorderWidth = 0.1f
    };
    
    table.AddCell(tbCell);
    
    table.WriteSelectedRows(0, -1, reader.GetPageSize(1).Left + pageMargin, reader.GetPageSize(1).Bottom + table.TotalHeight + pageMargin, stamper.GetOverContent(1));
    
    stamper.Close();
    reader.Close();
    

相关问题