合并两个.pdf页面,然后在VB.net中打印一个双面.pdf

合并两个.pdf页面,然后在VB.net中打印一个双面.pdf,vb.net,pdf,printing,itext,Vb.net,Pdf,Printing,Itext,我有下面的代码,在一个按钮点击,工作得很好。它需要10个单独的.pdf页面,并将它们合并成一个10页的.pdf文档,然后将该文档发送到本地打印机 现在,我想修改代码,使其接受十个单独的页面并合并它们,从而最终得到一个包含五个页面的.pdf文档,每个页面的两面都有打印。最终文件的第一页将由第1页和第2页组成,最终文件的第二页将由第3页和第4页组成,以此类推 任何帮助都将不胜感激 Protected Sub Button2_Click(ByVal sender As Object, ByVal e

我有下面的代码,在一个按钮点击,工作得很好。它需要10个单独的.pdf页面,并将它们合并成一个10页的.pdf文档,然后将该文档发送到本地打印机

现在,我想修改代码,使其接受十个单独的页面并合并它们,从而最终得到一个包含五个页面的.pdf文档,每个页面的两面都有打印。最终文件的第一页将由第1页和第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 
受保护的子按钮2\u单击(ByVal sender作为对象,ByVal e作为System.EventArgs)处理按钮2。单击
'临时文件以保存合并的PDF。
Dim tempFilename=IO.Path.GetTempFileName()
将tempFile设置为新的IO.FileStream(tempFilename,IO.FileMode.Create)
'设置iTextSharp文档以保存合并的PDF
Dim mergedDocument作为新的iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER)
将复印机调暗为新的iTextSharp.text.pdf.PdfCopy(合并文档,临时文件)
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.ConnectionString(“XXX\u 690218\u ccmConnectionString”).ConnectionString)
Dim PTime作为整数=0
Dim conn3作为Data.SqlClient.SqlConnection
Dim cmd3 As Data.SqlClient.SqlCommand
Dim r3作为Data.SqlClient.SqlDataReader
conn3=New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionString(“XXX\u 690218\u ccmConnectionString”).ConnectionString)

cmd3=New Data.SqlClient.SqlCommand(“从[MemProductionDetails]中选择*,其中([Counter]>='1'和[Counter]问题不在于PDF文档,你需要添加代码来告诉打印机双面打印,而不是在PDF中。显然,你需要双面打印机

你需要添加代码来告诉打印机双面打印,而不是在PDF中。显然,你需要双面打印机。哇-现在我觉得愚蠢。当然。在屏幕前花了太多的时间。把十页纸送到指定双面打印的打印机,我得到了五页双面纸。谢谢。