将文档转换为 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 格式非常简单,只需两行代码即可完成:
- 使用 Document 对象的构造函数之一,通过指定文档名称及其格式扩展名,将文档加载到 Document 对象中。
- 在 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"); |
您可以从Aspose.Words GitHub下载本示例的模板文件。
有时需要指定其他选项,这可能会影响将文档另存为 PDF 的结果。这些选项可以通过使用 PdfSaveOptions 类来指定,其中包含确定 PDF 输出如何显示的属性。
请注意,使用相同的技术,您可以将任何流程布局格式文档转换为 PDF 格式。
转换为不同的 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"); |
// 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); |
也可以看看
-
文章 渲染 了解有关固定页面和流程布局格式的更多信息
-
有关页面布局的更多信息,请参阅 转换为固定页面格式 文章
-
有关使用
PdfSaveOptions
类的更多信息,请参阅 转换为 PDF 时指定渲染选项 文章 -
了解转换为 PDF/A 和 PDF/UA 的功能 文章描述了哪些 PDF 标准以及 PDF 标准的相关 ISO 支持 Aspose.Words
-
选择哪种 PDF 标准更好 文章确定哪些 PDF 标准对哪些情况有意义
-
使用 PDF/A 或 PDF/UA 文章描述了 PDF/A 和 PDF/UA 格式文档内容的要求 - 主要是结构和字体的要求
-
保存为 PDF/A 和 PDF/UA 时出现辅助功能问题警告 文章描述了 PDF/A 和 PDF/UA 所施加的内容可访问性要求