【问题标题】:Add a table to existing PDF itextsharp with pdfStamper and VB or C#使用 pdfStamper 和 VB 或 C# 将表格添加到现有 PDF itextsharp
【发布时间】:2013-09-08 23:10:35
【问题描述】:

我想使用一个函数将表格插入到现有的 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 here 他们使用

  doc.Open()
...
    doc.Add(table)
...
    doc.Close()

如何在 C# 甚至 VB 中使用 pdfstamper 和 itextsharp 添加表格?

【问题讨论】:

    标签: c# vb.net itext


    【解决方案1】:

    只需插入类似stamper.Close()之前的内容:

        Dim nTbl As PdfPTable = New PdfPTable(2)
        'create column sizes 
        Dim rows As Single() = {50.0F, 250.0F}
        'set row width 
        nTbl.SetTotalWidth(rows)
        nTbl.AddCell("Cell1")
        nTbl.AddCell("Cell2")
        'coords x=300,y=300
        nTbl.WriteSelectedRows(0, 50, 300, 300, stamper.GetOverContent(1))
    
    
        stamper.Close()
        reader.Close()
    

    【讨论】:

      【解决方案2】:
          Dim oldFile As String = "D:\karuppasamy\OBM Application Form 2014 Final.pdf"
      
          Dim newFile As String = "D:\karuppasamy\PDF\OBM Application Form 2014 Final.pdf"
      
          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(oldFile)
      
              rect = reader.GetPageSizeWithRotation(1)
      
              stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(newFile, IO.FileMode.Create))
      
              cb = stamper.GetOverContent(1)
              Dim ct = New ColumnText(cb)
              ct.Alignment = Element.ALIGN_LEFT
              ct.SetSimpleColumn(70, 36, PageSize.A4.Width - 36, PageSize.A4.Height - 300)
              Dim nTbl As PdfPTable = New PdfPTable(2)
              'create column sizes 
              Dim rows As Single() = {135.0F, 145.0F}
              'set row width 
              nTbl.SetTotalWidth(rows)
      
              nTbl.AddCell(New Paragraph("Application Ref No:", FontFactory.GetFont("Arial", 15)))
              nTbl.AddCell(New Paragraph("HPOBM00017", FontFactory.GetFont("Arial", 15)))
      
              'coords x=300,y=300
              '  nTbl.WriteSelectedRows(0, 50, 40, 835, stamper.GetOverContent(1))
              nTbl.WriteSelectedRows(0, 20, 300, 835, stamper.GetOverContent(1))
      
              stamper.Close()
              reader.Close()
              ct.Go()
      
          Catch ex As Exception
              Throw ex
          End Try
      

      【讨论】:

      • 最好包含对您的更改的高级描述。此外,您的代码具有指向您的主目录的硬编码路径,这限制了可移植性。
      • 你应该解释你的答案,而不仅仅是发布代码
      猜你喜欢
      • 2013-02-09
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多