文書を画像に変換する

DOCXやPDFなどの他の形式の文書の代わりに画像を取得する必要がある場合があります。 たとえば、ドキュメントページのプレビューをwebサイトまたはアプリケーションに追加したり、ドキュメントの「スキャン」を作成して請求書を送信したりする必要があります。 これは、任意のサポートされているロード形式の文書を任意のサポートされている保存形式の画像に変換する必要がある場合です。

画像形式に変換する

既に説明したすべての変換例と同様に、新しいドキュメントを作成するか、サポートされている任意の形式で既存のドキュメントをロードし、必要な変更を加えて、JPEG、PNG、BMPなどの使用可能な任意の画像形式で保存する必要があります。

次のコード例は、DOCXをJPEGに変換する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Load the document from disk.
Document doc = new Document(dataDir + "TestDoc.pdf");
// Save the document in JPEG format.
doc.save(dataDir + "SaveDocx2Jpeg.jpeg");

画像に変換するときに保存オプションを指定する

Aspose.Wordsは、ドキュメントをさまざまな画像形式で保存する方法をより詳細に制御できるImageSaveOptionsクラスを提供します。 このクラスの一部のプロパティは、FixedPageSaveOptionsSaveOptionsなどの基本クラスのプロパティを継承またはオーバーロードしますが、画像の保存に固有のオプションもあり

PageSetプロパティを使用して、画像形式に変換するページを指定することができます。 たとえば、最初のページまたは明確なページのプレビューのみが必要な場合に適用できます。

また、次のプロパティを使用して、出力画質とピクセル形式を制御することもできます– HorizontalResolution, VerticalResolution, Resolution, Scale, PixelFormat, 次のプロパティを使用して、画像の色の設定を設定するだけでなく、– ImageBrightness, ImageColorMode, ImageContrast, PaperColor.

また、JpegQualityTiffCompressionなど、特定の形式に適用されるプロパティもあります。

次のコード例は、いくつかの追加設定を適用して最初のドキュメントページのプレビューを作成する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Load the document from disk.
Document doc = new Document(dataDir + "TestDoc.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
// Set the "PageSet" to "0" to convert only the first page of a document.
options.setPageSet(new PageSet(0));
// Change the image's brightness and contrast.
// Both are on a 0-1 scale and are at 0.5 by default.
options.setImageBrightness(0.3f);
options.setImageContrast(0.7f);
// Change the horizontal resolution.
// The default value for these properties is 96.0, for a resolution of 96dpi.
options.setHorizontalResolution(72f);
// Save the document in JPEG format.
doc.save(dataDir + "SaveDocx2Jpeg.jpeg", options);