2017-02-06 28 views
0

PdfFormFieldを含むPdfPTableを既存のPDFに書き込むことができないのはなぜですか? iTextSharpは私に言いますiTextsharp PdfPTable writeselectrows

このコンテキストではサポートされていません。 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 
+0

そして、それは何ですか? – Wotikar

+0

'FieldPositioningEvents'は' writer.addAnnotation(field) '/' fieldWriter.addAnnotation(cellField) 'コールを使用するため、新しいPDFを作成するために' PdfDocument'インスタンスと組み合わせてベース 'PdfWriter'を使用する場合にのみ使用できますスタンピングのコンテキストではサポートされていません。 'PdfStamper' /' PdfStamperImp'にページ番号がある 'addAnnotation'オーバーロードを使用する独自のセルとページイベントリスナーを作成します。 – mkl

+0

サンプルコードやリンクがありますか? – Wotikar

答えて

0

問題

iTextSharpクラスFieldPositioningEventsであるとして、それがどのwriter.addAnnotation(field)/fieldWriter.addAnnotation(cellField)呼び出しを使用しているため、最初から新しいPDFを作成するためにPdfDocumentインスタンスと組み合わせてベースPdfWriterを使用している場合にのみ使用可能ですスタンピングのコンテキストではサポートされていません。

は単にPdfStamperに存在するページ番号とaddAnnotationオーバーロードを使用して、独自のセルのイベントリスナーを作成し、この制限を回避するためのソリューション。

(残念ながら私は、この世紀にはまだコーディング任意の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(); 
+0

そして、もし私が最初のページにのみテーブルをしたいのですが、どうすればいいですか? – Wotikar

+0

* "最初のページにのみテーブルが必要な場合はどうすればいいですか?" * - ページパラメータとして「1」を使用します。 – mkl

関連する問題