Merge pdf 's in vb

Does anyone know how to combine two pdf's into one using visual basic? I did open both files and wrote it to a new file. but for some reason I can only see the first pdf, although the file new pdf is equal to first + second. Here is my code:
Dim InputData() As Byte
'A and b are files that I am trying to combine
'c is the new file
Open "c: empa.pdf" For Binary As #1
Open "c: emp.pdf" For Binary As #2
Open "c: empc.pdf" For Binary As #3
ReDim InputData(1 To LOF(1)) As Byte
Dim test As Long
Get #1, , InputData
Put #3, , InputData
ReDim InputData(1 To LOF(2)) As Byte
Get #2, , InputData
Put #3, , InputData
Close #1
Close #2
Close #3

Comments

  • : Does anyone know how to combine two pdf's into one using visual basic? I did open both files and wrote it to a new file. but for some reason I can only see the first pdf, although the file new pdf is equal to first + second. Here is my code:
    : Dim InputData() As Byte
    : 'A and b are files that I am trying to combine
    : 'c is the new file
    : Open "c: empa.pdf" For Binary As #1
    : Open "c: emp.pdf" For Binary As #2
    : Open "c: empc.pdf" For Binary As #3
    : ReDim InputData(1 To LOF(1)) As Byte
    : Dim test As Long
    : Get #1, , InputData
    : Put #3, , InputData
    : ReDim InputData(1 To LOF(2)) As Byte
    : Get #2, , InputData
    : Put #3, , InputData
    : Close #1
    : Close #2
    : Close #3
    :
    :
    I faced this problem and tried to use the same way. This is not going to work. You can use another way. What you can do convert pdf files to postscript files append them and convert back to pdf format. To do that you need download Ghostsript software(it's free) from http://www.ghostscript.com/doc/AFPL/get800.htm Do not forget to download GSview also (it's also free). When you will be done with downloading, let me know and I'll post some example of VB code
  • : Does anyone know how to combine two pdf's into one using visual basic? I did open both files and wrote it to a new file. but for some reason I can only see the first pdf, although the file new pdf is equal to first + second.

    It's not that simple. Open up a PDF in a text editor and you'll see what I mean. Here's the contents of a simple PDF file:

    [code]
    %PDF-1.3
    %
    4 0 obj
    << /Type /Info
    /Producer (FOP 0.20.4) >>
    endobj
    5 0 obj
    << /Length 660 /Filter [ /ASCII85Decode /FlateDecode ]
    >>
    stream
    Gb"/%9omaW&A@7.pt/3?Z'3g8Y:D14W'(-C=Gc.tUsmhf[7fkloYiCVf>NKM(?am9&?
    o;H00B5l>7TujjEgY+G)".FbPRrO7t%8"D=5L&SIJn'W!@s/5=JcbV3.##mu4U>%
    eVrIuMb@=n&U7+t][;,fDKY=0+b),HA;/'Kc5+46Foh+n:0Wo=$%7SgD-
    MiFgQSPLJ:93n*"($p0M4D`T0bE8Hb/C@O`^[H6"I1F*P^'61m
    (5Y7Hh@$J76L/7Ved7Sa9)m5t+EDG>C?Jp'-3hRRSqA`P!
    K$eLmT!,1'?I#S7<[429uf01-[KV91H+0^1d9YkGlCdZ#rPb@ld2fV=!BW8HA3n:)1CE_bt%
    Y3kkAjDu^#`s@]j-.,Lf",(u`n:?*o+^=sm.Jsb81D"Yi7iXc`W:O%?(5s@5o9tfZW )
    QIiloSAs=:mLI<U1YH*We>,O5-UJ#g$NgA[1/R6!#GYUnG#IQQX/IQ mP:s?,)
    6:qMe7m).8GVlI)l#Mi%MMDhPF?]P2c6?Ns)m8R(fAY^]_^=s%$goi"WBo[?87&J[<*2$-f*7?bmC$<W~>
    endstream
    endobj
    6 0 obj
    << /Type /Page
    /Parent 1 0 R
    /MediaBox [ 0 0 595 842 ]
    /Resources 3 0 R
    /Contents 5 0 R
    >>
    endobj
    7 0 obj
    << /Type /Font
    /Subtype /Type1
    /Name /F1
    /BaseFont /Helvetica
    /Encoding /WinAnsiEncoding >>
    endobj
    1 0 obj
    << /Type /Pages
    /Count 1
    /Kids [6 0 R ] >>
    endobj
    2 0 obj
    << /Type /Catalog
    /Pages 1 0 R
    >>
    endobj
    3 0 obj
    <<
    /Font << /F1 7 0 R >>
    /ProcSet [ /PDF /ImageC /Text ] >>
    endobj
    xref
    0 8
    0000000000 65535 f
    0000001035 00000 n
    0000001093 00000 n
    0000001143 00000 n
    0000000015 00000 n
    0000000071 00000 n
    0000000822 00000 n
    0000000928 00000 n
    trailer
    <<
    /Size 8
    /Root 2 0 R
    /Info 4 0 R
    >>
    startxref
    1221
    %%EOF
    [/code]

    First of all, notice the %PDF and %%EOF "tags"? If you just concatenate two PDFs together, the first %%EOF is going to stop any processing so you won't ever see the second. Secondly, do you see any text in there? The actual document looks something like this:

    [hr]
    [blue][size=5]Extensible Markup Language (XML) 1.0[/size][/blue]

    The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML.

    The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML.
    [hr]

    You don't see any of that text in the PDF because PDF content is in an indexed binary format. You can't just go around mashing them together all willy-nilly.

    That being said, I have heard that it is possible to accomplish what you are asking using Ghostscript and a special set of instructions, though I have never been able to figure it out.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • Ya, I noticed those things. I even opend the pdf file in Textpad. Removed some stuff thinking I can play around and see what I need to fiddle around with to get it to work. Before I rely on the ghost script thing I am going to get a copy of AppendPDF by Appligent. This ocx will allow you to merge 2 pdfs( lines or whatever you specify). I am going to play around and see what it does to the new file. Then see if I can write it myself.
    Thanks for all your inputs. I do appreciate it.

    Saro

    : : Does anyone know how to combine two pdf's into one using visual basic? I did open both files and wrote it to a new file. but for some reason I can only see the first pdf, although the file new pdf is equal to first + second.
    :
    : It's not that simple. Open up a PDF in a text editor and you'll see what I mean. Here's the contents of a simple PDF file:
    :
    : [code]
    : %PDF-1.3
    : %
    : 4 0 obj
    : << /Type /Info
    : /Producer (FOP 0.20.4) >>
    : endobj
    : 5 0 obj
    : << /Length 660 /Filter [ /ASCII85Decode /FlateDecode ]
    : >>
    : stream
    : Gb"/%9omaW&A@7.pt/3?Z'3g8Y:D14W'(-C=Gc.tUsmhf[7fkloYiCVf>NKM(?am9&?
    : o;H00B5l>7TujjEgY+G)".FbPRrO7t%8"D=5L&SIJn'W!@s/5=JcbV3.##mu4U>%
    : eVrIuMb@=n&U7+t][;,fDKY=0+b),HA;/'Kc5+46Foh+n:0Wo=$%7SgD-
    : MiFgQSPLJ:93n*"($p0M4D`T0bE8Hb/C@O`^[H6"I1F*P^'61m
    : (5Y7Hh@$J76L/7Ved7Sa9)m5t+EDG>C?Jp'-3hRRSqA`P!
    : K$eLmT!,1'?I#S7<[429uf01-[KV91H+0^1d9YkGlCdZ#rPb@ld2fV=!BW8HA3n:)1CE_bt%
    : Y3kkAjDu^#`s@]j-.,Lf",(u`n:?*o+^=sm.Jsb81D"Yi7iXc`W:O%?(5s@5o9tfZW )
    : QIiloSAs=:mLI<U1YH*We>,O5-UJ#g$NgA[1/R6!#GYUnG#IQQX/IQ mP:s?,)
    : 6:qMe7m).8GVlI)l#Mi%MMDhPF?]P2c6?Ns)m8R(fAY^]_^=s%$goi"WBo[?87&J[<*2$-f*7?bmC$<W~>
    : endstream
    : endobj
    : 6 0 obj
    : << /Type /Page
    : /Parent 1 0 R
    : /MediaBox [ 0 0 595 842 ]
    : /Resources 3 0 R
    : /Contents 5 0 R
    : >>
    : endobj
    : 7 0 obj
    : << /Type /Font
    : /Subtype /Type1
    : /Name /F1
    : /BaseFont /Helvetica
    : /Encoding /WinAnsiEncoding >>
    : endobj
    : 1 0 obj
    : << /Type /Pages
    : /Count 1
    : /Kids [6 0 R ] >>
    : endobj
    : 2 0 obj
    : << /Type /Catalog
    : /Pages 1 0 R
    : >>
    : endobj
    : 3 0 obj
    : <<
    : /Font << /F1 7 0 R >>
    : /ProcSet [ /PDF /ImageC /Text ] >>
    : endobj
    : xref
    : 0 8
    : 0000000000 65535 f
    : 0000001035 00000 n
    : 0000001093 00000 n
    : 0000001143 00000 n
    : 0000000015 00000 n
    : 0000000071 00000 n
    : 0000000822 00000 n
    : 0000000928 00000 n
    : trailer
    : <<
    : /Size 8
    : /Root 2 0 R
    : /Info 4 0 R
    : >>
    : startxref
    : 1221
    : %%EOF
    : [/code]
    :
    : First of all, notice the %PDF and %%EOF "tags"? If you just concatenate two PDFs together, the first %%EOF is going to stop any processing so you won't ever see the second. Secondly, do you see any text in there? The actual document looks something like this:
    :
    : [hr]
    : [blue][size=5]Extensible Markup Language (XML) 1.0[/size][/blue]
    :
    : The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML.
    :
    : The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML.
    : [hr]
    :
    : You don't see any of that text in the PDF because PDF content is in an indexed binary format. You can't just go around mashing them together all willy-nilly.
    :
    : That being said, I have heard that it is possible to accomplish what you are asking using Ghostscript and a special set of instructions, though I have never been able to figure it out.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :

  • [b][red]This message was edited by kel1981b at 2003-3-12 8:17:44[/red][/b][hr]
    : : Does anyone know how to combine two pdf's into one using visual basic? I did open both files and wrote it to a new file. but for some reason I can only see the first pdf, although the file new pdf is equal to first + second.
    :
    : It's not that simple. Open up a PDF in a text editor and you'll see what I mean.
    :
    Actually it's just a few lines of code ...
    [code]Private Sub Command1_Click()
    Dim retval
    Dim action As String, action2 As String
    'On Error GoTo ErrHandler

    GSCheck = True

    If Dir(App.Path & "Acrobat.ps") <> "" Then 'need to do that otwerwise Ghost run time error appears
    Kill (App.Path & "Acrobat.ps")
    End If

    If Dir(App.Path & "Reader.ps") <> "" Then
    Kill (App.Path & "Reader.ps")
    End If
    'the following conver first pdf file to ps format
    action = App.Path & "gswin32.exe" & _
    " -q -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=" & Chr(34) & _
    App.Path & "Acrobat.ps" & Chr(34) & " " & Chr(34) & "C:Program FilesAdobeAcrobat 5.0HelpENUACROBAT.PDF" & Chr(34)

    retval = Shell(action)

    'convert second pdf file to ps format

    action2 = App.Path & "gswin32.exe" & _
    " -q -dNOPAUSE -dBATCH -sDEVICE=pswrite -sOutputFile=" & Chr(34) & _
    App.Path & "Reader.ps" & Chr(34) & " " & Chr(34) & "C:Program FilesAdobeAcrobat 5.0HelpENUReader.pdf" & Chr(34)

    retval = Shell(action2)
    '
    'have to wait until ghost window becomes inactive
    Do Until GSCheck = False
    GSCheckWindow
    DoEvents
    Sleep (1300)
    Me.Refresh
    Loop

    'append two ps files using Binary Read tecnick

    sFirstFileName = App.Path & "Acrobat.ps"
    sSecondFileName = App.Path & "Reader.ps"
    sNewFileName = App.Path & "AcrobatAppend.ps"

    iFirstFile = FreeFile
    Open sFirstFileName For Binary Access Read As #iFirstFile
    iSecondFile = FreeFile
    Open sSecondFileName For Binary Access Read As #iSecondFile
    iNewFile = FreeFile

    'Create a New file
    Open sNewFileName For Binary Access Write As #iNewFile

    'Size the array to hold the file contents
    ReDim abBuffer(1 To LOF(iFirstFile))
    'Read the file contents
    Get #iFirstFile, , abBuffer
    Put #iNewFile, , abBuffer

    'Size the array to hold the file contents

    ReDim abBuffer(1 To LOF(iSecondFile))

    'Read the file contents
    Get #iSecondFile, , abBuffer
    Put #iNewFile, , abBuffer


    'print appended ps file which looks like pdf

    Dim sPrinter As String, sGhost

    sGhost = " -q -dNOPAUSE -dBATCH -sDEVICE="
    sPrinter = Printer.DeviceName

    action = "C:Program FilesGhostgumgsviewgsview32.exe " & _
    "-s" & sPrinter & " " & App.Path & "AcrobatAppend.ps"

    Text1 = action

    retval = Shell(action)

    Close #iSecondFile, #iNewFile, #iFirstFile
    End Sub[/code]


  • I do this almost daily. I use a [link=http://www.rasteredge.com/how-to/vb-net-imaging/pdf-processing/][color=Black]pdf processing programme[/color][/link] found on the internet . Install it and it becomes a selectable merger option.Then you can [link=http://www.rasteredge.com/how-to/vb-net-imaging/pdf-merge/][color=Black]merge PDFs into one[/color][/link] in any program at all, including Adobe Acrobat . Just open the PDFs, select merge, and choice the form you want, the task will be finished in several seconds. if you haven't found a good choice , you can have a try. best wishes.


  • Does anyone have any ideas about the PDF processor? I am testing with it about its PDF merging project these days. I want to look for some suggestion about it. Thanks in advance.

    Best regards,
    Arron

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories