将文档转换为 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。本文解释了如何执行此转换。

将 DOC 或 DOCX 转换为 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-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Document.docx")
doc.save(ARTIFACTS_DIR + "BaseConversions.docx_to_pdf.pdf")
view raw docx-to-pdf.py hosted with ❤ by GitHub

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

转换为各种 PDF 标准

Aspose.Words提供PdfCompliace枚举支持将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-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Rendering.docx")
save_options = aw.saving.PdfSaveOptions()
save_options.compliance = aw.saving.PdfCompliance.PDF17
doc.save(ARTIFACTS_DIR + "WorkingWithPdfSaveOptions.conversion_to_pdf_17.pdf", save_options)

将图像转换为 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-Python-via-.NET.git.
self.convert_image_to_pdf(IMAGES_DIR + "Logo.jpg",
ARTIFACTS_DIR + "BaseConversions.JpgToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Transparent background logo.png",
ARTIFACTS_DIR + "BaseConversions.PngToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Windows MetaFile.wmf",
ARTIFACTS_DIR + "BaseConversions.WmfToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Tagged Image File Format.tiff",
ARTIFACTS_DIR + "BaseConversions.TiffToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Graphics Interchange Format.gif",
ARTIFACTS_DIR + "BaseConversions.GifToPdf.pdf")
view raw image-to-pdf.py hosted with ❤ by GitHub
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
@staticmethod
def convert_image_to_pdf(input_file_name: str, output_file_name: str):
"""Converts an image to PDF using Aspose.Words for .NET.
:param input_file_name: File name of input image file.
:param output_file_name: Output PDF file name.
"""
print("Converting " + input_file_name + " to PDF...")
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Read the image from file
with drawing.Image.from_file(input_file_name) as image:
# 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".
dimension = drawing.imaging.FrameDimension(image.frame_dimensions_list[0])
frames_count = image.get_frame_count(dimension)
for frame_idx in range(frames_count):
# Insert a section break before each new page, in case of a multi-frame TIFF.
if frame_idx != 0:
builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE)
image.select_active_frame(dimension, frame_idx)
frame_stream = io.BytesIO()
image.save(frame_stream, drawing.imaging.ImageFormat.png)
# 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.
page_setup = builder.page_setup
page_setup.page_width = aw.ConvertUtil.pixel_to_point(image.width, image.horizontal_resolution)
page_setup.page_height = aw.ConvertUtil.pixel_to_point(image.height, image.vertical_resolution)
# Insert the image into the document and position it at the top left corner of the page.
builder.insert_image(
frame_stream,
aw.drawing.RelativeHorizontalPosition.PAGE,
0,
aw.drawing.RelativeVerticalPosition.PAGE,
0,
page_setup.page_width,
page_setup.page_height,
aw.drawing.WrapType.NONE)
doc.save(output_file_name)

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

减小 PDF 输出大小

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

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Rendering.docx")
save_options = aw.saving.PdfSaveOptions()
save_options.optimize_output = True
doc.save(ARTIFACTS_DIR + "PdfSaveOptions.OptimizeOutput.pdf", save_options)

减小 PDF 输出大小

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

也可以看看