This example shows how to get and set print options.
//-------------------------------------------------------------- // Preconditions: // 1. Create a C# Windows console project. // 2. Copy and paste this example into the C# IDE. // 3. Add a reference to: // install_dir\APISDK\tlb\DraftSight.Interop.dsAutomation.dll. // 4. Add references to System and System.Windows.Forms. // 5. Start DraftSight and open a document. // 6. Start debugging the project. // // Postconditions: Message boxes pop up if setting any printer // option fails for the specified printer. Read the text in each // message box before clicking OK to close it. //---------------------------------------------------------------- using DraftSight.Interop.dsAutomation; using System; using System.Runtime.InteropServices; using System.Windows.Forms;
static class Module1
{
public static void Main()
{
DraftSight.Interop.dsAutomation.Application dsApp;
Document dsDoc = default(Document);
//Connect to DraftSight
dsApp = (DraftSight.Interop.dsAutomation.Application)Marshal.GetActiveObject("DraftSight.Application");
dsApp.AbortRunningCommand(); // abort any command currently running in DraftSight to avoid nested commands
//Get active document
dsDoc = (Document)dsApp.GetActiveDocument();
if (dsDoc != null)
{
//Get and set printing options
GetSetPrintOptions(dsApp);
}
}
public static void GetSetPrintOptions(DraftSight.Interop.dsAutomation.Application dsApp)
{
PrintManager dsPrintMgr = default(PrintManager);
string printerName = null;
object[] dsVarPaperSizes = null;
double paperLength = 0;
double paperWidth = 0;
double rightLength = 0;
double rightWidth = 0;
string paperSize = null;
double precision = 0;
double xOffset = 0;
double yOffset = 0;
dsPrintOrientation_e printOrientation = default(dsPrintOrientation_e);
bool centerPrint = false;
int printQuality = 0;
bool scaleLineWeight = false;
bool scaleToFit = false;
string styleTable = null;
bool userScale = false;
dsViewDisplayStyle_e viewDisplayStyle = default(dsViewDisplayStyle_e);
//Get PrintManager
dsPrintMgr = dsApp.GetPrintManager();
if (dsPrintMgr != null)
{
//Get printer name
printerName = "JPG";
dsPrintMgr.Printer = printerName;
if (string.IsNullOrEmpty(printerName))
{
MessageBox.Show("Failed to set IPrintManager.Printer property " + printerName + " value.");
}
//Get available paper sizes
dsVarPaperSizes = (object[])dsPrintMgr.AvailablePaperSizes();
if (dsVarPaperSizes == null)
{
MessageBox.Show("List of available paper sizes is empty for " + dsPrintMgr.Printer + " printer.");
}
//Set paper size to "UserDefinedRaster (87.00 x 134.00Pixels)"
paperSize = "UserDefinedRaster (87.00 x 134.00Pixels)";
dsPrintMgr.PaperSize = paperSize;
if (paperSize != dsPrintMgr.PaperSize)
{
MessageBox.Show("Failed to set IPrintManager.PaperSize property to " + paperSize + " value.");
}
//Get paper size
rightLength = 134;
rightWidth = 87;
dsPrintMgr.GetPaperSize(out paperLength, out paperWidth);
//Verify paper length value
precision = 1E-09;
if (Math.Abs(rightLength - paperLength) > precision)
{
MessageBox.Show("IPrintManager.GetPaperSize method returns wrong paper length for '" + paperSize + "' paper size.");
}
//Verify paper width value
if (Math.Abs(rightWidth - paperWidth) > precision)
{
MessageBox.Show("IPrintManager.GetPaperSize method returns wrong paper width for '" + paperSize + "' paper size.");
}
//Set paper size to "VGA_(640.00_x_480.00_Pixels)"
paperSize = "VGA_(640.00_x_480.00_Pixels)";
dsPrintMgr.PaperSize = paperSize;
if (paperSize != dsPrintMgr.PaperSize)
{
MessageBox.Show("Failed to set IPrintManager.PaperSize property to " + paperSize + " value.");
}
//Get paper size
rightLength = 480;
rightWidth = 640;
dsPrintMgr.GetPaperSize(out paperLength, out paperWidth);
//Verify paper length value
if (Math.Abs(rightLength - paperLength) > precision)
{
MessageBox.Show("IPrintManager.GetPaperSize method returns wrong paper length for '" + paperSize + "' paper size.");
}
//Verify paper width value
if (Math.Abs(rightWidth - paperWidth) > precision)
{
MessageBox.Show("IPrintManager.GetPaperSize method returns wrong paper width for '" + paperSize + "' paper size.");
}
//Get print margins
double top = 0;
double bottom = 0;
double left = 0;
double right = 0;
dsPrintMgr.GetPrintMargins(out top, out bottom, out left, out right);
//Set print offset
xOffset = 5.0;
yOffset = 10.0;
dsPrintMgr.SetPrintOffset(xOffset, yOffset);
//Get print margins
double resultXOffset = 0;
double resultYOffset = 0;
dsPrintMgr.GetPrintOffset(out resultXOffset, out resultYOffset);
//Verify print offset values
if (Math.Abs(resultXOffset - xOffset) > precision)
{
MessageBox.Show("IPrintManager.GetPrintOffset method returns wrong XOffset value. It should be " + xOffset + ", but it is " + resultXOffset + ".");
}
if (Math.Abs(resultYOffset - yOffset) > precision)
{
MessageBox.Show("IPrintManager.GetPrintOffset method returns wrong YOffset value. It should be " + yOffset + ", but it is " + resultYOffset + ".");
}
//Set print orientation
printOrientation = dsPrintOrientation_e.dsPrintOrientation_Landscape;
dsPrintMgr.Orientation = printOrientation;
//Verify if print orientation was set
if (printOrientation != dsPrintMgr.Orientation)
{
MessageBox.Show("Failed to set IPrintManager.Orientation property. It should be " + printOrientation + ", but it is " + dsPrintMgr.Orientation + ".");
}
//Set PrintOnCenter property
centerPrint = false;
dsPrintMgr.PrintOnCenter = centerPrint;
if (centerPrint != dsPrintMgr.PrintOnCenter)
{
MessageBox.Show("Failed to set IPrintManager.PrintOnCenter property to " + centerPrint);
}
//Set print quality
printQuality = 300;
dsPrintMgr.Quality = printQuality;
//Set ScaleLineWeight property
scaleLineWeight = false;
dsPrintMgr.ScaleLineWeight = scaleLineWeight;
if (scaleLineWeight != dsPrintMgr.ScaleLineWeight)
{
MessageBox.Show("Failed to set IPrintManager.ScaleLineWeight property.");
}
//Set ScaleToFit property
scaleToFit = false;
dsPrintMgr.ScaleToFit = scaleToFit;
if (scaleToFit != dsPrintMgr.ScaleToFit)
{
MessageBox.Show("Failed to set IPrintManager.ScaleToFit property to " + scaleToFit);
}
//Set StyleTable property
styleTable = "default.ctb";
dsPrintMgr.StyleTable = styleTable;
if (styleTable != dsPrintMgr.StyleTable)
{
MessageBox.Show("Failed to set IPrintManager.StyleTable property to " + styleTable);
}
//Set UserScale property
userScale = false;
dsPrintMgr.UserScale = userScale;
//Set ViewDisplayStyle property
viewDisplayStyle = dsViewDisplayStyle_e.dsViewDisplayStyle_Rendered;
dsPrintMgr.ViewDisplayStyle = viewDisplayStyle;
if (viewDisplayStyle != dsPrintMgr.ViewDisplayStyle)
{
MessageBox.Show("Failed to set IPrintManager.ViewDisplayStyle property to " + viewDisplayStyle);
}
}
else
{
MessageBox.Show("IDocument.GetPrintManager returns Nothing.");
}
}
}