【问题标题】:iTextSharp - Read also Second PageiTextSharp - 另请阅读第二页
【发布时间】:2016-12-15 10:15:48
【问题描述】:

我是新来的。我正在编写一个程序并使用 itextsharp。我想导入各种文件,然后保存。不幸的是我不明白,第二页被阅读了。第一页kappt super的编辑。这是我的代码:

       If ComboBox1.SelectedItem = "DPD" Then


        Dim oldFile As String = "templates/dpd-schadenformular.pdf"
        Dim newFile As String = "output/DPD-Output.pdf"

        ' Create reader
        Dim reader As New PdfReader(oldFile)
        Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
        Dim document As New Document(size)

        ' Create the writer
        Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
        document.Open()
        Dim cb As PdfContentByte = writer.DirectContent

        ' Set the font, color and size properties for writing text to the PDF
        Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
        cb.SetColorFill(BaseColor.DARK_GRAY)
        cb.SetFontAndSize(bf, 8)

        ' Write text in the PDF
        cb.BeginText()

        Dim kundennummer As String = "Kundennummer " & Form3.dpdkdnr.Text

        Dim trackid1 As String = track1.Text
        Dim trackid2 As String = track2.Text

        Dim descr As String = beschreibungschaden.Text
        Dim warenart As String = paketinhalt.Text

        Dim empfnam As String = empfnamebox.Text
        Dim empfstr As String = empfstrbox.Text
        Dim empfplz As String = empfplzbox.Text





        ' Set the alignment and coordinates here
        cb.ShowTextAligned(1, kundennummer, 360, 638, 0)

        cb.ShowTextAligned(1, trackid1, 336, 685, 0)
        cb.ShowTextAligned(1, trackid2, 430, 685, 0)

        cb.ShowTextAligned(1, descr, 150, 135, 0)
        cb.ShowTextAligned(1, warenart, 90, 235, 0)

        cb.ShowTextAligned(1, empfnam, 370, 441, 0)
        cb.ShowTextAligned(1, empfstr, 370, 416, 0)
        cb.ShowTextAligned(1, empfplz, 370, 381, 0)



        cb.EndText()

        ' Put the text on a new page in the PDF 
        Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
        cb.AddTemplate(page, 0, 0)

        ' Close the objects
        document.Close()
        fs.Close()
        writer.Close()
        reader.Close()


        ' -----------------------------------------------
        ' -----------------------------------------------
        ' --------------------- DPD Ende ----------------
        ' -----------------------------------------------
        ' -----------------------------------------------

    End If

关于方法、解决方案或帮助我会非常高兴并提前感谢您

莫里斯

【问题讨论】:

  • A 使用PdfWriter 合并来自不同文档的页面通常是一个不好的选择;而是应该使用PdfCopy(它甚至允许在复制的页面上进行标记)。 B 如果由于某种原因您的情况是规则的例外并且PdfWriteruse 是合适的,请使用document.NewPager() 并且可能使用循环。
  • 哇,感谢您的快速反馈 :) 因为我没有太多经验,您能帮我举个例子吗? `提前谢谢你:(
  • 我最后一次进行 VB 编码是在 1990 年代后期。因此,恐怕我不是快速构建一些 VB 示例的人......

标签: vb.net itext


【解决方案1】:

这是我用来向目标文档添加新页面的方法。

 'myarray is the document to be added,   mydoc is the document added to   

Public Function AdddbFiletoDoc(myarray() As Byte, mydoc As Document, mywriter As PdfWriter, mycb As PdfContentByte)
    Dim myreader As New PdfReader(myarray)
    Dim numofPages As Integer = myreader.NumberOfPages, mypage As PdfImportedPage
    Dim currpage As Integer = 0
    Do While currpage < numofPages
        currpage += 1
        mydoc.SetPageSize(PageSize.A4)
'You can add a flag to the method to determine if you want a new page or not
        mydoc.NewPage()
        mypage = mywriter.GetImportedPage(myreader, currpage)
        mycb.AddTemplate(mypage, 1.0F, 0, 0, 1.0F, 0, 0)
    Loop

End Function

【讨论】:

  • 谢谢你 haim katz,不幸的是,我的问题是源文件有 pdf 2 面,我想简单地阅读这些内容并将它们存储在上面。我不是视觉基础方面的专家。如果有人有 1:1 的解决方案,我将不胜感激。
  • 我不明白您将 pdf 存储在哪里。在数据库中?在同一页面上?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多