Microsoft Print To PDF - Set Paper Size
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to set the paper size when printing a PDF to Microsoft Print to PDF Printer. Code below. Code works and runs fine but the result PDF isn't created to the Paper size set in the code, it's created to the Paper size that is set as default when the drawing opens.
//Print to PDF
var pm = revitDoc.PrintManager;
pm.SelectNewPrintDriver("Microsoft Print to PDF");
pm.CombinedFile = true;
pm.PrintToFile = true;
pm.PrintToFileName = pdfFileName;
pm.PrintRange = PrintRange.Select;
//Get current print set up
var currentPrintSetup = pm.PrintSetup.CurrentPrintSetting;
//Set to in session set up so we can modify it
pm.PrintSetup.CurrentPrintSetting = pm.PrintSetup.InSession;
//Apply Settings
pm.Apply();
//Get and set the required paper size
PaperSize requiredPaperSize = null;
foreach (PaperSize existingPaperSize in pm.PaperSizes)
{
if (existingPaperSize.Name == printSize)
{
requiredPaperSize = existingPaperSize;
break;
}
}
if (requiredPaperSize == null) throw new Exception("Paper size " + printSize + " is not available for Microsoft Print To PDF");
pm.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize = requiredPaperSize;
//Apply Settings
pm.Apply();
var vSet = new ViewSet();
vSet.Insert(view);
var tr = new Transaction(revitDoc, "Print PDF");
tr.Start();
try
{
// Start timer to close print dialog when it shows
AutoEnterPrintDialog();
//Print
revitDoc.Print(vSet, true);
//Stop timer
plotTimer.Stop();
}
catch (Exception e)
{
throw e;
}
finally
{
// Set Print settings back to what they were before this command ran
pm.PrintSetup.CurrentPrintSetting = currentPrintSetup;
pm.Apply();
// Always commit transaction
tr.Commit();
}
//Return printed PDF
return pdfFileName;
}