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

时间:2017-05-23 16:40:56

标签: vb.net pdf printing itext

我在单击按钮时有以下代码,效果非常好。它需要十个单独的.pdf页面并将它们合并为一个十页的.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 

1 个答案:

答案 0 :(得分:1)

问题不在于PDF文档,您需要添加代码以告知打印机双面打印,您不需要在PDF中执行此操作。显然你需要一台双面打印机