このクイック チュートリアルでは、Java を使用して Word で画像のサイズを変更する方法 について説明します。これには、環境設定に関する情報、アプリケーションを作成するための段階的なプロセス、Java を使用して Word で画像サイズを変更するための実行可能なサンプル コードが含まれています。このアプリケーションを作成し、サイズ変更された画像を含む Word ドキュメントを DOCX、DOC、またはその他の必要な形式で保存するために必要な、重要なクラス、メソッド、およびプロパティの概要を説明します。
Java を使用して Word で写真のサイズを変更する手順
- Aspose.Words for Java を使用してサイズ変更された画像を追加するように IDE を設定します
- Document クラス オブジェクトを使用して Word ファイルを作成または読み込み、カスタム サイズの画像を追加します
- Document オブジェクトを使用して DocumentBuilder クラス オブジェクトをインスタンス化する
- write() メソッドを使用してオプションのテキストを書き込みます
- insertImage() メソッドを使用して画像を挿入し、参照を取得してサイズを変更します
- 幅と高さを設定して画像のサイズを変更し、ドキュメントを保存します
これらの手順では、Java を使用して Word で画像のサイズを変更する方法 について説明します。環境設定を共有し、Document クラス オブジェクトを使用して Word ファイルを作成します。 DocumentBuilder クラスは、write() メソッドを使用してオプションのテキストを追加し、insertImage() メソッドを使用して画像ファイル名とパスを指定して画像を挿入するために使用されます。 Shape クラス オブジェクトは、setWidth() および setHeight() メソッドを使用して幅と高さを設定するために使用される insertImage() によって返されます。
Java を使用して MS Word で画像のサイズを変更するコード
import com.aspose.words.ConvertUtil; | |
import com.aspose.words.Document; | |
import com.aspose.words.DocumentBuilder; | |
import com.aspose.words.License; | |
import com.aspose.words.Shape; | |
public class AsposeTest { | |
public static void main(String[] args) throws Exception {//Main function to add resized image to a Word file using Java | |
// Instantiate the license | |
License lic = new License(); | |
lic.setLicense("Aspose.Total.lic"); | |
// Instantiate a new Document object | |
Document wordDoc = new Document(); | |
// Create a DocumentBuilder and initialize it with the Document class object | |
DocumentBuilder documentBuilder = new DocumentBuilder(wordDoc); | |
// Write some text for reference to the document before inserting an image | |
documentBuilder.write("Here is the image with its original size"); | |
// Insert an image with its original size | |
Shape image = documentBuilder.insertImage("sampleImage.jpg"); | |
// Write some sample text before the image that will be resized | |
documentBuilder.write("Following image is resized"); | |
// Insert another image and get its reference to change size | |
image = documentBuilder.insertImage("sampleImage.jpg"); | |
// Set image properties width and height | |
image.setWidth(ConvertUtil.inchToPoint(0.60)); | |
image.setHeight(ConvertUtil.inchToPoint(0.60)); | |
// Save the document with a resized image in it | |
wordDoc.save("FileWithResizedImages.docx"); | |
System.out.println("Done"); | |
} | |
} |
このサンプル コードは、Java を使用して Word で画像のサイズを変更する プロセスを示しています。これは、ユーティリティ クラス ConvertUtil を使用してポイントに変換される必要なパラメーターをインチ単位で提供することにより、setWidth() および setHeight() メソッドを使用して、Shape クラス オブジェクトを使用してサイズを設定します。 setBehindText() メソッドを使用して画像をテキストの後ろに配置し、setBounds() メソッドを使用して配置を定義し、setHorizontalAlignment() および setVerticalAlignment() メソッドを使用して形状の配置を設定することにより、画像の他のプロパティも設定できます。
このチュートリアルでは、Java を使用して Word で画像のサイズを変更する 方法について説明しました。 Word 文書に透かしを追加するプロセスを知りたい場合は、Javaを使用してWordに透かしを追加する方法 の記事を参照してください。