Home » Asp.netRSS

Help - Merging Two PDF files OR Merging Two Binary files

For some reason the last PDF file to be written to the "final document" is the only PDF File that is visible when the "final document" is open.  However, all of the bytes are accounted for in the file size...so for example: file1 = 60 bytes and file2 = 49 bytes.  The final document has 109 bytes but only contains file2 when viewed in the pdf viewer.  Any ideas?  What am I doing wrong? 


Public Sub getPDFsAndMerge()

        Dim myFinalDocumentPath As String = "C:\Temp\myFinalMergedPDFFile.pdf"
        Dim myFirstDocumentPath As String = "C:\Temp\fileOne.pdf"
        Dim mySecondDocumentPath As String = "C:\Temp\FileTwo.pdf"

        Dim fsFinalStream As New FileStream(myFinalDocumentPath, FileMode.Append, FileAccess.Write)

        Dim fsFirstStream As New FileStream(myFirstDocumentPath, FileMode.Open, FileAccess.Read)
        Dim fsSecondStream As New FileStream(mySecondDocumentPath, FileMode.Open, FileAccess.Read)

        Dim count As Integer = 1024
        Dim buffer(count - 1) As Byte

        '*** Write to final Document from first stream.
        count = fsFirstStream.Read(buffer, 0, count)
        Do Until count = 0
            fsFinalStream.Write(buffer, 0, count)
            count = fsFirstStream.Read(buffer, 0, count)

        '*** Write to final Document from second stream.
        count = fsSecondStream.Read(buffer, 0, count)
        Do Until count = 0
            fsFinalStream.Write(buffer, 0, count)
            count = fsSecondStream.Read(buffer, 0, count)

    End Sub


3 Answers Found


Answer 1

You can't just push two binary files together and get a valid new one any more than you could push two Mini Coopers together and get an SUV.

You'll need a libray like this one: http://itextpdf.com/


Answer 2

It would be nice if as a programmer you wouldn't assume that something can't be done just because you haven't
done it before.  There is always a solution.  Using adobe components.  I found this
solution on another site.


 Sub MergePDF(ByVal ThePath As String, ByVal outFileName As String)
        On Error GoTo serror

        Dim dPDDocMerge As New Acrobat.AcroPDDoc
        Dim dPDDoc As New Acrobat.AcroPDDoc
        Dim strFiles() As String
        Dim numPage As Integer
        Dim TotalPage As Integer
        Dim objThisFile As IO.FileInfo 'get FileInfo object for file  string

        strFiles = System.IO.Directory.GetFiles(ThePath) ' Read in the file names

        Dim b As Boolean ' mostly for testing purposes... could use it for error 'checking to make sure that a file is really added before deleleting it...

        For i As Integer = 0 To strFiles.Length - 1 ' run through all the files  in 'the directory
            objThisFile = New IO.FileInfo(strFiles(i)) ' Get the extension

            If objThisFile.Extension = ".pdf" Then ' Only add in PDFs

                If dPDDocMerge.GetFileName = "" Then ' check if it's the first file
                    dPDDocMerge = New Acrobat.AcroPDDoc
                    b = dPDDocMerge.Open(strFiles(i)) ' open  first file
                    TotalPage = dPDDocMerge.GetNumPages
                    dPDDoc = New Acrobat.AcroPDDoc
                    b = dPDDoc.Open(strFiles(i)) ' open other files
                    numPage = dPDDocMerge.GetNumPages ' get the page count
                    TotalPage += numPage
b = dPDDocMerge.InsertPages(numPage - 1, dPDDoc, 0, dPDDoc.GetNumPages, _ False) ' Insert
                End If
            End If

        'b = dPDDocMerge.Save(1, ThePath & "\" & outFileName) ' save file
        b = dPDDocMerge.Save(1, ThePath & "\" & "\MyTest.PDF") ' save file
        b = dPDDocMerge.Close()
        Exit Sub
    End Sub


Answer 3

Wellens, you give perfect answers to question but without sample code. Please provide sample codes so that it will be very helpful to a newbies who are struggling to get the code....

I have  a question like.....i am doing shopping cart project. i took a grid and displayed all items from database and i placed a addtocart button in page. once clicked these items will be added to cart in nextpage. In this page i have a gridview with checkbox and delete button in page to delete a row of selected checkbox. my sample code in second page is like this...

for (int i = 0; i < GridView1.Rows.Count; i++)
            CheckBox chkdel = (CheckBox)GridView1.Rows[i].Cells[5].FindControl("chkdelete");
            if (chkdel.Checked == true)

I dont know what to next to delete a row from grid without deleting from actual database.

Please help  me out as i need to release my project.....

Thanks in advance.....


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter