我想使用一个函数将表格插入到现有的 PDF 中,例如:
Private Shared Function wTable(ByVal cols As Integer) As iTextSharp.text.Table
Dim table As New iTextSharp.text.Table(cols)
With table
.WidthPercentage = 100
.BorderWidth = 0
.Cellpadding = 1
.Cellspacing = 0
.TableFitsPage = False
.CellsFitPage = True
.AutoFillEmptyCells = True
.Widths = New Single() {20, 80}
End With
Dim font As iTextSharp.text.Font = font8
Dim fontBold As iTextSharp.text.Font = font8Bold
Dim c As Cell = New Cell(New Phrase("Allacci Sparsi", fontBold))
c.SetHorizontalAlignment("center")
table.AddCell(c, 0, 0)
Dim str As String = "Il termine di esecuzione dei lavori è stabilito all’art. 14 del Foglio Condizioni e di seguito definiti in Tabella 1. " _
& "All'art. 16 del 'Foglio Condizioni' sono definiti i riferimenti per l’applicazione delle penali in caso di ritardo nell’esecuzione delle opere richieste. " _
& " All'art. 17 del 'Foglio Condizioni' sono definiti i criteri per l’incentivo sulla celerità di intervento."
c = New Cell(New Phrase(str, font))
c.SetHorizontalAlignment("left")
table.AddCell(c, 0, 1)
Return table
End Function
然后使用压模调用它,例如:
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim rect As iTextSharp.text.Rectangle = Nothing
Dim pageCount As Integer = 0
Try
reader = New iTextSharp.text.pdf.PdfReader(sourcePdf)
rect = reader.GetPageSizeWithRotation(1)
stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputPdf, IO.FileMode.Create))
cb = stamper.GetOverContent(1)
Dim ct = New ColumnText(cb)
ct.Alignment = Element.ALIGN_CENTER
ct.SetSimpleColumn(36, 36, PageSize.A4.Width - 36, PageSize.A4.Height - 300)
ct.AddElement(wTable(2))
ct.Go()
stamper.Close()
reader.Close()
Catch ex As Exception
Throw ex
End Try
然而程序抛出异常说:“元素是不允许的”。
我看过一个插入表格的例子,但他们在这里不使用pdfstamper
doc.Open()
...
doc.Add(table)
...
doc.Close()
如何在 C# 甚至 VB 中使用带有 itextsharp 的 pdfstamper 添加表格?