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

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

Word 文書を PDF に変換する

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

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

DOCX または DOC を 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-.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 は、DOC または DOCX からさまざまな PDF 形式標準 (PDF 1.7、PDF 1.5 など) への変換をサポートする PDFコンプライアンスenumeration を提供します。

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

// 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);

関連項目