【问题标题】:Merging two .pdf pages and then printing a 2-sided .pdf In VB.net合并两个 .pdf 页面,然后在 VB.net 中打印 2 面 .pdf
【发布时间】:2017-10-23 17:55:20
【问题描述】:

我在按钮单击中有以下代码,效果非常好。它需要十个单独的 .pdf 页并将它们合并为一个十页的 .pdf 文档,然后将该文档发送到本地打印机。

现在我想修改代码,使其获取十个单独的页面并将它们合并,这样我最终得到一个包含五页的单个 .pdf 文档,每页的两面都有打印。最终文档的第一页将由第 1 页和第 2 页组成,最终文档的第 2 页将由第 3 页和第 4 页组成,依此类推。

任何帮助将不胜感激!

 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' Temp file to hold merged PDF.
    Dim tempFilename = IO.Path.GetTempFileName()
    Dim tempFile As New IO.FileStream(tempFilename, IO.FileMode.Create)

    ' Set up iTextSharp document to hold merged PDF
    Dim mergedDocument As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER)
    Dim copier As New iTextSharp.text.pdf.PdfCopy(mergedDocument, tempFile)
    mergedDocument.Open()

    Dim conn As Data.SqlClient.SqlConnection
    Dim cmd As Data.SqlClient.SqlCommand
    Dim r As Data.SqlClient.SqlDataReader
    conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("XXX_690218_ccmConnectionString").ConnectionString)

    Dim PTime As Integer = 0
    Dim conn3 As Data.SqlClient.SqlConnection
    Dim cmd3 As Data.SqlClient.SqlCommand
    Dim r3 As Data.SqlClient.SqlDataReader
    conn3 = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("XXX_690218_ccmConnectionString").ConnectionString)
    cmd3 = New Data.SqlClient.SqlCommand("SELECT * FROM [MemProductionDetails] Where ([Counter] >= '1' AND [Counter] <= '10') ORDER BY [Counter], [Counter2]", conn3)
    cmd3.CommandType = Data.CommandType.Text
    conn3.Open()
    r3 = cmd3.ExecuteReader(Data.CommandBehavior.CloseConnection)
    If r3.HasRows Then
        Do While r3.Read()

             Dim pic1 As String = "C:\Users\XXX\Documents\Sync\images\cardlabels\" & strLetter & ".png"
              Using inputPdfStream As IO.Stream = New IO.FileStream("C:\Users\XXX\Documents\Recipes\" + r3.Item("ItemNum").ToString + ".pdf", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
               Using inputImageStream As IO.Stream = New IO.FileStream(pic1, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

                    Using outputPdfStream As IO.Stream = New IO.FileStream(Server.MapPath(".") + "/recipesnew/" + r3.Item("ItemNum").ToString + ".pdf", IO.FileMode.Create, IO.FileAccess.ReadWrite, IO.FileShare.None)
                        Dim reader1 = New iTextSharp.text.pdf.PdfReader(inputPdfStream)
                        Dim stamper = New iTextSharp.text.pdf.PdfStamper(reader1, outputPdfStream)
                        Dim pdfContentByte = stamper.GetOverContent(1)

                        Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(inputImageStream)
                        image__1.SetAbsolutePosition(527, 710)
                        image__1.ScaleAbsolute(60, 60)
                        pdfContentByte.AddImage(image__1)

                        Dim bf As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
                        pdfContentByte.SetColorFill(iTextSharp.text.BaseColor.DARK_GRAY)
                        pdfContentByte.SetFontAndSize(bf, 9)
                        pdfContentByte.BeginText()

                        pdfContentByte.ShowTextAligned(1, "X", 292, 46, 0)
                        pdfContentByte.ShowTextAligned(1, "X", 297, 29, 0)
                        pdfContentByte.ShowTextAligned(1, "X", 411, 46, 0)
                        pdfContentByte.ShowTextAligned(1, "X", 416, 29, 0)
                        pdfContentByte.ShowTextAligned(1, "X", 529, 46, 0)
                        pdfContentByte.ShowTextAligned(1, "X", 533, 29, 0)
                        Dim strSingDbl As String = "  Single"
                        If r3.Item("ItemQuantity").ToString = "3" Or r3.Item("ItemQuantity").ToString = "4" Then
                            intLabelCount = (intLabelCount * 2) + 1
                        End If
                        If r3.Item("ItemQuantity").ToString = "2" Then
                            strSingDbl = "  Double"
                        ElseIf r3.Item("ItemQuantity").ToString = "3" Then
                            strSingDbl = " Sng & Dbl"
                        ElseIf r3.Item("ItemQuantity").ToString = "4" Then
                            strSingDbl = "2 Doubles"
                        End If

                        pdfContentByte.ShowTextAligned(1, strSingDbl, 553, 755, 0)
                        pdfContentByte.ShowTextAligned(1, (intLabelCount + 1).ToString & " Total", 555, 715, 0)


                        pdfContentByte.EndText()


                        stamper.Close()
                        reader1.Close()
                        outputPdfStream.Close()
                        inputImageStream.Close()
                        inputPdfStream.Close()
                        outputPdfStream.Dispose()

                    End Using
                End Using
            End Using

            Dim reader As New iTextSharp.text.pdf.PdfReader(New iTextSharp.text.pdf.RandomAccessFileOrArray(Server.MapPath(".") + "/Tomsrecipesnew/" + r3.Item("ItemNum").ToString + ".pdf", True), Nothing)

            For pageNum = 1 To reader.NumberOfPages
                copier.AddPage(copier.GetImportedPage(reader, pageNum))
            Next

            PTime = PTime + 1
        Loop

        mergedDocument.Close()
        tempFile.Dispose()

        Dim pathToExecutable As String = "AcroRd32.exe"
        Dim sReport = tempFilename 'Complete name/path of PDF file

        Dim SPrinter = "RICOH MP C5503"


        Dim starter As New ProcessStartInfo(pathToExecutable, "/t """ + sReport + """ """ + SPrinter + """")

        Dim Process As New Process()

        Process.StartInfo = starter
        Process.Start()
        If TextBox7.Text = TextBox8.Text Then
            Process.WaitForExit(PTime * 9000)
        Else
            Process.WaitForExit(PTime * 6800)
        End If

        Process.Kill()
        Process.Close()


    End If
    conn3.Close()
    r3.Close()




End Sub 

【问题讨论】:

  • 您将需要添加代码来告诉打印机双面打印,您不会在 PDF 中这样做。显然你需要一台双面打印机。
  • 哇——现在我觉得自己很愚蠢。当然。在屏幕前的时间太多了。将十页发送到指定用于双面打印的打印机,我得到五页双面打印。谢谢。

标签: vb.net pdf printing itext


【解决方案1】:

问题不在于 PDF 文档,您需要添加代码来告诉打印机双面打印,而您不会在 PDF 中这样做。显然你需要一台双面打印机

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2013-06-13
    • 2018-06-27
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多