How to Merge Multiple PDFs After Plotting in AutoCAD?

How to Merge Multiple PDFs After Plotting in AutoCAD?

sonchai_n
Contributor Contributor
3,362 Views
6 Replies
Message 1 of 7

How to Merge Multiple PDFs After Plotting in AutoCAD?

sonchai_n
Contributor
Contributor

Hi everyone,

I often need to plot multiple DWG files to individual PDF files using AutoCAD. After plotting, I want to combine these PDFs into a single PDF document.

Does AutoCAD provide any built-in library or feature to merge PDFs directly? Alternatively, is there a way to achieve this using VBA?

For example, I'm thinking of a VBA function like this:

 

Public Sub MergePDFs(inputFiles As Collection, outputFileName As String)
     ' Code to merge PDF files
End Sub

 

Sub TestMergePDFs()
    Dim inputFiles As Collection
    Dim outputFileName As String

    ' Initialize the collection
    Set inputFiles = New Collection

    ' Add input PDF files to the collection
    inputFiles.Add "C:\Path\To\File1.pdf"
    inputFiles.Add "C:\Path\To\File2.pdf"
    inputFiles.Add "C:\Path\To\File3.pdf"

    ' Specify the output PDF file name
    outputFileName = "C:\Path\To\MergedOutput.pdf"

    ' Call the MergePDFs function
    MergePDFs inputFiles, outputFileName
End Sub

Public Sub MergePDFs(inputFiles As Collection, outputFileName As String)
  ' Code to merge PDF files
End Sub

If anyone has experience with this or can share some sample VBA code or tools to handle this, I'd really appreciate it!

Thanks in advance for your help!

 

0 Likes
3,363 Views
6 Replies
Replies (6)
Message 2 of 7

Norman_Yuan
Mentor
Mentor

No, there is no built-in VBA function doing PDF merging. The only way of getting single PDF file of multi-page plotting is to use "Publish" command. With this command AutoCAD itself uses PDF merging library to do it. 

 

You need to find third party library for this. If you do AutoCAD .NET API, there are tons' of PDF manipulating tools/libraries available, many of them are free/open sourced. Unfortunately, there are not many fee ones for COM/VBA support - the world have moved on for newer technologies a long, long time ago.

 

Also, if you do AutoCAD API programming, the "PUBLISH" command actually has API support, meaning you can use code to control the publishing with extra flexibility and get a single PDF file of multiple pages of plotting without the need of third party library.  Again, VBA does not support publishing API.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 7

sonchai_n
Contributor
Contributor

Thank you for the detailed explanation!

For AutoCAD Electrical, is it possible to use VBA to iterate through the active projects one by one and then call the PUBLISH command for each project?

If VBA cannot directly call the PUBLISH command, is there any workaround or recommended method to automate this process in AutoCAD Electrical?

Thanks in advance for your guidance!

0 Likes
Message 4 of 7

baksconstructor
Advocate
Advocate

@sonchai_n wrote:

If VBA cannot directly call the PUBLISH command, is there any workaround or recommended method to automate this process in AutoCAD Electrical?


 

Check out this solution - Revers 
In the settings you can select a folder with files from which PDF files will be created.

0 Likes
Message 5 of 7

sonchai_n
Contributor
Contributor

Thank you for the suggestion!
Revers is indeed a good way to manage PDF files.

Could you please let me know more about KDMSoft.net? Specifically:

  • What programming language or platform was used to develop it?

Looking forward to your insights!

0 Likes
Message 6 of 7

thair_joudi
Explorer
Explorer

First, you need to install the PDFtk Server tool which is commonly used for client-side scripting or server-side processing of PDFs, you can download it from the official PDFtk website.

After installing PDFtk Server, you need to locate the executable file pdftk.exe on your computer, the default installation path is usually "C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe", but it might be different if you chose a custom installation directory.

Once you find the path, make a note of it, as you will need to update the script with this path.

 

Below is the VBA script that merges multiple PDFs into a single output file, it constructs the PDFtk command to merge the PDF files, runs the command using PowerShell and it was the only way I could accomplish handling file paths with spaces effectively.

 

Sub TestMergePDFs()
    Dim inputFiles As Collection
    Dim outputFileName As String

    ' Initialize the collection of input PDF files
    Set inputFiles = New Collection
    inputFiles.Add "D:\AutoCAD Drawings\Project01\MEP\HVAC\AC\PDFs\100.pdf"
    inputFiles.Add "D:\AutoCAD Drawings\Project01\MEP\HVAC\AC\PDFs\101.pdf"
    inputFiles.Add "D:\AutoCAD Drawings\Project01\MEP\HVAC\AC\PDFs\102.pdf"

    ' Specify the output PDF file name
    outputFileName = "D:\AutoCAD Drawings\Project01\MEP\HVAC\AC\PDFs\AC.pdf"

    ' Call the MergePDFs function
    MergePDFs inputFiles, outputFileName
End Sub

Public Sub MergePDFs(inputFiles As Collection, outputFileName As String)
    Dim DblQuote As String
    DblQuote = """" ' This is a single (") character

    ' Path to PDFtk executable - UPDATE THIS PATH
    Dim pdftkPath As String
    pdftkPath = "C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
    ' Define the PDFtk operation and output file path
    Dim Operations As String
    Operations = "cat output"

    ' Construct the argument string
    Dim ArgumentString As String
    Dim file As Variant
    For Each file In inputFiles
        ArgumentString = ArgumentString & " " & DblQuote & file & DblQuote
    Next file
    ArgumentString = ArgumentString & " " & Operations & " " & DblQuote & outputFileName & DblQuote

    ' Create the PowerShell script content
    Dim psScript As String
    psScript = "& '" & pdftkPath & "' " & ArgumentString

    ' Write the PowerShell script to a temporary file
    Dim ScriptFilePath As String
    ScriptFilePath = Environ("temp") & "\merge_pdfs.ps1"
    Dim ScriptFile As Integer
    ScriptFile = FreeFile
    Open ScriptFilePath For Output As ScriptFile
    Print #ScriptFile, psScript
    Close ScriptFile

    ' Run the PowerShell script
    Dim shell As Object
    Set shell = CreateObject("WScript.Shell")
    shell.Run "powershell.exe -File " & DblQuote & ScriptFilePath & DblQuote, 0, False ' 0 for hidden window, False to not wait for completion
End Sub

 

Happy merging!

0 Likes
Message 7 of 7

baksconstructor
Advocate
Advocate

@sonchai_n wrote:

Thank you for the suggestion!
Revers is indeed a good way to manage PDF files.

Could you please let me know more about KDMSoft.net? Specifically:

  • What programming language or platform was used to develop it?

Looking forward to your insights!


Perhaps you'd better write to the author.
As far as I understand, there is VB.net

0 Likes