Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.Words を使用すると、ユーザーは非常に柔軟な方法で画像を操作できます。この記事では、画像を操作する可能性の一部のみを紹介します。
DocumentBuilder は、インラインまたはフローティング イメージを挿入できる insert_image メソッドのオーバーロードをいくつか提供します。画像が EMF または WMF メタファイルの場合、メタファイル形式でドキュメントに挿入されます。他のすべての画像は PNG 形式で保存されます。 画像の挿入 メソッドでは、さまざまなソースからの画像を使用できます。
URL からStream パラメータを渡してストリームから各 画像の挿入 メソッドには、次のオプションを使用してイメージを挿入できるオーバーロードがさらにあります。
画像を含むファイルを表す単一の文字列を insert_image に渡し、画像をインライン グラフィックとしてドキュメントに挿入します。
次のコード例は、ドキュメント内のカーソル位置にインライン イメージを挿入する方法を示しています。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.insert_image(docs_base.images_dir + "Logo.jpg")
doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_inline_image.doc")次のコード例は、ファイルまたは URL から指定された位置とサイズにフローティング イメージを挿入する方法を示しています。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.insert_image(docs_base.images_dir + "Logo.jpg",
aw.drawing.RelativeHorizontalPosition.MARGIN,
100,
aw.drawing.RelativeVerticalPosition.MARGIN,
100,
200,
100,
aw.drawing.WrapType.SQUARE)
doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_floating_image.doc")すべてのイメージは、Shape ノード内の Document に保存されます。すべての画像、または特定のタイプの画像をドキュメントから抽出するには、次の手順に従います。
次のコード例は、ドキュメントから画像を抽出してファイルとして保存する方法を示しています。
この例のテンプレート ファイルは ここ からダウンロードできます。
この例では、Word 文書のすべてのページまたは特定のページに同じまたは異なるバーコードを追加する方法を示します。ドキュメントのすべてのページにバーコードを追加する直接的な方法はありませんが、次のコードに示すように、move_to_section、move_to_header_footer、および insert_image メソッドを使用して任意のセクションまたはヘッダー/フッターに移動し、バーコード イメージを挿入できます。
次のコード例では、ドキュメントの各ページにバーコード イメージを挿入します。
幾何学的形状のアスペクト比は、さまざまな次元でのサイズの比率です。 aspect_ratio_locked を使用して画像のアスペクト比をロックできます。シェイプのアスペクト比のデフォルト値は ShapeType によって異なります。 ShapeType.IMAGE の場合は True、他の形状タイプの場合は False です。
次のコード例は、アスペクト比を操作する方法を示しています。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False
doc.save(docs_base.artifacts_dir+"WorkingWithImages.set_aspect_ratio_locked.doc")ページ上に描画される形状の実際の境界ボックスが必要な場合は、bounds_in_points プロパティを使用してこれを実現できます。
次のコード例は、このプロパティの使用方法を示しています。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False
print("\nGets the actual bounds of the shape in points.")
rect = shape.get_shape_renderer().bounds_in_points
print(f"{rect.x}, {rect.y}, {rect.width}, {rect.height}")画像のトリミングとは通常、フレーミングを改善するために画像の不要な外側部分を除去することを指します。また、特定の領域への焦点を高めるために画像の一部を削除するためにも使用されます。
次のコード例は、Aspose.Words API を使用してこれを実現する方法を示しています。
# The path to the documents directory.
inputPath = docs_base.images_dir + "Logo.jpg"
outputPath = docs_base.artifacts_dir + "cropped_logo.jpg"
self.crop_image(inputPath,outputPath, 100, 90, 200, 200)@staticmethod
def crop_image(inPath : str, outPath : str, left : int, top : int, width : int, height : int) :
doc = aw.Document();
builder = aw.DocumentBuilder(doc)
croppedImage = builder.insert_image(inPath)
src_width_points = croppedImage.width
src_height_points = croppedImage.height
croppedImage.width = aw.ConvertUtil.pixel_to_point(width)
croppedImage.height = aw.ConvertUtil.pixel_to_point(height)
widthRatio = croppedImage.width / src_width_points
heightRatio = croppedImage.height / src_height_points
if (widthRatio< 1) :
croppedImage.image_data.crop_right = 1 - widthRatio
if (heightRatio< 1) :
croppedImage.image_data.crop_bottom = 1 - heightRatio
leftToWidth = aw.ConvertUtil.pixel_to_point(left) / src_width_points
topToHeight = aw.ConvertUtil.pixel_to_point(top) / src_height_points
croppedImage.image_data.crop_left = leftToWidth
croppedImage.image_data.crop_right = croppedImage.image_data.crop_right - leftToWidth
croppedImage.image_data.crop_top = topToHeight
croppedImage.image_data.crop_bottom = croppedImage.image_data.crop_bottom - topToHeight
croppedImage.get_shape_renderer().save(outPath, aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG))Aspose.Words は、DOCX を RTF に変換しながら、ドキュメント内の利用可能なすべての画像を WMF 形式で保存する機能を提供します。
次のコード例は、RTF 保存オプションを使用して画像を WMF として保存する方法を示しています。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.