問題
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();
そして、それは何ですか? – Wotikar
'FieldPositioningEvents'は' writer.addAnnotation(field) '/' fieldWriter.addAnnotation(cellField) 'コールを使用するため、新しいPDFを作成するために' PdfDocument'インスタンスと組み合わせてベース 'PdfWriter'を使用する場合にのみ使用できますスタンピングのコンテキストではサポートされていません。 'PdfStamper' /' PdfStamperImp'にページ番号がある 'addAnnotation'オーバーロードを使用する独自のセルとページイベントリスナーを作成します。 – mkl
サンプルコードやリンクがありますか? – Wotikar