By Wayne Brill
There is a VBA example that uses the PDF translator in the Inventor API help file. Here is an example in C++. You can replace the code in the SimpleExe SDK example to test this code. (At this location on my system)
"C:\Users\Public\Documents\Autodesk\Inventor 2014\SDK\DeveloperTools\Samples\VC++\Standalone Applications\Inventor\SimpleExe"
It creates a pdf for each sheet and one PDF with all the sheets.
<code_begin>
// SimpleExe.cpp : Defines the entry point for the console application.
#include "stdafx.h"
// Forward declarations
static HRESULT PrintPDF();
// Main. Note that all COM related activity (including the automatic 'release' within smart
// pointers) MUST take place BEFORE CoUnitialize(). Hence the function 'block' within which
// the smart-pointers construct and destruct (and AddRef and Release) keeping the CoUnitialize
// safely out of the way.
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT Result = NOERROR;
Result = CoInitialize (NULL);
if (SUCCEEDED(Result))
Result = PrintPDF();
CoUninitialize();
return 0;
}
static HRESULT PrintPDF()
{
HRESULT Result = NOERROR;
CLSID InvAppClsid;
Result = CLSIDFromProgID (L"Inventor.Application", &InvAppClsid);
if (FAILED(Result)) return Result;
CComPtr<IUnknown> pInvAppUnk;
Result = ::GetActiveObject (InvAppClsid, NULL, &pInvAppUnk);
if (FAILED (Result))
{
_tprintf_s (_T("*** Could not get hold of an active Inventor application ***\n"));
return Result;
}
CComPtr<Application> pInvApp;
Result = pInvAppUnk->QueryInterface (__uuidof(Application), (void **) &pInvApp);
if (FAILED(Result)) return Result;
CComPtr<Document> pDoc;
Result = pInvApp->get_ActiveDocument(&pDoc);
if (FAILED(Result)) return Result;
CComQIPtr<DrawingDocument> pDrawDoc = pDoc;
// Get the PDF translator Add-In.
CComPtr<ApplicationAddIns> pAddIns;
Result = pInvApp->get_ApplicationAddIns(&pAddIns);
CComPtr<ApplicationAddIn> pAddIn;
Result = pAddIns->get_ItemById(_bstr_t("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}"), &pAddIn);
CComQIPtr<TranslatorAddIn> pTransAddIn = pAddIn;
CComPtr<TransientObjects> pTransObjects;
Result = pInvApp->get_TransientObjects(&pTransObjects);
// Create the context.
CComPtr<TranslationContext> pContext;
Result = pTransObjects->CreateTranslationContext(&pContext);
Result = pContext->put_Type(kFileBrowseIOMechanism);
// Create a NameValueMap object
CComPtr<NameValueMap> pOptions;
Result = pTransObjects->CreateNameValueMap(&pOptions);
// Create a DataMedium object
CComPtr<DataMedium> pDataMedium;
Result = pTransObjects->CreateDataMedium(&pDataMedium);
// Check whether the translator has 'SaveCopyAs' options
VARIANT_BOOL hasOptions;
Result = pTransAddIn->get_HasSaveCopyAsOptions(pDrawDoc, pContext, pOptions, &hasOptions);
if (Result == S_OK)
{
CComPtr<Sheets>pSheets;
Result = pDrawDoc->get_Sheets(&pSheets);
long sheetCount;
Result = pSheets->get_Count(&sheetCount);
if (true)
{
for( int i=1; i<=sheetCount; ++i)
{
Result = pOptions->put_Value(_bstr_t("Vector_Resolution"), CComVariant(400));
Result = pOptions->put_Value(_bstr_t("Sheet_Range"), CComVariant(kPrintSheetRange));
Result = pOptions->put_Value(_bstr_t("Custom_Begin_Sheet"), CComVariant(i));
Result = pOptions->put_Value(_bstr_t("Custom_End_Sheet"), CComVariant(i));
//Set the destination file name
char filename[100];
sprintf_s(filename, "c:\\temp\\test%d.pdf", i);
pDataMedium->put_FileName(_bstr_t(filename));
// Publish document.
Result = pTransAddIn->SaveCopyAs(pDrawDoc, pContext, pOptions, pDataMedium);
}
Result = pOptions->put_Value(_bstr_t("Sheet_Range"), CComVariant(kPrintAllSheets));
Result = pOptions->put_Value(_bstr_t("Custom_Begin_Sheet"), CComVariant(1));
Result = pOptions->put_Value(_bstr_t("Custom_End_Sheet"), CComVariant(sheetCount));
pDataMedium->put_FileName(_bstr_t("c:\\temp\\testPDF_all_Sheets_in_one.pdf"));
// Publish document.
Result = pTransAddIn->SaveCopyAs(pDrawDoc, pContext, pOptions, pDataMedium);
}
else
{
Result = pOptions->put_Value(_bstr_t("Vector_Resolution"), CComVariant(400));
Result = pOptions->put_Value(_bstr_t("Sheet_Range"), CComVariant(kPrintSheetRange));
Result = pOptions->put_Value(_bstr_t("Custom_Begin_Sheet"), CComVariant(1));
Result = pOptions->put_Value(_bstr_t("Custom_End_Sheet"), CComVariant(sheetCount));
//Set the destination file name
char filename[500];
sprintf_s(filename, "c:\\temp\\testAll.pdf");
pDataMedium->put_FileName(_bstr_t(filename));
// Publish document.
Result = pTransAddIn->SaveCopyAs(pDrawDoc, pContext, pOptions, pDataMedium);
}
}
return Result;
}
<code_end>