[VB.net] Adobe Acrobat combined PDF

[VB.net] Adobe Acrobat combined PDF

ChristianAndersenIsmyname
Advocate Advocate
2,627 Views
10 Replies
Message 1 of 11

[VB.net] Adobe Acrobat combined PDF

ChristianAndersenIsmyname
Advocate
Advocate

Hello, I've searched the web but can't find any helpful example codes I can turn to fit my code.

 

What my code does right now:

  • Generates a list of all approved drawings in assembly
  • Creates PDFs of those drawings

The missing step is so combine those PDFs as one.

 

My code now:

Dim Acroapp As Acrobat.CAcroApp = CreateObject("AcroExch.App")
Dim files(ListBox4.Items.Count) As String
Dim Path As String = TextBox1.Text
 
Dim invapp As Inventor.Application
invapp = GetObject(, "Inventor.Application")
For Each itm In ListBox4.Items

   Dim x As Integer = ListBox4.Items.IndexOf(itm)
   invapp.Documents.Open(itm.ToString & "idw", True)
   invapp.ActiveDocument.SaveAs(Path & "\" & items.ToString & ".pdf", True)
   invapp.ActiveDocument.Close()
   files(x) = Path & " \ " & items.ToString & ".pdf"
Next

'Acroapp.saveas(files(), Path & "\CombinedFile.pdf") ' This is where I can't find any examples.
MsgBox("Completed")

 AcroApp doesn't show SaveAs as a option, so this is only to display what I want to achieve.

 

I've shortened the code to only show relevant info, and therefore some numbers/paths may be incorrect.

 

Thanks for any help!

0 Likes
Accepted solutions (1)
2,628 Views
10 Replies
Replies (10)
Message 2 of 11

yan.gauthier
Advocate
Advocate

In my C# Addin, I use IText so I am not 100% sure but I found this example :

 

Sub Button1_Click()

    Dim AcroApp As Acrobat.CAcroApp

    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc

    Dim numPages As Integer

    Set AcroApp = CreateObject("AcroExch.App")

    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")

    Part1Document.Open ("C:\temp\Part1.pdf")
    Part2Document.Open ("C:\temp\Part2.pdf")

    ' Insert the pages of Part2 after the end of Part1
    numPages = Part1Document.GetNumPages()

    If Part1Document.InsertPages(numPages - 1, Part2Document,
		0, Part2Document.GetNumPages(), True) = False Then
        MsgBox "Cannot insert pages"
    End If

    If Part1Document.Save(PDSaveFull, "C:\temp\MergedFile.pdf") = False Then
        MsgBox "Cannot save the modified document"
    End If

    Part1Document.Close
    Part2Document.Close

    AcroApp.Exit
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing

    MsgBox "Done"

End Sub

 

Link: http://khkonsulting.com/2009/03/adobe-acrobat-and-vba-an-introduction/

 

0 Likes
Message 3 of 11

ChristianAndersenIsmyname
Advocate
Advocate

I’ve seen this example, but from my understanding this is combining the two parts that are mentioned in the code. However, my list can have 150 PDFs, the amount of documents have to be dynamic.


But I might be able to use parts of the code, but I don’t understand which line generates the new merged file.

0 Likes
Message 4 of 11

CadUser46
Collaborator
Collaborator

Just a thought but have you considered zipping the files instead?

I don't know your workflow but an alternative approach may yield additional benefits like searching file names etc.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 5 of 11

ChristianAndersenIsmyname
Advocate
Advocate

Zipping isn’t a option. Reason is that this combined pdf will be sent to the mechanics who assemble the project.

The way we do it now is sorting out those 100 something files from a pdf folder (with a few thousand PDFs), and then combine the selected ones after we’ve sorted them numerically by drawing numbers. 


By doing this by code every step will be significantly time saved, because finding and sorting the correct drawings will be done automatically.

0 Likes
Message 6 of 11

yan.gauthier
Advocate
Advocate

what prevent you from building a list of all files and then execute to code like in the exemple ?

 

I would build an array of string representing the file path of the pdf and then simply run a for loop to add them to a newly created pdf. Insert them using your loop pointer.

 

set newPdfFile = 'Create a new pdf file (I Don't know how with acro)
Set PartDocument = CreateObject("AcroExch.PDDoc")

for i = 0 to files.count - 1
    PartDocument.open(files(i))
    newPdfFile.InsertPages(newPdfFile.GetNumPages(), PartDocument, 0, PartDocument.GetNumPages(),false)
    PartDocument.Close
next
0 Likes
Message 7 of 11

ChristianAndersenIsmyname
Advocate
Advocate
set newPdfFile = 'Create a new pdf file (I Don't know how with acro)

 This line would be the solution I think. I havent seen any examples where something similar to this has been used.

It's basically what I have in my original post, instead of Arcobat.SaveAs(something), this line above could be used instead.

0 Likes
Message 8 of 11

I've made a some progress. Issue now is that it doesn't insert the pages to the PDF I've created. Hopefully someone can help me with this.

newPDF.InsertPages(-1, Line(5) & "\Combined " & asmpartnum.Value & "\" & PDFname, 1, 1, 0)

My code now creates the new PDF, and find all the PDFs that should be inserted into the new one.

It runs through everything. If I add a messagebox after the line above, it shows up.

Line(4) and Line(5) is paths I've grabbed from a txtfile. For example: C:\TEMP

 

Full code here:

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Dim invapp As Inventor.Application
        invapp = GetObject(, "Inventor.Application")
        Dim asmDoc As AssemblyDocument
        asmDoc = invapp.ActiveDocument
        Dim asmpartnum As Inventor.Property = Trim(asmDoc.PropertySets("Design Tracking Properties").Item("Part Number"))
        Dim PDFNewName As String
        Dim newPDF As Acrobat.CAcroPDDoc
        Dim PDFPath As String

        PDFPath = TextBox1.Text
        PDFNewName = PDFPath & "\Combined all files " & asmpartnum.Value & ".pdf"
        newPDF = CreateObject(“AcroExch.pdDoc”)
        newPDF.Create()
        newPDF.Open(PDFNewName)

        For Each items In ListBox1.Items
            For Each Doc In asmDoc.AllReferencedDocuments
                If Doc.displayname = items Then
                    Dim drawing As String = Doc.FullFileName
                    drawing = drawing.Substring(0, drawing.Length - 3)
                    Dim Drawingdocument As DrawingDocument = invapp.Documents.Open(drawing & "idw", False)

                    Dim PartnumberProp As Inventor.Property = Trim(Doc.PropertySets("Design Tracking Properties").Item("Part Number"))
                    Dim RevisionNumberProp As Inventor.Property = Drawingdocument.PropertySets("Summary Information").Item("Revision Number")
                    Dim PDFname As String = Trim(PartnumberProp.Value) & "-" & RevisionNumberProp.Value & ".pdf"

                    Drawingdocument.Close()

                    If System.IO.File.Exists(Line(4) & "\" & PDFname) Then
                        Dim fi As New System.IO.FileInfo(Line(4) & "\" & PDFname)
                        fi.CopyTo(Line(5) & "\Combined " & asmpartnum.Value & "\" & PDFname)
                        newPDF.InsertPages(-1, Line(5) & "\Combined " & asmpartnum.Value & "\" & PDFname, 1, 1, 0)
                    End If
                End If
            Next
        Next

        newPDF.Save(1, PDFNewName)
        newPDF.Close()
        newPDF = Nothing
        MsgBox("Complete")
    End Sub

 

0 Likes
Message 9 of 11

yan.gauthier
Advocate
Advocate
Accepted solution

This line need to be different:

 

 newPDF.InsertPages(-1, Line(5) & "\Combined " & asmpartnum.Value & "\" & PDFname, 1, 1, 0)

 

From the Adobe Api reference: 

 

InsertPages(nInsertPageAfter, IPDDocSource, nStartPage, nNumPages, bBookmarks)

 

first doc page is 0, so it should be:

 

 MergePDFDocument.InsertPages(MergeDocumentLastPage - 1, SourceDocument, 0, SourceDocumentPageCount -1, false)

 

thus why you need to open the pdf files to know how many pages they contain.

 

Message 10 of 11

ChristianAndersenIsmyname
Advocate
Advocate

I'm now counting the pages of the new pdf, and the pdf it shall insert.

newPDF.InsertPages(newPages - 1, currentpdf, 0, currentPages - 1, False)

The code below does exactly the same as the code in my previous post. It creates the pdf but are unable to insert.

I think I understood what you ment, and it makes sense.

 

Thank you so much for the time you try to help me out.

When I get this code to work, I'll buy you a beer 😁

 

        Dim invapp As Inventor.Application
        invapp = GetObject(, "Inventor.Application")
        Dim asmDoc As AssemblyDocument
        asmDoc = invapp.ActiveDocument
        Dim asmpartnum As Inventor.Property = asmDoc.PropertySets("Design Tracking Properties").Item("Part Number")
        If System.IO.Directory.Exists(Line(5) & "\Combined " & asmpartnum.Value) Then System.IO.Directory.Delete(Line(5) & "\Combined " & asmpartnum.Value, True)
        System.IO.Directory.CreateDirectory(Line(5) & "\Combined " & asmpartnum.Value)
        Dim PDFNewName As String
        Dim newPDF As Acrobat.CAcroPDDoc
        Dim PDFPath As String
        Dim newPages As Integer
        Dim currentPages As Integer
        PDFPath = TextBox1.Text
        PDFNewName = PDFPath & "\Combined all files " & asmpartnum.Value & ".pdf"
        newPDF = CreateObject(“AcroExch.pdDoc”)
        newPDF.Create()

        For Each itm In ListBox4.Items
            Dim x As Integer = ListBox4.Items.IndexOf(itm)
            For Each items In ListBox1.Items
                Dim y As Integer = ListBox1.Items.IndexOf(items)
                If x = y Then
                    For Each Doc In asmDoc.AllReferencedDocuments
                        If Doc.displayname = items Then

                            Dim drawing As String = Doc.FullFileName
                            drawing = drawing.Substring(0, drawing.Length - 3)
                            Dim Drawingdocument As DrawingDocument = invapp.Documents.Open(drawing & "idw", False)

                            Dim PartnumberProp As Inventor.Property = Doc.PropertySets("Design Tracking Properties").Item("Part Number")
                            Dim RevisionNumberProp As Inventor.Property = Drawingdocument.PropertySets("Summary Information").Item("Revision Number")
                            Dim PDFname As String = Trim(PartnumberProp.Value) & "-" & RevisionNumberProp.Value & ".pdf"

                            Drawingdocument.Close()
                            Dim sourcePDF As String = Line(4) & "\" & PDFname
                            Dim destinationPDF As String = Line(5) & "\Combined " & asmpartnum.Value & "\" & PDFname
                            If System.IO.File.Exists(sourcePDF) Then
                                newPDF.Open(PDFNewName)
                                newPages = newPDF.GetNumPages()
                                Dim currentpdf As Acrobat.CAcroPDDoc
                                currentpdf = CreateObject(“AcroExch.pdDoc”)
                                currentpdf.Open(destinationPDF)
                                currentPages = currentpdf.GetNumPages()

                                newPDF.InsertPages(newPages - 1, currentpdf, 0, currentPages - 1, False)

                                currentpdf.Close()
                                Dim fi As New System.IO.FileInfo(sourcePDF)
                                fi.CopyTo(destinationPDF)
                                newPDF.Save(1, PDFNewName)
                                newPDF.Close()
                            End If
                        End If
                    Next
                End If
            Next
        Next
        newPDF = Nothing
        MsgBox("Complete")

 

0 Likes
Message 11 of 11

Finally got it working!

 

Thanks a lot for all your help @yan.gauthier !

        ' Get inventor app
        Dim invapp As Inventor.Application
        invapp = GetObject(, "Inventor.Application")
        ' Get assembly
        Dim asmDoc As AssemblyDocument
        asmDoc = invapp.ActiveDocument
        Dim asmpartnum As Inventor.Property = asmDoc.PropertySets("Design Tracking Properties").Item("Part Number")
        ' Creating directory
        If Not System.IO.Directory.Exists(TextBox1.Text) Then System.IO.Directory.CreateDirectory(TextBox1.Text)
        ' Grabbing all the info I need
        Dim PDFNewName As String
        Dim newPDF As Acrobat.CAcroPDDoc
        Dim PDFPath As String
        Dim newPages As Integer
        Dim currentPages As Integer
        PDFPath = TextBox1.Text
        PDFNewName = PDFPath & "\Combined all files " & asmpartnum.Value & ".pdf"
        newPDF = CreateObject("AcroExch.pdDoc")
        ' Check if PDF already exists
        If System.IO.File.Exists(PDFNewName) Then System.IO.File.Delete(PDFNewName)
        newPDF.Create()
        newPDF.Open(PDFNewName)

        ' Get listbox4 values (fullfilepaths without extensions)
        For Each itm In ListBox4.Items
            Dim x As Integer = ListBox4.Items.IndexOf(itm)
            For Each items In ListBox1.Items
                Dim y As Integer = ListBox1.Items.IndexOf(items)
                If x = y Then
                    ' Get document in listbox to check iProperties
                    For Each Doc In asmDoc.AllReferencedDocuments
                        If Doc.displayname = items Then
                            Dim drawing As String = Doc.FullFileName
                            drawing = drawing.Substring(0, drawing.Length - 3)
                            Dim Drawingdocument As DrawingDocument = invapp.Documents.Open(drawing & "idw", False)
                            Dim PartnumberProp As Inventor.Property = Doc.PropertySets("Design Tracking Properties").Item("Part Number")
                            Dim RevisionNumberProp As Inventor.Property = Drawingdocument.PropertySets("Summary Information").Item("Revision Number")
                            Dim PDFname As String = Trim(PartnumberProp.Value) & "-" & RevisionNumberProp.Value & ".pdf"
                            Drawingdocument.Close()
                            ' Check if PDF of drawing already exists
                            Dim sourcePDF As String = Line(4) & "\" & PDFname
                            If System.IO.File.Exists(sourcePDF) Then
                                ' Adds existing PDF to combined PDF
                                newPages = newPDF.GetNumPages()
                                Dim currentpdf As Acrobat.CAcroPDDoc
                                currentpdf = CreateObject("AcroExch.pdDoc")
                                currentpdf.Open(sourcePDF)
                                currentPages = currentpdf.GetNumPages()
                                newPDF.InsertPages(newPages - 1, currentpdf, 0, currentPages - 1, True)
                                currentpdf.Close()
                            End If
                        End If
                    Next
                End If
            Next
        Next
        newPDF.Save(1, PDFNewName)
        newPDF.Close()
        newPDF = Nothing
        MsgBox("Complete")

 

0 Likes