Merging PDFs using iLogic

Merging PDFs using iLogic

Anonymous
Not applicable
3,293 Views
25 Replies
Message 1 of 26

Merging PDFs using iLogic

Anonymous
Not applicable

Using iLogic, I am cycling through a user defined-folder of idws to update their title blocks and borders, update a few iProperties from a user-defined Excel file, and printing a PDF of the whole drawing.

 

What I would like to do is create a master PDF that I could append each created PDF print to.

 

Is this even possible through iLogic?  If so, how would I approach this?

 

Thank you,

Brian Daley...

0 Likes
3,294 Views
25 Replies
Replies (25)
Message 2 of 26

Curtis_Waguespack
Consultant
Consultant

Hi briandaley,

 

I think you'd need to use something Adobe or DynamicPDF and call it to do the merging, as in this example:

http://www.dynamicpdf.com/Products/DynamicPDF/Examples/Web_VB/MergerExamples/SourceView.aspx?class=M...

 

You can search "Merge PDF vb.net" also and you might find other information.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 3 of 26

NachoShaw
Advisor
Advisor
Hi

There is a dll that you can reference that will create a single pdfs out of a folder of pdfs. I can't remember the name right now but I'll look it up out of my files tomorrow.

I used it for a similar process. I set it up to create a pdf just before I wanted the report so that I got the latest files (and new ones added / removed ones deleted). It's a pretty good tool, I'll post some usage code too.

Only thing I'm not sure of is if ilogic works with you. I'd reference it in vba or better still, vb.net


Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 4 of 26

w.pepping
Advocate
Advocate

After you have stored the pdf's in a folder you can merge them with itextsharp.dll.

Get control of your sheet metal files.
Download the app from matprop.com
0 Likes
Message 5 of 26

NachoShaw
Advisor
Advisor

yep,

 

that's the dll I was referring to. I will post some sample code later for usage. Not sure if you can use it in iLogic though

 

 

Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 6 of 26

frederic.vandenplas
Collaborator
Collaborator

@Anonymous

 

Have a look at this thread, i've attached a sample .NET program which merges pdf using the itextsharp pdf library

 

http://forums.autodesk.com/t5/inventor-customization/pdf3d-2d/td-p/6756605

 

You can write an exernal exe, pass parameters to it from within ilogic using shell or something like that.

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 7 of 26

Anonymous
Not applicable

I appreciate the responses but due to policy restrictions, I cannot download any third-party applications to use with Inventor.  I'm not even able to get the free program, Visual Studio Code, to have access to the API.

 

My goal was to hopefully find a way to call either Adobe or CutePDF to do the legwork for me.

 

My current PDF printing is done by

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
Sub PDFPrinter(oDoc, oPDFAddIn)
'Saves a PDF in current folder					
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
If oPDFAddIn.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 1
	oOptions.Value("All_Color_As_Black") = False
	oOptions.Value("Remove_Line_Weights") = False
	oOptions.Value("Vector_Resolution") = 2400
					
	oContext.Type = kFileBrowseIOMechanism
	Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
	Dim pdfFilePath
	pdfFilePath = oDoc.FullFileName
	pdfFilePath = Left(pdfFilePath, Len(pdfFilePath)-4)
	oData.FileName = pdfFilePath & ".pdf"
					
	Call oPDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oData)
Else
End If

'Prints through default printer
'Dim oPrintMgr As PrintManager
'oPrintMgr = oDoc.PrintManager
'oPrintMgr.ScaleMode = kCustomScale
'oPrintMgr.ScaleMode = PrintScaleModeEnum.kPrintBestFitScale
'oPrintMgr.PrintRange = kPrintAllSheets
'oPrintMgr.NumberofCopies = 1
'oPrintMgr.ColorMode = kPrintDefaultColorMode
'oPrintMgr.Orientation = kLandscapeOrientation
'oPrintMgr.PaperSize = SizeActiveSheet
'oPrintMgr.SubmitPrint

'Programming by Brian Daley...
End Sub

I cycle through a whole folder using this program to do a few things to each file.  This is driven by a list in an Excel spreadsheet.  If I could just have each pdf append to the end of a master pdf, that would be perfect.

 

0 Likes
Message 8 of 26

Anonymous
Not applicable

So there is no solution without downloading a third party app?

0 Likes
Message 9 of 26

GeorgK
Advisor
Advisor
0 Likes
Message 10 of 26

Anonymous
Not applicable

Correct me if I'm wrong but both of those links appear to rely on iTextSharp to make the code function.

0 Likes
Message 11 of 26

NachoShaw
Advisor
Advisor
You are going to struggle to do it without some sort of 3rd party application. Inventor and vba doesn't do this afaik. I texts harp is a good tool although You can't reference it from vba.

Can't you get vs at home, do it there as the exe and then run that at work?

You could always make a vb.net (not c#) wrapper that references I texts itextsharp and then reference that dll in vba? They will be 2 dlls not exe's so effectively, they don't run as such like exe's do. Just an idea


Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 12 of 26

WilliamBellE6H7K
Participant
Participant

Hey mate, can you please post this code you where refering to

0 Likes
Message 13 of 26

NachoShaw
Advisor
Advisor

Hi

 

Attached is the iTextSharp.dll reference needed. I made a class a long time ago and converted it iLogic. you shouldnt need to alter any of the code in the MergePDF class, just adjust the rows in the Main Sub at the top.

 

Place the itextsharp.dll in a local place, mine was placed here

 

C:\Users\Public\global references

 

here is the iLogic code

AddReference "C:\Users\Public\global references\itextsharp.dll"
addreference "System.IO"
Sub Main()

	'set the path of the folder contaiing all of the pdfs
	Dim _Path As String = "C:\MyPath\MyPDF Collection"

	'set the name of the new pdf you want to create without the extension
	Dim _Name As String = "MyNewPDF"

	'get the result, it will be either the path of the PDF or FALSE
	Dim MakePDF As New MergePDF(_Path, _Name)

	'get the PDF result
	Dim OpenPDF As String = MakePDF.GetPDF

	'open on condition to the result
	Select Case OpenPDF
		Case "FALSE"
			MsgBox("No PDF was generated")
		Case Else
			Process.Start(OpenPDF)
	End Select

End Sub

Public Class MergePDF


#Region "PRIVATE PROPERTIES"
    Private Property _Folder_Path As String = String.Empty
    Private Property _PDF_Name As String = Nothing
    Private Property _GetPDF As String = String.Empty
#End Region

#Region "FRIEND PROPERTIES THAT YOU CALL BACK"
    Friend Property GetPDF As String
        Set(value As String)
            _GetPDF = value
        End Set
        Get
            Return _GetPDF
        End Get
    End Property

#End Region

#Region "CONSTRUCTORS"
    Public Sub New(ByVal _folderpath As String, ByVal _pdfname As String)

        'set the pdf Folder path
        _Folder_Path = _folderpath

        'set the pdf name
        _PDF_Name = _pdfname

        'create the PDF and set the path
        _GetPDF = CreatePDF(_Folder_Path)
    End Sub

#End Region

#Region "HELPERS"
    Private Function CreatePDF(ByVal sFolderPath) As String

        Dim bOutputfileAlreadyExists As Boolean = False
        Dim sOutFilePath As String = IO.Path.Combine(sFolderPath, _PDFName & ".pdf")

        'sdet up return for a successful pdf. ret changes to FALSE if any errors occur for qualifying purposes
        Dim ret As String = sOutFilePath

        If IO.File.Exists(sOutFilePath) Then
            Try
                IO.File.Delete(sOutFilePath)
            Catch ex As Exception
                bOutputfileAlreadyExists = True
            End Try
        End If

        Dim iPageCount As Integer = GetPageCount(sFolderPath)
        If iPageCount > 0 And bOutputfileAlreadyExists = False Then

            Dim oFiles As String() = IO.Directory.GetFiles(sFolderPath)
            Dim oPdfDoc As New iTextSharp.text.Document()
            Dim oPdfWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(oPdfDoc, New IO.FileStream(sOutFilePath, IO.FileMode.Create))

            oPdfDoc.Open()

            System.Array.Sort(Of String)(oFiles)

            For i As Integer = 0 To oFiles.Length - 1
                Dim sFromFilePath As String = oFiles(i)
                Dim oFileInfo As New IO.FileInfo(sFromFilePath)
                Dim sFileType As String = "PDF"
                Dim sExt As String = PadExt(oFileInfo.Extension)

                Try
                    AddPdf(sFromFilePath, oPdfDoc, oPdfWriter)
                Catch ex As Exception
                    ret = "FALSE"
                End Try
            Next

            Try
                oPdfDoc.Close()
                oPdfWriter.Close()
            Catch ex As Exception

                Try
                    IO.File.Delete(sOutFilePath)
                Catch ex2 As Exception
                End Try
            End Try
        End If

        Dim oFolders As String() = IO.Directory.GetDirectories(sFolderPath)
        For i As Integer = 0 To oFolders.Length - 1
            Dim sChildFolder As String = oFolders(i)
            Dim iPos As Integer = sChildFolder.LastIndexOf("\")
            Dim sFolderName As String = sChildFolder.Substring(iPos + 1)
            CreatePDF(sChildFolder)
        Next

        Return ret

    End Function

    Private Sub AddPdf(ByVal sInFilePath As String, ByRef oPdfDoc As iTextSharp.text.Document, ByRef oPdfWriter As iTextSharp.text.pdf.PdfWriter)

        Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
        Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
        Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
        Dim iPage As Integer = 0

        Do While (iPage < iNumberOfPages)
            iPage += 1
            oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
            oPdfDoc.NewPage()

            Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
            Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
            If (iRotation = 90) Or (iRotation = 270) Then
                oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
            Else
                oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
            End If
        Loop

    End Sub

    Private Function PadExt(ByVal s As String) As String
        s = UCase(s)
        If s.Length > 3 Then
            s = s.Substring(1, 3)
        End If
        Return s
    End Function

   Private Function GetPageCount(ByVal sFolderPath As String) As Integer
        Dim iRet As Integer = 0
        Dim oFiles As String() = IO.Directory.GetFiles(sFolderPath)

        For i As Integer = 0 To oFiles.Length - 1
            Dim sFromFilePath As String = oFiles(i)
            Dim oFileInfo As New IO.FileInfo(sFromFilePath)
            Dim sFileType As String = "PDF"
            Dim sExt As String = PadExt(oFileInfo.Extension)

            iRet += 1
        Next

        Return iRet
    End Function
#End Region

End Class

 

 

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


Message 14 of 26

WilliamBellE6H7K
Participant
Participant

Thanks for that mate, worked great

0 Likes
Message 15 of 26

WilliamBellE6H7K
Participant
Participant

Hi Mate

 

What the best way to change to PDF name to a variable that's used else ware in the code. Ive tried a few different ways with no luck as alot of the rule is private functions.

0 Likes
Message 16 of 26

NachoShaw
Advisor
Advisor

Hey

 

Glad it worked. In the Sub Main() iLogic code, this line-

 

'set the name of the new pdf you want to create without the extension
Dim _Name As String = "MyNewPDF"

sets the name of the PDF. You can capture the variable value of the name you need first and set it here to change the PDF name.

 

Thanks

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 17 of 26

emanuel.c
Collaborator
Collaborator

Hi @NachoShaw 

Since upgrading to Inventor 2025.2 I get an error when running this code, here:

 

emanuelc_0-1734106867383.png

Can you help me with this?

Thank you very much!

0 Likes
Message 18 of 26

g.georgiades
Advocate
Advocate

In the Addreference section at the top of the ilgoic rule, add this line to it

 

AddReference "System.Private.Uri"
Message 19 of 26

emanuel.c
Collaborator
Collaborator

Thanks for the input @g.georgiades 

It fixed that issue but here is a next thing popping up...

emanuelc_0-1734108691274.png

 

0 Likes
Message 20 of 26

NachoShaw
Advisor
Advisor

2025 is using NET 8 vs 2024 that is using .Net Framework 4.8 so they are different. It looks like you cant directly reference .Net Framework 4.8 in a NET 8 project as they are different runtimes. your options may be to create a NET 8 wrapper to control the pdf methods and reference that into iLogic but im not 100% on that.

 

There is a method shared by @JeltDeJong that handles both frameworks within a single project, maybe that might help? Here is the link to Jelte de Jong site

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes