画像の操作
Aspose.Wordsを使用すると、ユーザーは非常に柔軟な方法で画像を操作できます。 この記事では、画像を操作する可能性のいくつかのみを探索できます。
文書 {#how-to-extract-images-from-a-document}から画像を抽出する方法
すべての画像はドキュメント内のShapeノード内に保存されます。 すべての画像または特定の種類の画像をドキュメントから抽出するには、次の手順を実行します:
- すべてのシェイプノードを選択するには、getChildNodesメソッドを使用します。
- 結果のノードコレクションを反復処理します。
- ブールプロパティhasImageを確認します。
- ImageDataプロパティを使用してイメージデータを抽出します。
- 画像データをファイルに保存します。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ExtractImagesToFiles.class); | |
Document doc = new Document(dataDir + "Image.SampleImages.doc"); | |
NodeCollection<Shape> shapes = (NodeCollection<Shape>) doc.getChildNodes(NodeType.SHAPE, true); | |
int imageIndex = 0; | |
for (Shape shape : shapes | |
) { | |
if (shape.hasImage()) { | |
String imageFileName = String.format( | |
"Image.ExportImages.{0}_out_{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); | |
shape.getImageData().save(dataDir + imageFileName); | |
imageIndex++; | |
} | |
} |
各文書ページにバーコードを挿入する方法
この例では、Word文書のすべてのページまたは特定のページに同じまたは異なるバーコードを追加できます。 ドキュメントのすべてのページにバーコードを直接追加する方法はありませんが、moveToSection、moveToHeaderFooter、insertImageメソッドを使用して任意のセクションまたはヘッダー/フッターに移動し、次のコードでわかるようにバーコード画像を挿入することができます。
次のコード例は、ドキュメントの各ページにバーコードイメージを挿入する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(InsertBarcodeImage.class); | |
Document doc = new Document(dataDir + "Image.SampleImages.doc"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// The number of pages the document should have. | |
int numPages = 4; | |
// The document starts with one section, insert the barcode into this existing section. | |
InsertBarcodeIntoFooter(builder, doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY); | |
InsertBarcodeIntoFooter(builder, doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY); | |
for (int i = 1; i < numPages; i++) { | |
// Clone the first section and add it into the end of the document. | |
Section cloneSection = (Section) doc.getFirstSection().deepClone(false); | |
// cloneSection.getPageSetup().getSectionStart() = SectionStart.NEW_PAGE; | |
doc.appendChild(cloneSection); | |
// Insert the barcode and other information into the footer of the section. | |
InsertBarcodeIntoFooter(builder, cloneSection, i, HeaderFooterType.FOOTER_PRIMARY); | |
} | |
dataDir = dataDir + "Document_out_.docx"; | |
// Save the document as a PDF to disk. You can also save this directly to a stream. | |
doc.save(dataDir); |
イメージ {#lock-aspect-ratio-of-image}のアスペクト比をロックする
幾何学的形状のアスペクト比は、異なる寸法におけるそのサイズの比である。 AspectRatioLockedを使用して画像のアスペクト比をロックできます。 シェイプのアスペクト比のデフォルト値はShapeTypeに依存します。 これはShapeType.Image
ではtrue、他の図形タイプではfalseです。
次のコード例は、アスペクト比を操作する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(DocumentBuilderSetImageAspectRatioLocked.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(false); | |
doc.save(dataDir + "output.doc"); |
点 {#how-to-get-actual-bounds-of-shape-in-points}で形状の実際の境界を取得する方法
ページ上にレンダリングされた形状の実際の境界ボックスが必要な場合は、BoundsInPointsプロパティを使用してこれを実現できます。
次のコード例は、このプロパティを使用する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.insertImage(dataDir + "Test.png"); | |
shape.setAspectRatioLocked(false); | |
System.out.print("\nGets the actual bounds of the shape in points. "); | |
System.out.println(shape.getShapeRenderer().getBoundsInPoints()); |
画像の切り抜き
画像のトリミングとは、通常、フレーミングを改善するために、画像の不要な外側部分を除去することを指します。 また、特定の領域への焦点を増やすために、画像の一部を除去するためにも使用されます。
次のコード例は、Aspose.WordsAPIを使用してこれを実現する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CropImages.class); | |
String inputPath = dataDir + "ch63_Fig0013.jpg"; | |
String outputPath = dataDir + "cropped-1.jpg"; | |
cropImage(inputPath, outputPath, 124, 90, 570, 571); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void cropImage(String inPath, String outPath, int left, int top, int width, int height) throws Exception { | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
BufferedImage img = ImageIO.read(new File(inPath)); | |
int effectiveWidth = img.getWidth() - width; | |
int effectiveHeight = img.getHeight() - height; | |
Shape croppedImage = builder.insertImage(img, | |
ConvertUtil.pixelToPoint(img.getWidth() - effectiveWidth), | |
ConvertUtil.pixelToPoint(img.getHeight() - effectiveHeight)); | |
double widthRatio = croppedImage.getWidth() / ConvertUtil.pixelToPoint(img.getWidth()); | |
double heightRatio = croppedImage.getHeight() / ConvertUtil.pixelToPoint(img.getHeight()); | |
if (widthRatio < 1) | |
croppedImage.getImageData().setCropRight(1 - widthRatio); | |
if (heightRatio < 1) | |
croppedImage.getImageData().setCropBottom(1 - heightRatio); | |
float leftToWidth = (float) left / img.getWidth(); | |
float topToHeight = (float) top / img.getHeight(); | |
croppedImage.getImageData().setCropLeft(leftToWidth); | |
croppedImage.getImageData().setCropRight(croppedImage.getImageData().getCropRight() - leftToWidth); | |
croppedImage.getImageData().setCropTop(topToHeight); | |
croppedImage.getImageData().setCropBottom(croppedImage.getImageData().getCropBottom() - topToHeight); | |
croppedImage.getShapeRenderer().save(outPath, new ImageSaveOptions(SaveFormat.JPEG)); | |
} |
画像をWMFとして保存する
Aspose.Wordsは、ドキュメント内の使用可能なすべての画像を保存する機能を提供します。 WMFDOCXをRTFに変換している間にフォーマットします。
次のコード例は、RTF保存オプションを使用して画像をWMFとして保存する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String fileName = "TestFile.doc"; | |
Document doc = new Document(dataDir + fileName); | |
RtfSaveOptions saveOpts = new RtfSaveOptions(); | |
saveOpts.setSaveImagesAsWmf(true); | |
doc.save(dataDir + "output.rtf", saveOpts); |