ドキュメントとは別に図形をレンダリングする
文書を処理する場合、一般的なタスクは、文書内にあるすべての画像を抽出し、外部の場所にエクスポートすることです。 このタスクは、画像データを抽出して保存する機能を既に提供しているAspose.WordsAPIで簡単になります。 ただし、段落、矢印図形、小さな画像を含むテキストボックスなど、異なる種類の描画オブジェクトで表される他の種類のグラフィックコンテンツを同様に抽出する必要がある場合があります。 このオブジェクトは個々のコンテンツ要素の組み合わせであるため、このオブジェクトをレンダリングする簡単な方法はありません。 また、コンテンツが単一の画像のように見えるオブジェクトにグループ化されている場合もあります。
Aspose.Wordsは、図形から単純な画像をレンダリングされたコンテンツとして抽出するのと同じ方法で、このタイプのコンテンツを抽出する機能を提供します。 この記事では、この機能を使用して、ドキュメントとは独立して図形をレンダリングする方法について説明します。
Aspose.Wordsの図形の種類
ドキュメント描画レイヤ内のすべてのコンテンツは、Aspose.Wordsドキュメントオブジェクトモジュール(DOM)内のShapeまたはGroupShapeノードで表されます。 このようなコンテンツは、テキストボックス、画像、AutoShapes、OLEオブジェクトなどです。 一部のフィールドは、INCLUDEPICTURE
フィールドなどの図形としてもインポートされます。
単純な画像は、ShapeType.ImageのShapeノードで表されます。 このshapeノードには子ノードはありませんが、このshapeノード内に含まれる画像データにはShape.ImageDataプロパティからアクセスできます。 一方、図形は多くの子ノードで構成することもできます。 たとえば、ShapeType.TextBoxプロパティで表されるテキストボックスの形状は、ParagraphやTableなどの多くのノードで構成できます。 ほとんどの図形には、ParagraphおよびTableブロックレベルのノードを含めることができます。 これらは、本体に表示されるノードと同じノードです。 図形は常にいくつかの段落の一部であり、直接インラインに含まれるか、**Paragraph,**に固定されていますが、ドキュメントページのどこにでも"浮動"します。
ドキュメントには、グループ化された図形を含めることもできます。 グループ化は、複数のオブジェクトを選択し、右クリックメニューの"グループ化"をクリックすることでMicrosoft Wordで有効にすることができます。
Aspose.Wordsでは、これらの図形のグループはGroupShapeノードで表されます。 これらは、グループ全体をイメージにレンダリングするのと同じ方法で呼び出すこともできます。
DOCX形式には、図やグラフなどの特殊な種類の画像を含めることができます。 これらの図形は、Aspose.WordsのShapeノードを介しても表現され、これも画像としてレンダリングするための同様の方法を提供します。 設計上、図形がイメージ(ShapeType.Image)でない限り、図形に別の図形を子として含めることはできません。 たとえば、Microsoft Wordでは、テキストボックスを別のテキストボックス内に挿入することはできません。
上で説明した図形の型は、ShapeRendererクラスを介して図形をレンダリングするための特別なメソッドを提供します。 ShapeRendererクラスのインスタンスは、GetShapeRendererメソッドを介して、またはShapeをShapeRendererクラスのコンストラクタに渡すことによって、ShapeまたはGroupShapeのために取得されます。 このクラスはメンバーへのアクセスを提供し、次の図形をレンダリングすることができます:
- Saveメソッドオーバーロードを使用してディスク上のファイル
- Saveメソッドオーバーロードを使用したストリーム
- RenderToSizeメソッドとRenderToScaleメソッドを使用したグラフィックスオブジェクト
ファイルまたはストリームへのレンダリング
Saveメソッドは、図形をファイルまたはストリームに直接レンダリングするオーバーロードを提供します。 どちらのオーバーロードもImageSaveOptionsクラスのインスタンスを受け入れ、図形をレンダリングするためのオプションを定義できます。 これはDocument.Saveメソッドと同じように機能します。 このパラメーターは必須ですが、カスタムオプションがないことを指定して、null値を渡すことができます。
図形は、SaveFormat列挙体で指定された任意の画像形式でエクスポートできます。 たとえば、イメージはSaveFormat.Jpeg列挙体を指定してJPEGなどのラスターイメージとして、またはSaveFormat.Emfを指定してEMFなどのベクターイメージとしてレンダリングできます。
次のコード例は、ドキュメントとは別にEMFイメージに図形をレンダリングし、ディスクに保存することを示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
ShapeRenderer r = shape.getShapeRenderer(); | |
// Define custom options which control how the image is rendered. Render the | |
// shape to the JPEG raster format. | |
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.EMF); | |
imageOptions.setScale(1.5f); | |
// Save the rendered image to disk. | |
r.save(dataDir + "TestFile.RenderToDisk_Out.emf", imageOptions); |
次のコード例は、ドキュメントとは別にJPEGイメージに図形をレンダリングし、ストリームに保存する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
ShapeRenderer r = new ShapeRenderer(shape); | |
// Define custom options which control how the image is rendered. Render the | |
// shape to the vector format EMF. | |
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG); | |
// Output the image in gray scale | |
imageOptions.setImageColorMode(ImageColorMode.GRAYSCALE); | |
// Reduce the brightness a bit (default is 0.5f). | |
imageOptions.setImageBrightness(0.45f); | |
FileOutputStream stream = new FileOutputStream(dataDir + "TestFile.RenderToStream_Out.jpg"); | |
// Save the rendered image to the stream using different options. | |
r.save(stream, imageOptions); |
ImageSaveOptionsクラスを使用すると、イメージのレンダリング方法を制御するさまざまなオプションを指定できます。 上記の機能は、GroupShapeおよびShapeノードにも同様に適用することができます。
Graphics
オブジェクトへのレンダリング
Graphicsオブジェクトに直接レンダリングすると、独自の設定とGraphicsオブジェクトの状態を定義できます。 一般的なシナリオでは、図形をWindowsフォームまたはビットマップから取得したGraphicsオブジェクトに直接レンダリングします。 Shapeノードがレンダリングされると、設定はシェイプの外観に影響します。 たとえば、Graphicsオブジェクトに対してRotateTransformメソッドまたはScaleTransformメソッドを使用して、図形を回転または拡大縮小できます。
次の例は、ドキュメントとは別にGraphicsオブジェクトに図形をレンダリングし、レンダリングされたイメージに回転を適用する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
// The shape renderer is retrieved using this method. This is made into a | |
// separate object from the shape as it internally | |
// caches the rendered shape. | |
ShapeRenderer r = shape.getShapeRenderer(); | |
// Find the size that the shape will be rendered to at the specified scale and | |
// resolution. | |
Dimension shapeSizeInPixels = r.getSizeInPixels(1.0f, 96.0f); | |
// Rotating the shape may result in clipping as the image canvas is too small. | |
// Find the longest side | |
// and make sure that the graphics canvas is large enough to compensate for | |
// this. | |
int maxSide = Math.max(shapeSizeInPixels.width, shapeSizeInPixels.height); | |
BufferedImage image = new BufferedImage((int) (maxSide * 1.25), (int) (maxSide * 1.25), | |
BufferedImage.TYPE_INT_ARGB); | |
// Rendering to a graphics object means we can specify settings and | |
// transformations to be applied to | |
// the shape that is rendered. In our case we will rotate the rendered shape. | |
Graphics2D gr = (Graphics2D) image.getGraphics(); | |
// Clear the shape with the background color of the document. | |
gr.setBackground(shape.getDocument().getPageColor()); | |
gr.clearRect(0, 0, image.getWidth(), image.getHeight()); | |
// Center the rotation using translation method below | |
gr.translate(image.getWidth() / 8, image.getHeight() / 2); | |
// Rotate the image by 45 degrees. | |
gr.rotate(45 * Math.PI / 180); | |
// Undo the translation. | |
gr.translate(-image.getWidth() / 8, -image.getHeight() / 2); | |
// Render the shape onto the graphics object. | |
r.renderToSize(gr, 0, 0, shapeSizeInPixels.width, shapeSizeInPixels.height); | |
ImageIO.write(image, "png", new File(dataDir + "TestFile.RenderToGraphics_out.png")); | |
gr.dispose(); |
同様に、RenderToSizeメソッドと同様に、NodeRendererBaseから継承されたRenderToSizeメソッドは、ドキュメントコンテンツのサムネイルを作成するのに便利です。 形状のサイズは、コンストラクタを介して指定されます。 RenderToSizeメソッドは、Graphicsオブジェクト、画像位置のX座標とY座標、およびGraphicsオブジェクトに描画される画像のサイズ(幅と高さ)を受け入れます。RenderToSizeメソッドは、Graphicsオブジェクト、画像位置のX座標とY座標、およびGraphicsオブジェクトに描画される画像のサイズ(幅と高さ)を受け入れます。
Shapeは、NodeRendererBaseクラスから継承されたShapeRenderer.RenderToScaleメソッドを使用して、特定のスケールにレンダリングできます。 これは、同じ主要なパラメーターを受け入れるDocument.RenderToScaleメソッドに似ています。 これらの2つのメソッドの違いは、ShapeRenderer.RenderToScaleメソッドでは、リテラルサイズではなく、レンダリング中にシェイプを拡大縮小するfloat値を選択することです。 Float値が1.0に等しい場合、シェイプは元のサイズの100%でレンダリングされます。 浮動小数点値0.5はイメージサイズを半分に縮小します。
シェイプイメージのレンダリング
Shapeクラスは、AutoShape、テキストボックス、フリーフォーム、OLEオブジェクト、ActiveXコントロール、画像など、描画レイヤー内のオブジェクトを表します。 Shapeクラスを使用すると、Microsoft Wordドキュメント内の図形を作成または変更できます。 図形の重要なプロパティは、そのShapeTypeです。 異なるタイプの図形は、Word文書で異なる機能を持つことができます。 たとえば、画像とOLE図形のみがその中に画像を持つことができますが、ほとんどの図形はテキストのみを持つことができます。
次の例は、シェイプイメージをドキュメントとは別にJPEGイメージにレンダリングし、ディスクに保存する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
dataDir = dataDir + "TestFile.RenderShape_out.jpg"; | |
// Save the Shape image to disk in JPEG format and using default options. | |
shape.getShapeRenderer().save(dataDir, null); |
図形サイズの取得
ShapeRendererクラスは、GetSizeInPixelsメソッドを使用して図形のサイズをピクセル単位で取得する機能も提供します。 このメソッドは、図形がレンダリングされるときに図形サイズの計算に使用されるスケールとDPIの2つの浮動小数点(単一)パラメータを受け入れます。 このメソッドは、計算されたサイズの幅と高さを含むSizeオブジェクトを返します。 これは、レンダリングされた出力から新しいビットマップを作成する場合など、レンダリングされた図形のサイズを事前に知る必要がある場合に
以下の例は、レンダリングする図形の幅と高さを持つ新しいBitmapオブジェクトとGraphicsオブジェクトを作成する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
Point2D.Float shapeSizeInDocument = shape.getShapeRenderer().getSizeInPoints(); | |
float width = shapeSizeInDocument.x; // The width of the shape. | |
float height = shapeSizeInDocument.y; // The height of the shape. | |
Dimension shapeRenderedSize = shape.getShapeRenderer().getSizeInPixels(1.0f, 96.0f); | |
BufferedImage image = new BufferedImage(shapeRenderedSize.width, shapeRenderedSize.height, | |
BufferedImage.TYPE_INT_RGB); | |
Graphics gr = image.getGraphics(); | |
// Render shape onto the graphics object using the RenderToScale or RenderToSize | |
// methods of ShapeRenderer class. | |
gr.dispose(); |
RenderToSizeまたはRenderToScaleメソッドを使用すると、レンダリングされた画像サイズもSizeFオブジェクトに返されます。 これは変数に代入して、必要に応じて使用することができます。
SizeInPointsプロパティは、ポイント単位で測定された形状サイズを返します(ShapeRendererを参照)。 結果は、幅と高さを含むSizeF
オブジェクトになります。