画像の操作
Aspose.Words を使用すると、ユーザーは非常に柔軟な方法で画像を操作できます。この記事では、画像を操作する可能性の一部のみを紹介します。
画像{#insert-an-image}を挿入する方法
DocumentBuilder は、インラインまたはフローティング イメージを挿入できる InsertImage メソッドのオーバーロードをいくつか提供します。画像が EMF または WMF メタファイルの場合、メタファイル形式でドキュメントに挿入されます。他のすべての画像は PNG 形式で保存されます。 InsertImage メソッドでは、さまざまなソースからの画像を使用できます。
- ファイルまたは
URL
からString
パラメータ InsertImage を渡して送信 Stream
パラメータ InsertImage を渡すことによるストリームからの送信- Image パラメータ InsertImage を渡して Image オブジェクトから
- バイト配列パラメータ InsertImage を渡すことによるバイト配列から
各 InsertImage メソッドには、次のオプションを使用してイメージを挿入できるオーバーロードがさらにあります。
- 特定の位置でのインラインまたはフローティング (InsertImage など)
- パーセンテージ スケールまたはカスタム サイズ (InsertImage など)。さらに、InsertImage メソッドは作成および挿入されたばかりの Shape オブジェクトを返すため、Shape のプロパティをさらに変更できます。
インライン画像{#insert-an-inline-image}を挿入する方法
画像を含むファイルを表す単一の文字列を InsertImage に渡し、画像をインライン グラフィックとしてドキュメントに挿入します。
次のコード例は、ドキュメント内のカーソル位置にインライン イメージを挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertImage(dataDir + "Watermark.png"); | |
dataDir = dataDir + "DocumentBuilderInsertInlineImage_out.doc"; | |
doc.Save(dataDir); |
フローティング イメージ {#insert-a-floating-image} を挿入する方法
次のコード例は、ファイルまたは URL
から指定された位置とサイズにフローティング イメージを挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertImage(dataDir + "Watermark.png", | |
RelativeHorizontalPosition.Margin, | |
100, | |
RelativeVerticalPosition.Margin, | |
100, | |
200, | |
100, | |
WrapType.Square); | |
dataDir = dataDir + "DocumentBuilderInsertFloatingImage_out.doc"; | |
doc.Save(dataDir); |
ドキュメントから画像を抽出する方法
すべてのイメージは、Shape ノード内の Document に保存されます。すべての画像、または特定のタイプの画像をドキュメントから抽出するには、次の手順に従います。
- GetChildNodes メソッドを使用して、すべての Shape ノードを選択します。
- 結果として得られるノード コレクションを反復処理します。
- HasImage ブール値プロパティを確認します。
- ImageData プロパティを使用して画像データを抽出します。 ・画像データをファイルに保存します。
次のコード例は、ドキュメントから画像を抽出してファイルとして保存する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
Document doc = new Document(dataDir + "Image.SampleImages.doc"); | |
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); | |
int imageIndex = 0; | |
foreach (Shape shape in shapes) | |
{ | |
if (shape.HasImage) | |
{ | |
string imageFileName = string.Format( | |
"Image.ExportImages.{0}_out{1}", imageIndex, FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)); | |
shape.ImageData.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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
// Create a blank documenet. | |
Document doc = new Document(); | |
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.FirstSection, 1, HeaderFooterType.FooterPrimary); | |
for (int i = 1; i < numPages; i++) | |
{ | |
// Clone the first section and add it into the end of the document. | |
Section cloneSection = (Section)doc.FirstSection.Clone(false); | |
cloneSection.PageSetup.SectionStart = SectionStart.NewPage; | |
doc.AppendChild(cloneSection); | |
// Insert the barcode and other information into the footer of the section. | |
InsertBarcodeIntoFooter(builder, cloneSection, i, HeaderFooterType.FooterPrimary); | |
} | |
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-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
実際の形状の境界をポイント単位で取得する方法
ページ上に描画される形状の実際の境界ボックスが必要な場合は、BoundsInPoints プロパティを使用してこれを実現できます。
次のコード例は、このプロパティの使用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
Console.Write("\nGets the actual bounds of the shape in points."); | |
Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints); |
画像のトリミング
画像のトリミングとは通常、フレーミングを改善するために画像の不要な外側部分を除去することを指します。また、特定の領域への焦点を高めるために画像の一部を削除するためにも使用されます。
次のコード例は、Aspose.Words API を使用してこれを実現する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
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-.NET | |
public static void CropImage(string inPath, string outPath, int left, int top,int width, int height) | |
{ | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Image img = Image.FromFile(inPath); | |
int effectiveWidth = img.Width - width; | |
int effectiveHeight = img.Height - height; | |
Shape croppedImage = builder.InsertImage(img, | |
ConvertUtil.PixelToPoint(img.Width - effectiveWidth), | |
ConvertUtil.PixelToPoint(img.Height - effectiveHeight)); | |
double widthRatio = croppedImage.Width / ConvertUtil.PixelToPoint(img.Width); | |
double heightRatio = croppedImage.Height / ConvertUtil.PixelToPoint(img.Height); | |
if (widthRatio< 1) | |
croppedImage.ImageData.CropRight = 1 - widthRatio; | |
if (heightRatio< 1) | |
croppedImage.ImageData.CropBottom = 1 - heightRatio; | |
float leftToWidth = (float)left / img.Width; | |
float topToHeight = (float)top / img.Height; | |
croppedImage.ImageData.CropLeft = leftToWidth; | |
croppedImage.ImageData.CropRight = croppedImage.ImageData.CropRight - leftToWidth; | |
croppedImage.ImageData.CropTop = topToHeight; | |
croppedImage.ImageData.CropBottom = croppedImage.ImageData.CropBottom - topToHeight; | |
croppedImage.GetShapeRenderer().Save(outPath, new ImageSaveOptions(SaveFormat.Jpeg)); | |
} |
画像を WMF {#save-images-as-wmf} として保存
Aspose.Words は、DOCX を RTF に変換しながら、ドキュメント内の利用可能なすべての画像を WMF 形式で保存する機能を提供します。
次のコード例は、RTF 保存オプションを使用して画像を WMF として保存する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
string fileName = "TestFile.doc"; | |
Document doc = new Document(dataDir + fileName); | |
RtfSaveOptions saveOpts = new RtfSaveOptions(); | |
saveOpts.SaveImagesAsWmf = true; | |
doc.Save(dataDir + "output.rtf", saveOpts); |