ドキュメントを PDF に変換する

ドキュメントをある形式から別の形式に簡単かつ確実に変換できる機能は、Aspose.Words の重要な機能です。変換用の最も一般的な形式の 1 つは PDF です。これは固定レイアウト形式であり、さまざまなプラットフォームでのレンダリング中にドキュメントの元の外観が保持されます。 Aspose.Words では、「レンダリング」という用語は、ドキュメントをページ分割されたファイル形式、またはページの概念を持つファイル形式に変換するプロセスを表すために使用されます。

Word 文書を PDF {#convert-a-word-document-to-pdf} に変換する

Word から PDF への変換は、いくつかの段階の計算を必要とするかなり複雑なプロセスです。 Aspose.Words レイアウト エンジンは、Microsoft Word のページ レイアウト エンジンの動作方法を模倣し、PDF 出力ドキュメントを Microsoft Word で表示されるものにできるだけ近づけます。

Aspose.Words を使用すると、Microsoft Office を使用せずに、プログラムによって文書を DOC や DOCX などの Word 形式から PDF に変換できます。この記事では、この変換を実行する方法について説明します。

DOC または DOCX を PDF {#convert-doc-or-docx-to-pdf} に変換

DOC または DOCX ドキュメント形式から Aspose.Words の PDF 形式への変換は非常に簡単で、次のような 2 行のコードだけで実行できます。

  1. コンストラクターの 1 つを使用して、ドキュメント名とその形式拡張子を指定して、ドキュメントを Document オブジェクトにロードします。
  2. Document オブジェクトで Document.save メソッドの 1 つを呼び出し、「.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 標準 {#convert-to-various-pdf-standards} に変換

Aspose.Words は、DOC または DOCX からさまざまな PDF 形式標準 (PDF 1.7、PDF 1.5 など) への変換をサポートする PdfCompliace 列挙を提供します。

次のコード例は、PDF17 に準拠した PdfSaveOptions を使用してドキュメントを PDF 1.7 に変換する方法を示しています。

# 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 が連結されます。

関連項目