Hello friends,
need your help to make a customization in C # to print PDF documents.
I tried to modify the sample standard API this link:
But no success, below is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.PlottingServices;
using System.Collections.Specialized;
namespace Plot
{
public class plotter
{
[CommandMethod ("Simplot" CommandFlags.Session)]
public void Print ()
{
short bgPlot =
(Short) Application.GetSystemVariable ("BACKGROUNDPLOT");
/ / Set the BACKGROUNDPLOT = 0 temporarily.
Application.SetSystemVariable ("BACKGROUNDPLOT", 0);
/ / Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database = acCurDb acDoc.Database;
acDoc.LockDocument ();
using (Transaction acTrans acCurDb.TransactionManager.StartTransaction = ())
{
/ / Reference the Layout Manager
LayoutManager acLayoutMgr;
acLayoutMgr = LayoutManager.Current;
/ / Get the current layout and output its name in the Command Line window
Layout acLayout;
acLayout = acTrans.GetObject (acLayoutMgr.GetLayoutId (acLayoutMgr.CurrentLayout)
OpenMode.ForRead) the layout;
/ / Get the PlotInfo from the layout
PlotInfo acPlInfo PlotInfo = new ();
acPlInfo.Layout = acLayout.ObjectId;
/ / Get a copy of the layout from the PlotSettings
PlotSettings acPlSet = new PlotSettings (acLayout.ModelType);
acPlSet.CopyFrom (acLayout);
/ / Update the object PlotSettings
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
/ / Set the plot type
acPlSetVdr.SetPlotType (acPlSet,
Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
/ / Set the plot scale
acPlSetVdr.SetUseStandardScale (acPlSet, true);
acPlSetVdr.SetStdScaleType (acPlSet, StdScaleType.ScaleToFit);
/ / Center the plot
acPlSetVdr.SetPlotCentered (acPlSet, true);
/ / Set the plot device to use
/ / AcPlSetVdr.SetPlotConfigurationName (acPlSet "Pdfcreator", "A3");
acPlSetVdr.SetPlotConfigurationName (acPlSet "DWG to PDF.pc3", "ISO_A3_ (297.00_x_430.00_mm)");
/ / Set the plot info to an override since it will
/ / Not be saved back to the layout
acPlInfo.OverrideSettings = acPlSet;
/ / Validate the plot info
PlotInfoValidator acPlInfoVdr PlotInfoValidator = new ();
acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
acPlInfoVdr.Validate (acPlInfo);
/ / Check to see if the plot is already in progress
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine acPlEng PlotFactory.CreatePublishEngine = ())
{
/ / Track the progress with the plot Progress dialog
PlotProgressDialog acPlProgDlg = new PlotProgressDialog (false,
1
true);
using (acPlProgDlg)
{
/ / Sets the status messages to display When plotting starts
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.DialogTitle,
"Plot Progress");
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job");
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet");
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress");
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.SheetProgressCaption,
"Progress Sheet");
/ / Set the plot progress range
acPlProgDlg.LowerPlotProgressRange = 0;
acPlProgDlg.UpperPlotProgressRange = 100;
acPlProgDlg.PlotProgressPos = 0;
/ / Display the Progress dialog
acPlProgDlg.OnBeginPlot ();
acPlProgDlg.IsVisible = true;
/ / Start to plot the layout
acPlEng.BeginPlot (acPlProgDlg, null);
/ / Define the plot output
acPlEng.BeginDocument (acPlInfo,
acDoc.Name,
null
1
true,
"C: \ \ MMID");
/ / Display information about the current plot
acPlProgDlg.set_PlotMsgString (PlotMessageIndex.Status,
"Plotting" + acDoc.Name + "-" +
acLayout.LayoutName);
/ / Set the sheet progress range
acPlProgDlg.OnBeginSheet ();
acPlProgDlg.LowerSheetProgressRange = 0;
acPlProgDlg.UpperSheetProgressRange = 100;
acPlProgDlg.SheetProgressPos = 0;
/ / Plot the first sheet / layout
PlotPageInfo acPlPageInfo PlotPageInfo = new ();
acPlEng.BeginPage (acPlPageInfo,
acPlInfo,
true,
null);
acPlEng.BeginGenerateGraphics (null);
acPlEng.EndGenerateGraphics (null);
/ / Finish plotting the sheet / layout
acPlEng.EndPage (null);
acPlProgDlg.SheetProgressPos = 100;
acPlProgDlg.OnEndSheet ();
/ / Finish plotting the document
acPlEng.EndDocument (null);
/ / Finish the plot
acPlProgDlg.PlotProgressPos = 100;
acPlProgDlg.OnEndPlot ();
acPlEng.EndPlot (null);
/ / Re-set the original value of BACKGROUNDPLOT.
Application.SetSystemVariable ("BACKGROUNDPLOT"
bgPlot);
}
}
}
}
}
}
}
But eInvalidInput error occurs in line with the excerpt:
acPlSetVdr.SetPlotConfigurationName (acPlSet "DWG to PDF.pc3", "ISO_A3_ (297.00_x_430.00_mm)");
I believe that is the name of the printer or media format. Also tried with PDFCreator, but it generates a plt file instead of a PDF.
From already thank you ..
Hi,
Maybe you can get some inspiration from this one:
hello,
definitely got some inspiration 😃
The code below takes an object of type Document and print the PDF of the same model.
The only thing missing is a pdf generated rotate to landscape ... if anyone knows and can share would be grateful
thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.PlottingServices;
using System.Collections.Specialized;
using Autodesk.AutoCAD.Publishing;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace FabricaDocumentos
{
public class Ploter
{
public void Imprimir(Document doc)
{
short bgPlot =
(short)Application.GetSystemVariable("BACKGROUNDPLOT");
//set the BACKGROUNDPLOT = 0 temporarily.
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
// Get the current document and database, and start a transaction
Document acDoc = doc;
Database acCurDb = acDoc.Database;
acDoc.LockDocument();
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Reference the Layout Manager
LayoutManager acLayoutMgr = LayoutManager.Current;
// Get the current layout and output its name in the Command Line window
Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead) as Layout;
// Get the PlotInfo from the layout
PlotInfo acPlInfo = new PlotInfo();
acPlInfo.Layout = acLayout.ObjectId;
// Get a copy of the PlotSettings from the layout
PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
acPlSet.CopyFrom(acLayout);
// Update the PlotSettings object
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
// Set the plot type
acPlSetVdr.SetPlotType(acPlSet,
Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
// Set the plot scale
acPlSetVdr.SetUseStandardScale(acPlSet, true);
acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
// Center the plot
acPlSetVdr.SetPlotCentered(acPlSet, true);
//Rotation
//acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees270);
// Set the plot device to use
acPlSetVdr.SetPlotConfigurationName(acPlSet, "PDFCreator", "A3");
//acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWG To PDF.pc3", "ISO_A3_(297.00_x_430.00_mm)");
// Set the plot info as an override since it will
// not be saved back to the layout
acPlInfo.OverrideSettings = acPlSet;
// Validate the plot info
PlotInfoValidator acPlInfoVdr = new PlotInfoValidator();
acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
acPlInfoVdr.Validate(acPlInfo);
// Check to see if a plot is already in progress
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
{
// Track the plot progress with a Progress dialog
PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false,
1,
true);
using (acPlProgDlg)
{
// Define the status messages to display when plotting starts
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle,
"Plot Progress");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption,
"Sheet Progress");
// Set the plot progress range
acPlProgDlg.LowerPlotProgressRange = 0;
acPlProgDlg.UpperPlotProgressRange = 100;
acPlProgDlg.PlotProgressPos = 0;
// Display the Progress dialog
acPlProgDlg.OnBeginPlot();
acPlProgDlg.IsVisible = true;
// Start to plot the layout
acPlEng.BeginPlot(acPlProgDlg, null);
// Define the plot output
acPlEng.BeginDocument(acPlInfo,
acDoc.Name,
null,
1,
false,
"c:\\mmidXOM");
string filename = Path.ChangeExtension("C\\mmidXOM", "pdf");
// Display information about the current plot
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status,
"Plotting: " + acDoc.Name + " - " +
acLayout.LayoutName);
// Set the sheet progress range
acPlProgDlg.OnBeginSheet();
acPlProgDlg.LowerSheetProgressRange = 0;
acPlProgDlg.UpperSheetProgressRange = 100;
acPlProgDlg.SheetProgressPos = 0;
// Plot the first sheet/layout
PlotPageInfo acPlPageInfo = new PlotPageInfo();
acPlEng.BeginPage(acPlPageInfo,
acPlInfo,
true,
null);
acPlEng.BeginGenerateGraphics(null);
acPlEng.EndGenerateGraphics(null);
// Finish plotting the sheet/layout
acPlEng.EndPage(null);
acPlProgDlg.SheetProgressPos = 100;
acPlProgDlg.OnEndSheet();
// Finish plotting the document
acPlEng.EndDocument(null);
// Finish the plot
acPlProgDlg.PlotProgressPos = 100;
acPlProgDlg.OnEndPlot();
acPlEng.EndPlot(null);
//re-set the original value of BACKGROUNDPLOT.
Application.SetSystemVariable("BACKGROUNDPLOT",
bgPlot);
}
}
}
}
}
}
}
If you use the autodesk pdf driver that you have commented out (//acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWG To PDF.pc3", "ISO_A3_(297.00_x_430.00_mm)") you can select the inversed page dimensions (430.00_x_297.00_mm).
I'll bet that PDFCreator only has 1 definition of "A3".
If your layouts are landscape already, you could try PlotRotation.Degrees000.
Can't find what you're looking for? Ask the community or share your knowledge.