将文档转换为 PDF

轻松可靠地将文档从一种格式转换为另一种格式的能力是 Aspose.Words 的一项关键功能。最流行的转换格式之一是 PDF–一种固定布局格式,它在各种平台上呈现时保留文档的原始外观。 Aspose.Words 中使用"渲染"术语来描述将文档转换为分页或具有页面概念的文件格式的过程。

将 Word 文档转换为 PDF

从 Word 到 PDF 的转换是一个相当复杂的过程,需要多个计算阶段。 Aspose.Words 布局引擎模仿 Microsoft Word 页面布局引擎的工作方式,使 PDF 输出文档看起来尽可能接近您在 Microsoft Word 中看到的内容。

使用 Aspose.Words,您可以以编程方式将文档从 Word 格式(例如 DOC 或 DOCX)转换为 PDF,而无需使用 Microsoft Office。本文解释了如何执行此转换。

将 DOCX 或 DOC 转换为 PDF

从 DOC 或 DOCX 文档格式转换为 Aspose.Words 格式的 PDF 格式非常简单,只需两行代码即可完成:

  1. 使用 Document 对象的构造函数之一,通过指定文档名称及其格式扩展名,将文档加载到 Document 对象中。
  2. Document 对象上调用 Document.Save 方法之一,并通过输入扩展名为".PDF"的文件名来指定所需的输出格式为 PDF。

以下代码示例演示如何使用 Save 方法将文档从 DOCX 转换为 PDF:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Document.docx");
doc.Save(ArtifactsDir + "BaseConversions.DocxToPdf.pdf");
view raw docx-to-pdf.cs hosted with ❤ by GitHub

您可以从Aspose.Words GitHub下载本示例的模板文件。

转换为不同的 PDF 标准

Aspose.Words提供Pdf合规性枚举支持将DOC或DOCX转换为各种PDF格式标准(如PDF 1.7、PDF 1.5等)。

以下代码示例演示如何使用 PdfSaveOptions 将文档转换为 PDF 1.7,并符合 PDF17:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Rendering.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions { Compliance = PdfCompliance.Pdf17 };
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions);

将图像转换为 PDF

转换为 PDF 不受 Microsoft Word 文档格式的限制。 Aspose.Words 支持的任何格式(包括以编程方式创建的格式)也可以转换为 PDF。例如,我们可以将单页图像(如 JPEG、PNG、BMP、EMF 或 WMF)以及多页图像(如 TIFF 和 GIF)转换为 PDF。

以下代码示例演示如何将 JPEG 和 TIFF 图像转换为 PDF:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
ConvertImageToPdf(ImagesDir + "Logo.jpg", ArtifactsDir + "BaseConversions.JpgToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Transparent background logo.png", ArtifactsDir + "BaseConversions.PngToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Windows MetaFile.wmf", ArtifactsDir + "BaseConversions.WmfToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Tagged Image File Format.tiff", ArtifactsDir + "BaseConversions.TiffToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Graphics Interchange Format.gif", ArtifactsDir + "BaseConversions.GifToPdf.pdf");
view raw image-to-pdf.cs hosted with ❤ by GitHub
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Converts an image to PDF using Aspose.Words for .NET.
/// </summary>
/// <param name="inputFileName">File name of input image file.</param>
/// <param name="outputFileName">Output PDF file name.</param>
public void ConvertImageToPdf(string inputFileName, string outputFileName)
{
Console.WriteLine("Converting " + inputFileName + " to PDF ....");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(inputFileName))
{
// Find which dimension the frames in this image represent. For example
// the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int framesCount = image.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
image.SelectActiveFrame(dimension, frameIdx);
// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
// Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(
image,
RelativeHorizontalPosition.Page,
0,
RelativeVerticalPosition.Page,
0,
ps.PageWidth,
ps.PageHeight,
WrapType.None);
}
}
doc.Save(outputFileName);
}

要使此代码正常工作,您需要将对 Aspose.Words 和 System.Drawing 的引用添加到您的项目中。

减小 PDF 输出大小

保存为 PDF 时,您可以指定是否要优化输出。为此,您需要将 OptimizeOutput 标志设置为 true,然后删除多余的嵌套画布和空画布,并连接具有相同格式的相邻 glyph。

以下代码示例展示了如何优化输出:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Rendering.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions { OptimizeOutput = true };
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.OptimizeOutput.pdf", saveOptions);

也可以看看