Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic save as PDF all sheets

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
jamieking89
6953 Views, 17 Replies

iLogic save as PDF all sheets

Morning

 

I was hoping someone might help me with my issue of my ilogic code saving as PDF, it only prints the current sheet and not all. I tried adding a for loop in to run through all sheets but this didnt work. It manages to print multiple sheets in dwg format but not PDF

 

Any help is much appreciated.

 

Name = iProperties.Value("Project", "Part Number") & ".dwfx"  
ThisDoc.Document.SaveAs(Name,True)  


Name = ".dwg"  
ThisDoc.Document.SaveAs(ThisDoc.PathAndFileName(False) & Name,True)  


Name =iProperties.Value("Project","Part Number") & ".pdf"  
ThisDoc.Document.SaveAs(Name,True)  



MessageBox.Show("Export of dwfx, dwg & pdf file complete", "Complete")

 

 

Tags (3)
Labels (1)
17 REPLIES 17
Message 2 of 18
JhoelForshav
in reply to: jamieking89

Hi @jamieking89 

Do you want each sheet as a separate pdf or all sheets in a multisheet pdf?

Message 3 of 18
jamieking89
in reply to: JhoelForshav

hi @JhoelForshav 

 

I would like this to be a multi sheet pdf.

 

Your help would be much appreciated.

 

Cheers

Jamie

Message 4 of 18
ckeveryga
in reply to: jamieking89

If ThisDoc.Document.DocumentType <> kDrawingDocumentObject Then
	MessageBox.Show("This can only be run on drawing documents.", "Error")
	Exit Sub
End If

Try

Dim oDraw As DrawingDocument = ThisApplication.ActiveDocument
Dim oPDFAddIn As TranslatorAddIn = Nothing
Dim oNameValueMap As NameValueMap
Dim oContext As TranslationContext
Dim oDataMedium As DataMedium

Dim oPath As String

'Save to desktop
oPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)

Dim oFileName As String = ThisDoc.FileName(False) 'without extension

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

If oPDFAddIn Is Nothing Then
    MsgBox("The DWG add-in could not be found.")
    Exit Sub
End If

' Check to make sure the add-in is activated.
If Not oPDFAddIn.Activated Then oPDFAddIn.Activate()

oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
'oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

'Set the PDF target file name
'oDataMedium.FileName = oPath & "\" & oFileName & ".pdf"
oDataMedium.FileName = ThisDoc.PathAndFileName(False) & ".pdf"

'Publish document
Try
	oPDFAddIn.SaveCopyAs(oDraw, oContext, oOptions, oDataMedium)
Catch ex As Exception
	MsgBox("Failed To Export: " & FileName) 
End Try

MessageBox.Show("PDF can be found on Desktop", "Export Complete")

Catch ex As Exception
	MessageBox.Show(ex.Message)
End try
Message 5 of 18
jamieking89
in reply to: ckeveryga

Hi @ckeveryga 

 

Thats great, lengthy code but works as desired. I previously had issues using PDF translator in VBA so switched to using a simple ilogic rule, this seemed to work though.

 

Many thanks

Jamie

Message 6 of 18
WCrihfield
in reply to: ckeveryga

@ckeveryga 

It looks like the code you posted has a Try...Catch block inside of another Try...Catch block.  That's usually not good practice (if allowed at all).  You should only use Try...Catch blocks around minimal areas of code, where you know it might fail, and don't want the code to stop/error out.  Have you tested it?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 18
WCrihfield
in reply to: jamieking89

I would suggest also putting a check in there which checks to see if the PDF file already exists, and asks you if you want to write over it or not.  This is the code block I use within my PDF exporter.

'Check to see if the PDF already exists, if it does, ask if you want to over write existing or not.
If System.IO.File.Exists(oDataMedium.FileName) = True Then
	oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
	"Do you want to over write it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
	If oAnswer = vbNo Then Return
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 18
ckeveryga
in reply to: WCrihfield

@WCrihfield  It was old code that I've been using for a while so it appears it needs updating. You are correct, the outer try..catch can be removed, thanks. It has been tested and it does work.

Message 9 of 18
ABHUT_9090
in reply to: jamieking89

HII @ckeveryga 

 

Could you send me the final updated code for this problem, because i am having the same problem as mentioned in the Title and I am looking for the same answer "multi-sheet in one single .pdf file".

 

Thanks,

 

Message 10 of 18
WCrihfield
in reply to: ABHUT_9090

Hi @ABHUT_9090.  Actually exporting a multi-sheet drawing to a multi-page PDF is the normal process and the easiest to do.  Exporting each drawing sheet to separate PDF files actually requires slightly more work and/or code.

 

Here is an iLogic rule I have used for exporting a the currently 'active' drawing document out as a PDF file.  If the drawing has multiple sheets, then the resulting PDF will have multiple pages...one for each drawing sheet.  There are numerous versions of similar code out here on the forums though.  Many have special PDF file naming requirements though, and many different means are being used for assembling that new file name, which makes for so many different variations of the code.  This code (below) simply names the PDF file the exact same as the drawing file, just with the ".pdf" file extension, because that usually suits my needs just fine.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule only works on Drawing Documents.  Exiting Rule.", vbExclamation, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	ExportToPDF(oDDoc)
End Sub

Sub ExportToPDF(oDrawing As DrawingDocument)
	'get the PDF Translator Add-in
	Dim oPDFAddin As TranslatorAddIn
	For Each oAddin As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddin.DisplayName = "Translator: PDF" Then
			oPDFAddin = oAddin
		End If
	Next

	'Create the variables needed by the Translator Add-in
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium

	'same path and file name as drawing, but with .pdf file extension
	oNewFileName = System.IO.Path.ChangeExtension(oDrawing.FullFileName, ".pdf")
	'oDeskTopDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory)

	'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
	If System.IO.File.Exists(oNewFileName) = True Then
		oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAnswer = vbNo Then Exit Sub
	End If
	
	oDataMedium.FileName = oNewFileName
	
	'The following If-Then statement defines the Options available, and their Values.
	If oPDFAddin.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		'oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Custom_Begin_Sheet") = 1
		'oOptions.Value("Custom_End_Sheet") = 4
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
		oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
	End If

	'Publish PDF
	oPDFAddin.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 18
ABHUT_9090
in reply to: jamieking89

Hii, @WCrihfield 

 

Thanks you for sharing this code , I tried but this code only print one active sheet of the drawing. I want all the sheet as one .pdf file with many pages.

 

Thanks,

Message 12 of 18
StewartFuchs
in reply to: WCrihfield

Sorry to revamp an old thread. I've been using your code for a while. I'd now like to add functionality into the rule to add the drawing revision and description into the output pdf filename.  Eg. 12345-A123_A (Bracket A).pdf  I tried to modify the code, but it comes up with errors. What needs to be put into the rule to achieve this?

 

Thanks

Message 13 of 18
WCrihfield
in reply to: StewartFuchs

Hi @StewartFuchs.  Here is an altered version of the code you can try out.  I put the name assembly code in the main Sub, then supplied that new full file name to the export Sub, instead of doing all of that down within the export Sub, because I have found that pattern to allow the export routines to be more dynamically useful across the board.

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule only works on Drawing Documents.  Exiting Rule.", vbExclamation, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim sPathAndName As String = System.IO.Path.ChangeExtension(oDDoc.FullFileName, vbNullString)
	Dim sRev As String = oDDoc.PropertySets.Item(1).Item("Revision Number").Value
	Dim sDesc As String = oDDoc.PropertySets.Item(3).Item("Description").Value
	Dim sNewFullName As String = sPathAndName & "_" & sRev & " (" & sDesc & ").pdf"
	ExportToPDF(oDDoc, sNewFullName)
End Sub

Sub ExportToPDF(oDrawing As DrawingDocument, sNewFullFileName As String)
	Dim oPDF As TranslatorAddIn
	oPDF = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oContext As TranslationContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	Dim oOptions As NameValueMap = oTO.CreateNameValueMap
	Dim oDataMedium As DataMedium = oTO.CreateDataMedium
	If System.IO.File.Exists(sNewFullFileName) = True Then
		oAns = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = sNewFullFileName
	If oPDF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
		oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
	End If
	Try
		oPDF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	Catch e As Exception
		Logger.Error("Error using SaveCopyAs method." & vbCrLf & e.Message & vbCrLf & e.StackTrace)
	End Try
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 18
StewartFuchs
in reply to: WCrihfield

That works brilliantly. Thanks so much.  I did make a minor change though as shown below, as it didn't pick up the description from the drawing. 

 

StewartFuchs_0-1675207040857.png

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("This Rule only works on Drawing Documents.  Exiting Rule.", vbExclamation, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim sPathAndName As String = System.IO.Path.ChangeExtension(oDDoc.FullFileName, vbNullString)
	Dim sRev As String = oDDoc.PropertySets.Item(1).Item("Revision Number").Value
	Dim sDesc As String = oDDoc.PropertySets.Item(3).Item("Description").Value
	Dim sNewFullName As String = sPathAndName & "_" & sRev & " (" & iProperties.Value("Project", "Description") & ").pdf"
	ExportToPDF(oDDoc, sNewFullName)
End Sub

Sub ExportToPDF(oDrawing As DrawingDocument, sNewFullFileName As String)
	Dim oPDF As TranslatorAddIn
	oPDF = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oContext As TranslationContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	Dim oOptions As NameValueMap = oTO.CreateNameValueMap
	Dim oDataMedium As DataMedium = oTO.CreateDataMedium
	If System.IO.File.Exists(sNewFullFileName) = True Then
		oAns = MsgBox("A PDF file with this name already exists." & vbCrLf &
		"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
		If oAns = vbNo Then Exit Sub
	End If
	oDataMedium.FileName = sNewFullFileName
	If oPDF.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("Publish_All_Sheets") = 1 ' 0 = False, 1 = True
		oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
		oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
		oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
		oOptions.Value("Launch_Viewer") = 1 ' 0 = False, 1 = True
	End If
	Try
		oPDF.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	Catch E As Exception
		Logger.Error("Error using SaveCopyAs method." & vbCrLf & E.Message & vbCrLf & E.StackTrace)
	End Try
End Sub
Message 15 of 18
ejaL9K8H
in reply to: jamieking89

Hi

Is it possible to change the visibility of an layer to off, but still maintaining the possibility to turn it on in the PDF?

ejaL9K8H_0-1687172899524.png

This is a video of how it os done in Abobe Acrobat PRO: https://www.youtube.com/watch?v=9xVUeGu669Y

Best regards Emil  Jakobsen

 

 

Hello everyone, Today I will show you. How to change layer properties in pdf using Adobe Acrobat Pro DC. Facebook Page: https://goo.gl/mVvmvA https://goo.gl/FmZ84U Please, Subscribe to My Channel: https://www.youtube.com/channel/UCPn18CT3Dv5Y_GPVhFkFK5g/videos ...
Message 16 of 18
WCrihfield
in reply to: ejaL9K8H

Hi @ejaL9K8H.  I do not believe that would be possible.  I believe the layers must be 'On' and must contain some geometry, when the drawing is exported to PDF, for it to retain those layers within the exported PDF.  Once you have the exported PDF, you can control the visibility of those included layers as you see in the video.  There are not that many options available when exporting a PDF manually (Options dialog below), and although it seems like there may be more options when exporting by code using the TranslatorAddIn, most of the options you may have seen do not actually pertain to PDF's, and none of them seem to offer that level of control over the Layers.  I have attached a text file containing all of the 'option' names, and what their value types, and values are.  The values can all vary, of course.  And the value type shown an Enum variation is simply the Integer Value representation of the Enum variation, instead of including the name of the Enum, and its textual variation name.  One important thing to note though, is that the same TranslatorAddIn object used for exporting PDF's is also used for exporting DWF & DWFx file types, so most of the options listed are for use when exporting those other file types.  And there is a different ApplicationAddIn object used for 3D PDFs.

WCrihfield_0-1687181132009.png

You would likely have to make sure those layers were turned on, at least temporarily, just before exporting to PDF, then turn them back off.  That Inventor side process could all be taken care of by code, but editing the Layer settings within the exported PDF after the export is another matter.  I am not that familiar with controlling either the Adobe Reader or Adoby Accrobat Pro by code, other than maybe simply opening the PDF file, printing it, and closing it again, using the default shell application.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 17 of 18
ejaL9K8H
in reply to: WCrihfield

Thanks for a detailed answer.
I had the feeling that would not be possible.

Thanks again

- Emil Jakobsen
Message 18 of 18
eharrisonTZS2M
in reply to: ejaL9K8H

here's a code I used to export all my drawing's within an .idw file to PDF format, to the same folder location that the .idw file exists.

----------------------------------------------------------------------------------------------------------------------------

'this rule outputs all drawing sheets to pdf, 3D models of first sheet not included

oPath = ThisDoc.Path

docName = ThisDoc.FileName(False)
path_and_name = oPath & "\" & docName
DWFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If DWFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
oOptions.Value("Launch_Viewer") = launchviewer
oOptions.Value("Publish_All_Component_Props") = 1
oOptions.Value("Publish_All_Physical_Props") = 1
oOptions.Value("Password") = 0
If TypeOf oDocument Is DrawingDocument Then
Dim oSheets As NameValueMap
oSheets = ThisApplication.TransientObjects.CreateNameValueMap
'oOptions.Value("Publish_Mode") = DWFPublishModeEnum.kCustomDWFPublish
oOptions.Value("Publish_All_Sheets") = 1

End If
End If

oDataMedium.FileName = path_and_name & ".pdf"
Call DWFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
If launchviewer = 1 Then ThisDoc.Launch(path_and_name & ".pdf")

oDocument.Save()

 

-------------------------------------------------------------------

 

after sometime of only one sheet publishing, i went to manually export the file, and as @WCrihfield (i think) stated, i had to change the print range to "all sheets" in the dialogue box shown below. after that, it worked like a dream.

 

eharrisonTZS2M_0-1702474847036.png

 

(note: you can change the oPath at the top of the text file to any directory, then at the bottom, change the file extension to dwf to get the same operation in a dwf file format.

 

oPath = ThisDoc.Path

     eg. oPath = O:\Product Drawings

 

THEN...change .pdf to .dwf

 

 

oDataMedium.FileName = path_and_name & ".pdf"
Call DWFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
If launchviewer = 1 Then ThisDoc.Launch(path_and_name & ".pdf")

oDocument.Save()

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report