为什么我不能将包含PdfPTable的PdfFormField写到现有的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发布于 2017-02-08 10:04:55
问题
现在的iTextSharp类FieldPositioningEvents只有在结合使用基本PdfWriter和PdfDocument实例从头创建新的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();https://stackoverflow.com/questions/42068371
复制相似问题