user1375002
user1375002

Reputation: 103

Merging two .pdf pages and then printing a 2-sided .pdf In VB.net

I have the following code in a button click that works very nicely. It takes ten individual .pdf pages and merges them into one ten page .pdf document and then sends that document to a local printer.

Now I would like to modify the code so that it takes the ten individual pages and merges them so that I end up with a single .pdf document that contains five pages with printing on both sides of each page. Page one of the final document would be made up of pages 1 and 2, page two of the final document would be made up of pages 3 and 4, etc.

Any help wold be greatly appreciated!

 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 

Upvotes: 0

Views: 465

Answers (1)

Prescott Chartier
Prescott Chartier

Reputation: 1673

The issue is not with the PDF document, you will need to add code to tell the printer to print on both sides, you don't do that in the PDF. Obviously you would need a duplex printer

Upvotes: 1

Related Questions