DWG DXF 转 PDF C# | 在 C# .NET 中将 Auto CAD 文件转换为 PDF JPEG PNG

在 C# 中将 DWG 或 DXF 转换为 PNG JPEG BMP GIF 或 TIFF

Aspose.CAD for .NET 可以将 AutoCAD 绘图格式,如 DXFDWG 转换为 PNGBMPTIFFJPEGGIF。它提供了高效且易于使用的 API 来实现此目标。

您可以使用以下简单步骤将任何受支持的 AutoCAD 绘图格式转换为栅格图像格式。

  1. 将 AutoCAD DWG 或 DXF 文件加载到 Image 类中。
  2. 创建 CadRasterizationOptions 的实例。
  3. 使用 PageWidthPageHeight 设置/更改图像的大小。
  4. 创建 ImageOptionsBase 的实例。
  5. VectorRasterizationOptions 属性设置为前一步创建的 CadRasterizationOptions 实例。
  6. 通过传递文件路径(或 MemoryStream 对象)以及在前一步中创建的 ImageOptionsBase 实例,使用 Image.Save 将 AutoCAD 图纸保存为 PDF。

以下是完整的源代码。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Create an instance of PngOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.PngOptions();
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_raster_image_out.png";
// Save resultant image
image.Save(MyDir, options);
}

默认情况下,API 仅渲染“模型”布局。但是,在将 CAD 绘图转换为图像格式时,您也可以指定所需的布局。

自定义 CAD 转换

CAD 到 PDF 和 CAD 到栅格图像转换过程是高度可配置的,因为 CadRasterizationOptions 类是以这种方式实现的,它提供了许多可选功能,您可以设置其以根据应用程序需求覆盖呈现过程。

CadRasterizationOptions 类

CadRasterizationOptions 类对于所有受支持的 CAD 格式(如 DWG 和 DXF)都是通用的,因此本文中分享的信息对上述 CAD 格式均有效。

最有用的 CadRasterizationOptions 类属性如下:

属性默认值是否必填说明
PageWidth0指定页面宽度。
PageHeight0指定页面高度
ScaleMethodScaleType.ShrinkToFit指定是否应自动缩放图纸。默认值会自动缩小图像以适应画布大小。切换到 GrowToFit 模式,或使用 None 设置以禁用自动缩放。
BackgroundColorColor.White指定输出图像的背景颜色。
DrawTypeCadDrawTypeMode.UseDrawColor指定实体的着色模式。指定 UseObjectColor 选项以使用其原生颜色绘制实体,或指定 UseDrawColor 选项以重写原生颜色。
DrawColorColor.Black指定重写的实体颜色(仅在 DrawType 设置为 UseDrawColor 属性值时)。
AutomaticLayoutsScalingFalse指定是否必须执行自动布局缩放以匹配模型布局。

设置画布大小和模式

从 CAD 导出到 PDF 或 CAD 到栅格图像格式并不是一项简单的任务。由于生成的 PDF 或图像需要定义画布大小,我们需要具体指定 PDF 页面输出尺寸以正确呈现绘图。如果不是明确设置 CadRasterizationOptions.PageWidthCadRasterizationOptions.PageHeight 属性,您可能会出现 ImageSaveException

此外,您可以指定尺寸缩放选项。缩放选项由 CadRasterizationOptions.ScaleMethod 属性设置。使用此选项可以根据 CadRasterizationOptions.PageWidthCadRasterizationOptions.PageHeight 的值自动调整图像尺寸。默认情况下,CadRasterizationOptions.ScaleMethod 设置为 ScaleType.ShrinkToFit 模式。此属性定义了以下行为:

  • 如果 CAD 绘图尺寸大于结果画布尺寸,则绘图尺寸将被缩小以适应结果画布,同时保持纵横比。
  • 如果 CAD 绘图尺寸小于结果画布尺寸,则将 CadRasterizationOptions.ScaleMethod 属性设置为 ScaleType.GrowToFit 以增加绘图大小以适应 PDF 画布,同时保持纵横比。
  • 或者使用 ScaleType.None 选项禁用自动缩放。

以下代码示例展示了自动缩放选项的使用。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.AutomaticLayoutsScaling = true;
rasterizationOptions.NoScaling = false;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

设置背景和绘图颜色

默认情况下,结果画布的调色板设置为通用文档方案。这意味着 CAD 绘图中的所有实体都用黑色笔在纯白色背景上绘制。这些设置可以通过 CadRasterizationOptions.BackgroundColorCadRasterizationOptions.DrawColor 属性进行更改。更改 CadRasterizationOptions.DrawColor 属性还需设置 CadRasterizationOptions.DrawType 属性,以便使用所需的绘图颜色。CadRasterizationOptions.DrawType 属性控制 CAD 实体是保留其颜色还是转换为自定义颜色。要保留实体颜色,请将 CadRasterizationOptions.DrawType 指定为 CadDrawTypeMode.UseObjectColor,否则指定 CadDrawTypeMode.UseDrawColor 值。

以下代码示例显示了如何使用不同的颜色属性。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.BackgroundColor = Aspose.CAD.Color.Beige;
rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseDrawColor;
rasterizationOptions.DrawColor = Aspose.CAD.Color.Blue;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

设置自动布局缩放

大多数 CAD 绘图在单个文件中存储了多个布局,而每个布局可能具有不同的尺寸。在将此类 CAD 绘图呈现为 PDF 时,PDF 的每个页面都可能根据布局大小具有不同的缩放。为了使呈现变得均匀,Aspose.CAD API 已公开 CadRasterizationOptions.AutomaticLayoutsScaling 属性。其默认值为 false,但设置为 true 时,API 将尝试为每个单独的布局搜索相应的缩放,并以相应的方式绘制它们,通过根据页面大小执行自动调整大小操作。

以下是 CadRasterizationOptions.AutomaticLayoutsScaling 属性如何与 CadRasterizationOptions.ScaleMethod 属性协同工作的。

  1. 如果 ScaleMethod 设置为 ScaleType.ShrinkToFitScaleType.GrowToFit,且 AutomaticLayoutsScaling 设置为 false,则所有布局(包括模型)将根据第一种选项进行处理。
  2. 如果 ScaleMethod 设置为 ScaleType.ShrinkToFitScaleType.GrowToFit,且 AutomaticLayoutsScaling 设置为 true,则所有布局(不包括模型)将根据其大小进行处理,而模型将根据第一种选项进行处理。
  3. 如果 ScaleMethod 设置为 ScaleType.None,且 AutomaticLayoutsScaling 设置为 true 或 false,则不会执行缩放。

以下代码示例显示了如何为 CAD 到 PDF 转换设置自动布局缩放。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
// Set Auto Layout Scaling
rasterizationOptions.AutomaticLayoutsScaling = true;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "result_out.pdf";
//Export the CAD to PDF
image.Save(MyDir, pdfOptions);
}

在 C# 中将 AutoCAD DXF 或 DWG 布局转换为 PNG 或其他图像格式

Aspose.CAD for .NET API 可以将支持格式的 CAD 布局(如 DXF 和 DWG)转换为 PNG BMP TIFF JPEG 和 GIF。API 还支持将 CAD 绘图的特定布局渲染为不同的 PSD 层。

以下是您可以通过以下简单步骤实现相同目标的方法。

  • 使用 Image 类加载 AutoCAD DWG 或 DXF 文件。
  • 设置/更改图像的宽度和高度。
  • 使用 CadRasterizationOptions.Layouts 属性设置所需的布局名称。
  • 创建 ImageOptionsBase 的实例,并将其 VectorRasterizationOptions 属性设置为前一步中创建的 CadRasterizationOptions 实例。
  • 将 CAD 布局保存为 TIFF 或图像。

以下是完整的源代码。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Specify a list of layout names
rasterizationOptions.Layouts = new string[] { "Model", "Layout1" };
// Create an instance of TiffOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_layoutstorasterimage_out.tiff";
// Save resultant image
image.Save(MyDir, options);
}

启用 CAD 渲染过程的跟踪

Aspose.CAD 引入了一系列类和支持的枚举字段,以协助跟踪 CAD 渲染过程。通过这些变化,CAD 到 PDF 转换现在可以在启用跟踪的情况下实现,如下所示。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
MemoryStream stream = new MemoryStream();
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions cadRasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
pdfOptions.VectorRasterizationOptions = cadRasterizationOptions;
cadRasterizationOptions.PageWidth = 800;
cadRasterizationOptions.PageHeight = 600;
image.Save(stream, pdfOptions);
}

CAD 渲染过程的跟踪可以检测到以下可能的问题。

  1. 缺少或损坏的头信息。
  2. 缺少布局信息。
  3. 缺少块实体。
  4. 缺少尺寸样式。
  5. 缺少样式。

在转换 CAD 图纸时替换字体

特定的 CAD 图纸可能使用某些在进行 CAD 到 PDF 或 CAD 到栅格图像转换时机器上不可用的特定字体。在这种情况下,Aspose.CAD API 将触发适当的异常以突出缺失的字体,并停止转换过程,因为 API 需要这些字体以正确呈现内容到生成的 PDF 或图像中。

Aspose.CAD API 提供了一种简单的方法来使用机制将所需的字体替换为可用字体。CadImage.Styles 属性返回 CadStylesDictionary 的实例,而它又包含 CAD 绘图中每种样式的 CadStyleTableObject,而 CadStyleTableObject.PrimaryFontName 可用于指定可用字体名称。

以下代码片段演示了对于 .NET API 如何更改 CAD 绘图中所有样式的字体。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
// Set the font name
style.PrimaryFontName = "Arial";
}
}
Console.WriteLine("\nFont changed successfully.");
}
public static void SubstitutingFontByName()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}
}

您还可以通过样式名称访问,仅更改某个特定样式的字体。以下代码片段演示了这种方法的用法。

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}

将 CAD 图层转换为栅格图像格式

Aspose.CAD for .NET API 提供了一种高效且易于使用的方式来指定所需的 CAD 图层名称并将其渲染为栅格图像格式。以下是您可以通过以下 5 个简单步骤实现相同目标的方法。

  1. 使用工厂方法 Load 将 CAD 文件加载到 Image 的实例中。
  2. 创建 CadRasterizationOptions 的实例并设置其必填属性,如 PageWidthPageHeight
  3. 使用 CadRasterizationOptions.Layers.Add 方法添加所需的图层名称。
  4. 创建 ImageOptionsBase 的实例,并将其 VectorRasterizationOptions 属性设置为前一步中创建的 CadRasterizationOptions 实例。
  5. 调用 Image.Save 方法,传递文件路径(或 MemoryStream 对象)以及在前一步中创建的 ImageOptionsBase 实例。

以下是完整的源代码。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of Image
using (var image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers= new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
MyDir = MyDir + "CADLayersToRasterImageFormats_out.jpg";
image.Save(MyDir, options);
}
Console.WriteLine("\nCAD layers converted successfully to raster image format.\nFile saved at " + MyDir);
}
public static void ConvertAllLayersToRasterImageFormats()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers = new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}
Console.WriteLine("\nCAD all layers converted successfully to raster image format.");

将所有 CAD 图层转换为单独的图像

您可以使用 CadImage.Layers 获取 CAD 绘图中的所有图层,并将每个图层渲染为单独的图像,如下所示。

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Set the drawing to render at the center of image
rasterizationOptions.CenterDrawing = true;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers.Add(layerName);
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}

将 DWF CAD 图层转换为栅格图像格式

Aspose.CAD for .NET API 提供了一种高效且易于使用的方式来指定所需的 CAD 图层名称并将其渲染为栅格图像格式。以下是您可以通过以下 5 个简单步骤实现相同目标的方法。

  1. 使用工厂方法 Load 将 DWF CAD 文件加载到 Image 的实例中。
  2. 创建 CadRasterizationOptions 的实例并设置其必填属性,如 PageWidthPageHeight
  3. 使用 CadRasterizationOptions.Layers.Add 方法添加所需的图层名称。
  4. 创建 ImageOptionsBase 的实例,并将其 VectorRasterizationOptions 属性设置为前一步中创建的 CadRasterizationOptions 实例。
  5. 调用 Image.Save 方法,传递文件路径(或 MemoryStream 对象)以及在前一步中创建的 ImageOptionsBase 实例。

以下是完整的源代码。

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string inputFile = MyDir + "18-12-11 9644 - site.dwf";
using (Image image = Image.Load(inputFile))
{
BmpOptions bmpOptions = new BmpOptions();
var dwfRasterizationOptions = new CadRasterizationOptions();
bmpOptions.VectorRasterizationOptions = dwfRasterizationOptions;
dwfRasterizationOptions.PageHeight = 500;
dwfRasterizationOptions.PageWidth = 500;
// export
string outPath = MyDir + "18-12-11 9644 - site.bmp";
image.Save(outPath, bmpOptions);
}

Aspose.CAD for .NET 直接在输出文档中写入有关 API 和版本号的信息。例如,在将文档渲染为 PDF 时,Aspose.CAD for .NET 在应用程序字段中填充 ‘Aspose.CAD’ 的值,在 PDF 生成器字段中填充值,例如 ‘Aspose.CAD v 17.10’。

请注意,您无法指示 Aspose.CAD for .NET 更改或删除该输出文档中的信息。