Publish DWG layout to actual size in PDF

Hi, is there a way to publish each AutoCAD layout to its actual size in a PDF? i notice that currently it reads the model space extents size to determine the page size for the PDF.

@connor.ferguson,
Hello. I guess yes, but we don’t have ready example for this. You may use property rasterizationOptions.LayoutPageSizes to set up the size for each layout and this example to set up exact page size Page Size not honored - #5 by oleksii.gorokhovatskyi. If you specify on some test file what you mean by “actual size” (to avoid any confusions) I will try to gather up all these pieces into one working example.

Great thanks Oleksii, i’ve attached a drawing where layout 1 is A0 and layout 2 is A4, i’m not sure how to detect the layout size and set it accordingly.Drawing4.zip (26.9 KB)

@connor.ferguson,
OK, I see sizes, let me try to get them from drawing and apply.

1 Like

@connor.ferguson,
here is the example of the main function and helper methods:

string fileName = "Drawing4.dwg";
string outPath = fileName + ".pdf";

using (CadImage cadImage = (CadImage)Image.Load(fileName))
{
	CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();

	foreach (CadLayout layout in cadImage.Layouts.Values)
	{
		if (layout.LayoutName == "Model") continue;

		SizeF pageSize = GetPageSizeFromLayout(layout);
		rasterizationOptions.LayoutPageSizes.Add(layout.LayoutName, pageSize);
	}

	PdfOptions pdfOptions = new PdfOptions();
	pdfOptions.VectorRasterizationOptions = rasterizationOptions;
	cadImage.Save(outPath, pdfOptions);
}

Helpers:

private static SizeF GetPageSizeFromLayout(CadLayout layout)
{
	var isLandscape = IsLayoutLandscape(layout);

	float pageWidth;
	float pageHeight;

	if (layout.PlotPaperUnits == CadPlotPaperUnits.PlotInInches)
	{
		pageWidth = (float)MillimetersToInches(layout.PlotPaperSize.Width);
		pageHeight = (float)MillimetersToInches(layout.PlotPaperSize.Height);
	}
	else
	{
		pageWidth = (float)layout.PlotPaperSize.Width;
		pageHeight = (float)layout.PlotPaperSize.Height;
	}

	if (isLandscape)
	{
		return new SizeF(pageHeight, pageWidth);
	}
	else
	{
		return new SizeF(pageWidth, pageHeight);
	}
}

private static bool IsLayoutLandscape(CadLayout layout)
{
	return layout.PlotRotation == CadPlotRotation.Clockwise90Degrees || layout.PlotRotation == CadPlotRotation.Counterclockwise90Degrees;
}

private static double MillimetersToInches(double millimeters)
{
	const double MillimetersToInchesFactor = 0.0393701;
	return millimeters * MillimetersToInchesFactor;
}

Perfect, thanks @Oleksii.Gorokhovatskyi

1 Like