将 DWG/DXF 图纸和布局导出为指定尺寸

将模型和所有布局导出为 A4 PDF 尺寸

Aspose.CAD API 允许您将 DWG/DXF 文件的所有工作表导出为指定的物理 PDF 尺寸。 以下示例代码设置 CadRasterizationOptions 对象的尺寸,以实现所需的 PDF 尺寸。 A4 纸张的尺寸为 210x297 毫米或 8.27x11.69 英寸,这些值在代码中使用。

CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
boolean isMetric = false;
double coefficient = 1.0;
switch (cadImage.getUnitType())
{
// it is possible to extend this with more types that may appear in drawings
case UnitType.Meter:
isMetric = true;
coefficient = 1.0;
break;
case UnitType.Millimeter:
isMetric = true;
coefficient = 0.001;
break;
case UnitType.Inch:
coefficient = 1.0;
break;
}
SizeF size;
if (isMetric)
{
double metersCoeff = 1 / 1000.0;
double scaleFactor = metersCoeff / coefficient;
size = new SizeF((float)(210 * scaleFactor), (float)(297 * scaleFactor));
cadRasterizationOptions.setUnitType(UnitType.Millimeter);
}
else
{
size = new SizeF((float)(8.3f / coefficient), (float)(11.7f / coefficient));
cadRasterizationOptions.setUnitType(UnitType.Inch);
}
cadRasterizationOptions.setPageSize(size);

以不同尺寸导出 CAD 布局到 PDF

有时需要将布局导出为其物理尺寸。下面的示例演示将绘图导出为多页 PDF,其中每一页都包含其布局内容,并具有自己的物理 PDF 尺寸。此示例使用 getLayoutPageSizes 属性。

CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
for (String layoutName : cadImage.getLayouts().getKeysTyped())
{
CadLayout layout = cadImage.getLayouts().get(layoutName);
if (layout.getLayoutName() == "Model") continue;
System.out.println(layout.getLayoutName());
SizeF pageSize = GetPageSizeFromLayout(layout);
cadRasterizationOptions.getLayoutPageSizes().put(layout.getLayoutName(), pageSize);
}
private static SizeF GetPageSizeFromLayout(CadLayout layout)
{
boolean isLandscape = IsLayoutLandscape(layout);
float pageWidth;
float pageHeight;
if (layout.getPlotPaperUnits() == CadPlotPaperUnits.PlotInInches)
{
pageWidth = (float)MillimetersToInches(layout.getPlotPaperSize().getWidth());
pageHeight = (float)MillimetersToInches(layout.getPlotPaperSize().getHeight());
}
else
{
pageWidth = (float)layout.getPlotPaperSize().getWidth();
pageHeight = (float)layout.getPlotPaperSize().getHeight();
}
if (isLandscape)
{
return new SizeF(pageHeight, pageWidth);
}
else
{
return new SizeF(pageWidth, pageHeight);
}
}
private static boolean IsLayoutLandscape(CadLayout layout)
{
short plotRotation = layout.getPlotRotation();
return plotRotation == CadPlotRotation.Clockwise90Degrees || plotRotation == CadPlotRotation.Counterclockwise90Degrees;
}
private static double MillimetersToInches(double millimeters)
{
double MillimetersToInchesFactor = 0.0393701;
return millimeters * MillimetersToInchesFactor;
}