文書をPDFに変換する
文書をある形式から別の形式に簡単かつ確実に変換する機能は、Aspose.Wordsの重要な機能です。 変換のための最も一般的な形式の一つはPDFです–さまざまなプラットフォーム上でのレンダリング中にドキュメントの元の外観を維持する固定レイアウ Aspose.Wordsで"レンダリング"という用語は、ドキュメントをページ分割されたファイル形式またはページの概念を持つファイル形式に変換するプロセスを記述する
Word文書をPDF{#convert-a-word-document-to-pdf}に変換する
単語からPDFへの変換は、いくつかの計算段階を必要とするかなり複雑なプロセスです。 Aspose.WordsレイアウトエンジンはMicrosoft Wordのページレイアウトエンジンの動作を模倣し、PDF出力ドキュメントをMicrosoft Wordで見ることができるものにできるだけ近く見えるようにします。
Aspose.Wordsを使用すると、Microsoft Officeを使用せずに、プログラムで文書をDOCまたはDOCX形式からPDFに変換できます。 この記事では、この変換を実行する方法について説明します。
DOCXまたはDOCをPDF{#converting-doc-or-docx-to-pdf}に変換する
DOCまたはDOCXドキュメント形式からAspose.WordsのPDF形式に変換するのは非常に簡単で、次のようなコード行だけで実現できます:
- ドキュメント名とその書式拡張子を指定して、そのコンストラクタのいずれかを使用してドキュメントを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-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Document.docx"); | |
doc->Save(ArtifactsDir + u"BaseConversions.DocxToPdf.pdf"); |
この例のテンプレートファイルは、次の場所からダウンロードできます Aspose.Words GitHub.
ドキュメントをPDFとして保存した結果に影響を与える追加のオプションを指定する必要がある場合があります。 これらのオプションは、PDF出力の表示方法を決定するプロパティを含むPdfSaveOptionsクラスを使用して指定できます。
同じ手法を使用して、任意のフローレイアウト形式の文書をPDF形式に変換できます。
異なるPDF標準{#converting-to-various-pdf-standards}に変換する
Aspose.WordsはPdfCompliace列挙体を提供し、DOCまたはDOCXをさまざまなPDF形式標準(次のような)に変換することをサポートしますPDF 1.7, PDF 1.5, など。).
次のコード例は、PDF17に準拠してPdfSaveOptionsを使用して文書をPDF1.7に変換する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx"); | |
auto saveOptions = MakeObject<PdfSaveOptions>(); | |
saveOptions->set_Compliance(PdfCompliance::Pdf17); | |
doc->Save(ArtifactsDir + u"WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions); |
画像をPDF{#convert-an-image-to-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-C.git. | |
ConvertImageToPdf(ImagesDir + u"Logo.jpg", ArtifactsDir + u"BaseConversions.JpgToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Transparent background logo.png", ArtifactsDir + u"BaseConversions.PngToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Windows MetaFile.wmf", ArtifactsDir + u"BaseConversions.WmfToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Tagged Image File Format.tiff", ArtifactsDir + u"BaseConversions.TiffToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Graphics Interchange Format.gif", ArtifactsDir + u"BaseConversions.GifToPdf.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.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> | |
void ConvertImageToPdf(String inputFileName, String outputFileName) | |
{ | |
std::cout << (String(u"Converting ") + inputFileName + u" to PDF ....") << std::endl; | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Read the image from file, ensure it is disposed. | |
{ | |
SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromFile(inputFileName); | |
// Insert a section break before each new page, in case of a multi-frame TIFF. | |
builder->InsertBreak(BreakType::SectionBreakNewPage); | |
// 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. | |
SharedPtr<PageSetup> ps = builder->get_PageSetup(); | |
ps->set_PageWidth(ConvertUtil::PixelToPoint(image->get_Width(), image->get_HorizontalResolution())); | |
ps->set_PageHeight(ConvertUtil::PixelToPoint(image->get_Height(), image->get_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->get_PageWidth(), ps->get_PageHeight(), WrapType::None); | |
} | |
doc->Save(outputFileName); | |
} |
このコードを動作させるには、Aspose.WordsとSystem.Drawing
への参照をプロジェクトに追加する必要があります。
また見て下さい
- この記事は、 レンダリング 固定ページ形式とフローレイアウト形式の詳細については、以下を参照してください
- この記事は、 固定ページ形式への変換 ページレイアウトの詳細については
- この記事は、 PDFに変換するときのレンダリングオプションの指定
PdfSaveOptions
クラスの使用の詳細については