文書をPDFに変換する
文書をある形式から別の形式に簡単かつ確実に変換する機能は、Aspose.Wordsの重要な機能です。 変換のための最も一般的な形式の一つはPDFです–さまざまなプラットフォーム上でのレンダリング中にドキュメントの元の外観を維持する固定レイアウ Aspose.Wordsで"レンダリング"という用語は、ドキュメントをページ分割されたファイル形式またはページの概念を持つファイル形式に変換するプロセスを記述する
Word文書をPDF {#convert-a-word-document-to-pdf}に変換する
単語からPDFへの変換は、いくつかの計算段階を必要とするかなり複雑なプロセスです。 Aspose.WordsレイアウトエンジンはMicrosoft Wordのページレイアウトエンジンの動作を模倣し、PDF出力ドキュメントをMicrosoft Wordで見ることができるものにできるだけ近く見えるようにします。
Aspose.Wordsを使用すると、MicrosoftOfficeを使用せずに、プログラムで文書を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-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "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-Java.git. | |
Document doc = new Document(getMyDir() + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); { saveOptions.setCompliance(PdfCompliance.PDF_17); } | |
doc.save(getArtifactsDir() + "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-Java.git. | |
convertImageToPDF(getImagesDir() + "Logo.jpg", getArtifactsDir() + "BaseConversions.JpgToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Transparent background logo.png", getArtifactsDir() + "BaseConversions.PngToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Windows MetaFile.wmf", getArtifactsDir() + "BaseConversions.WmfToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Tagged Image File Format.tiff", getArtifactsDir() + "BaseConversions.TiffToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Graphics Interchange Format.gif", getArtifactsDir() + "BaseConversions.GifToPdf.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
/** | |
* Converts an image to PDF using Aspose.Words for Java. | |
* | |
* @param inputFileName File name of input image file. | |
* @param outputFileName Output PDF file name. | |
* @throws Exception | |
*/ | |
private void convertImageToPDF(String inputFileName, String outputFileName) throws Exception { | |
// Create Aspose.Words.Document and DocumentBuilder. | |
// The builder makes it simple to add content to the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Load images from the disk using the appropriate reader. | |
// The file formats that can be loaded depends on the image readers available on the machine. | |
ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName)); | |
ImageReader reader = ImageIO.getImageReaders(iis).next(); | |
reader.setInput(iis, false); | |
// Get the number of frames in the image. | |
int framesCount = reader.getNumImages(true); | |
// Loop through all frames. | |
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) { | |
// Insert a section break before each new page, in case of a multi-frame image. | |
if (frameIdx != 0) | |
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); | |
// Select active frame. | |
BufferedImage image = reader.read(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.getPageSetup(); | |
ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth())); | |
ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight())); | |
// 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.getPageWidth(), | |
ps.getPageHeight(), | |
WrapType.NONE); | |
} | |
if (iis != null) { | |
iis.close(); | |
reader.dispose(); | |
} | |
doc.save(outputFileName); | |
} |
このコードを動作させるには、Aspose.Words、Javaへの参照を追加する必要があります。awt。イメージ、およびjavax。imageioクラスパッケージをプロジェクトに追加します。
PDF出力サイズを縮小する
PDFに保存するときに、出力を最適化するかどうかを指定できます。 これを行うには、OptimizeOutputフラグをtrueに設定する必要があり、冗長なネストされたキャンバスと空のキャンバスが削除され、同じ書式の隣接グリフが連結されます。
次のコード例は、出力を最適化する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); { saveOptions.setOptimizeOutput(true); } | |
doc.save(getArtifactsDir() + "WorkingWithPdfSaveOptions.OptimizeOutput.pdf", saveOptions); |
また見て下さい
- この記事は、 レンダリング 固定ページ形式とフローレイアウト形式の詳細については、以下を参照してください
- この記事は、 固定ページ形式への変換 ページレイアウトの詳細については
- この記事は、 PDFに変換するときのレンダリングオプションの指定
PdfSaveOptions
クラスの使用の詳細については