DocumentBuilderを使用して文書要素を挿入します
DocumentBuilderは文書を変更するために使用されます。 この記事では、いくつかのタスクを実行する方法について説明します。
テキストの文字列を挿入する
ドキュメントに挿入する必要があるテキストの文字列をDocumentBuilder.writeメソッドに渡すだけです。 テキストの書式設定はFont
プロパティによって決まります。 このオブジェクトには、さまざまなフォント属性(フォント名、フォントサイズ、色など)が含まれています。 いくつかの重要なフォント属性は、直接アクセスできるようにDocumentBuilderプロパティでも表されます。 これらはブール値のプロパティFont.getBold、Font.getItalic、およびFont.getUnderlineです。
次のコード例では、DocumentBuilderを使用して書式設定されたテキストを挿入します。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Font font = builder.getFont(); | |
font.setSize(16); | |
font.setColor(Color.blue); | |
font.setBold(true); | |
font.setName("Algerian"); | |
font.setUnderline(Underline.DOUBLE); | |
builder.write("aspose......... aspose_words_java"); |
段落の挿入
DocumentBuilder.writelnは、文書にもテキストの文字列を挿入しますが、さらに段落区切りを追加します。 現在のフォントの書式設定もDocumentBuilderで指定されます。getFontプロパティと現在の段落の書式はDocumentBuilderによって決まります。getParagraphFormatプロパティ。
次のコード例は、文書に段落を挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Font font = builder.getFont(); | |
font.setSize(16); | |
font.setColor(Color.DARK_GRAY); | |
font.setBold(true); | |
font.setName("Algerian"); | |
font.setUnderline(2); | |
ParagraphFormat paragraphFormat = builder.getParagraphFormat(); | |
paragraphFormat.setFirstLineIndent(12); | |
paragraphFormat.setAlignment(1); | |
paragraphFormat.setKeepTogether(true); | |
builder.write("This is a sample Paragraph"); | |
doc.save(dataDir + "InsertParagraph_out.doc"); |
テーブルの挿入
DocumentBuilder
を使用してテーブルを作成する基本的なアルゴリズムは簡単です:
- DocumentBuilder.startTableを使用してテーブルを開始します。
- DocumentBuilder.insertCellを使用してセルを挿入します。 これにより、新しい行が自動的に開始されます。 必要に応じて、DocumentBuilder.getCellFormatプロパティを使用してセルの書式を指定します。
DocumentBuilder
メソッドを使用してセルの内容を挿入します。- 行が完了するまで、手順2と3を繰り返します。
- 現在の行を終了するにはDocumentBuilder.endRowを呼び出します。 必要に応じて、DocumentBuilder.RowFormatプロパティを使用して行の書式を指定します。
- テーブルが完了するまで、手順2〜5を繰り返します。
- テーブルの構築を終了するにはDocumentBuilder.endTableを呼び出します。 適切なDocumentBuilderテーブルの作成方法については、以下で説明します。
テーブルの開始
DocumentBuilderを呼び出します。startTableは、テーブルを構築する最初のステップです。 セル内で呼び出すこともでき、この場合はネストされたテーブルを開始します。 次に呼び出すメソッドはDocumentBuilderです。insertCell.
セルの挿入
DocumentBuilderを呼び出した後。insertCellの場合、新しいセルが作成され、DocumentBuilder
クラスの他のメソッドを使用して追加したコンテンツが現在のセルに追加されます。 同じ行の新しいセルを開始するには、DocumentBuilderを呼び出します。insertCell再び。 DocumentBuilderを使用します。getCellFormatセルの書式設定を指定するプロパティ。 テーブルセルのすべての書式設定を表すgetCellFormatオブジェクトを返します。
行を終了する
DocumentBuilderを呼び出します。endRow現在の行を終了します。 あなたがDocumentBuilderを呼び出す場合。insertCellその直後に、テーブルは新しい行で続行されます。 行の書式設定を指定するには、DocumentBuilder.RowFormat
プロパティを使用します。 テーブル行のすべての書式設定を表すRowFormatオブジェクトを返します。
テーブルの終了
DocumentBuilderを呼び出します。endTable現在のテーブルを終了します。 このメソッドはDocumentBuilderの後に一度だけ呼び出す必要があります。endRowが呼び出されました。 呼び出されたとき、DocumentBuilder。endTableカーソルを現在のセルからテーブルの直後の位置に移動します。 次の例は、2つの行と2つの列を含む書式設定されたテーブルを作成する方法を示しています。
// 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(DocumentBuilderBuildTable.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS); | |
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); | |
builder.write("This is Row 1 Cell 1"); | |
builder.insertCell(); | |
builder.write("This is Row 1 Cell 2"); | |
builder.endRow(); | |
builder.getRowFormat().setHeight(100); | |
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY); | |
builder.getCellFormat().setOrientation(TextOrientation.UPWARD); | |
builder.write("This is Row 2 Cell 1"); | |
builder.insertCell(); | |
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD); | |
builder.write("This is Row 2 Cell 2"); | |
builder.endRow(); | |
builder.endTable(); | |
doc.save(dataDir + "output.doc"); |
休憩を挿入する
新しい行、段落、列、セクション、またはページを明示的に開始する場合は、DocumentBuilderを呼び出します。insertBreak. このメソッドに、BreakType
列挙体で表される挿入する必要のあるブレークの型を渡します。
次のコード例は、文書に改ページを挿入する方法を示しています。
// 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(DocumentBuilderInsertBreak.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.write("This is Page 1"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.write("This is Page 2"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.write("This is Page 3"); | |
doc.save(dataDir + "output.doc"); |
画像の挿入
DocumentBuilder は、インライン イメージまたはフローティング イメージを挿入できる DocumentBuilder.insertImage メソッドのオーバーロードをいくつか提供します。イメージが EMF または WMF メタファイルの場合、メタファイル形式でドキュメントに挿入されます。その他のすべてのイメージは PNG 形式で保存されます。DocumentBuilder.insertImage メソッドでは、さまざまなソースからのイメージを使用できます。
- 文字列パラメータを渡すことにより、ファイルまたは
URL
から Stream
パラメータを渡してストリームから- Imageパラメータを渡してImageオブジェクトから
- バイト配列パラメータを渡すことにより、バイト配列から
- その他
DocumentBuilderのそれぞれについて。insertImageメソッドでは、次のオプションを使用して画像を挿入できるようにするさらにオーバーロードがあります:
- 特定の位置でインラインまたはフローティング
- パーセントのスケールか注文のサイズ
さらに、DocumentBuilder。insertImageメソッドは、シェイプのプロパティをさらに変更できるように、作成され挿入されたShapeオブジェクトを返します。
インラインイメージの挿入
イメージを含むファイルを表す単一の文字列をDocumentBuilderに渡します。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(DocumentBuilderInsertInlineImage.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertImage(dataDir + "test.jpg"); | |
doc.save(dataDir + "output.doc"); |
フローティング(絶対配置)イメージの挿入
この例では、指定された位置とサイズのファイルまたはURL
から浮動イメージを挿入します。
// 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(DocumentBuilderInsertFloatingImage.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertImage(dataDir + "test.jpg", | |
RelativeHorizontalPosition.MARGIN, | |
100, | |
RelativeVerticalPosition.MARGIN, | |
100, | |
200, | |
100, | |
WrapType.SQUARE); | |
doc.save(dataDir + "output.doc"); |
ブックマークの挿入
ドキュメントにブックマークを挿入するには、次の操作を行います:
- DocumentBuilder.startBookmarkを呼び出して、ブックマークの目的の名前を渡します。
DocumentBuilder
メソッドを使用してブックマークテキストを挿入します。- DocumentBuilder.endBookmarkを呼び出して、DocumentBuilderで使用したのと同じ名前を渡します。startBookmark.
ブックマークは、任意の範囲に重複してまたがることができます。 有効なブックマークを作成するには、両方DocumentBuilderを呼び出す必要があります。startBookmarkとDocumentBuilder。同じブックマーク名を持つendBookmark。
不適切に形成されたブックマークまたは重複した名前のブックマークは、文書を保存するときに無視されます。
次のコード例は、ドキュメントビルダーを使用してドキュメントにブックマークを挿入する方法を示しています。
// 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(DocumentBuilderInsertBookmark.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.startBookmark("FineBookMark"); | |
builder.write("This is just a fine bookmark."); | |
builder.endBookmark("FineBookmark"); | |
doc.save(dataDir + "output.doc");// |
フィールドの挿入
Microsoft Word文書内のフィールドは、フィールドコードとフィールド結果で構成されます。 フィールドコードは数式のようなもので、フィールドの結果は数式が生成する値です。 フィールドコードには、特定のアクションを実行するための追加の命令であるフィールドスイッチも含まれている場合があります。 キーボードショートカットAlt+F9を使用して、Microsoft Wordのドキュメント内のフィールドコードと結果の表示を切り替えることができます。 フィールドコードは中括弧({ }
)の間に表示されます。ドキュメント内にフィールドを作成するにはDocumentBuilder.insertFieldを使用します。 フィールドタイプ、フィールドコード、およびフィールド値を指定する必要があります。 特定のフィールドコードの構文がわからない場合は、まずMicrosoft Wordにフィールドを作成し、そのフィールドコードを表示するように切り替えます。
次のコード例では、DocumentBuilderを使用して差し込み項目をドキュメントに挿入します。
// 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(DocumentBuilderInsertField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.getFont().setLocaleId(1031); | |
builder.insertField("MERGEFIELD Date1 \\@ \"dddd, d MMMM yyyy\""); | |
builder.write(" - "); | |
builder.insertField("MERGEFIELD Date2 \\@ \"dddd, d MMMM yyyy\""); | |
doc.save(dataDir + "output.doc"); |
Form
フィールドの挿入
フォームフィールドは、ユーザーとの"対話"を可能にする単語フィールドの特定のケースです。 Microsoft Wordのフォームフィールドには、textbox、Combobox、checkboxが含まれます。DocumentBuilderは、DocumentBuilder.insertTextInput、DocumentBuilder.insertCheckBox、DocumentBuilder.insertComboBoxの各タイプのフォームフィールドをドキュメントに挿入するための特別なメソッドを提供します。 フォームフィールドに名前を指定すると、同じ名前のブックマークが自動的に作成されることに注意してください。
テキスト入力の挿入
DocumentBuilder.insertTextInputテキストボックスを文書に挿入します。 次のコード例は、テキスト入力フォームフィールドをドキュメントに挿入する方法を示しています。
// 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(DocumentBuilderInsertTextInputFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertTextInput("TextInput", TextFormFieldType.REGULAR, "", "Hello", 0); | |
doc.save(dataDir + "output.doc"); |
CheckBox
の挿入
DocumentBuilderを呼び出します。insertCheckBox文書にcheckboxを挿入します。 次のコード例は、checkboxフォームフィールドをドキュメントに挿入する方法を示しています。
// 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(DocumentBuilderInsertCheckBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertCheckBox("CheckBox", true, true, 0); | |
doc.save(dataDir + "output.doc"); |
コンボボックスの挿入
DocumentBuilderを呼び出します。insertComboBoxコンボボックスをドキュメントに挿入します。 コンボボックスフォームフィールドをドキュメントに挿入する方法を次のコード例に示します。
// 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(DocumentBuilderInsertComboBoxFormField.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
String[] items = {"One", "Two", "Three"}; | |
builder.insertComboBox("DropDown", items, 0); | |
doc.save(dataDir + "output.doc"); |
フィールドレベルでのロケールの挿入
顧客は今、フィールドレベルでロケールを指定することができ、より良い制御を達成することができます。 ロケールIdはDocumentBuilder内の各フィールドに関連付けることができます。 以下の例は、このオプションを使用する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
Field field = builder.insertField(FieldType.FIELD_DATE, true); | |
field.setLocaleId(1049); | |
builder.getDocument().save(getArtifactsDir() + "WorkingWithFields.SpecifylocaleAtFieldlevel.docx"); |
HTMLの挿入
HTMLフラグメントまたはHTML文書全体を含むHTML文字列をWord文書に簡単に挿入できます。 この文字列をDocumentBuilderに渡すだけです。insertHtmlメソッド。 このメソッドの便利な実装の一つは、HTML文字列をデータベースに格納し、Mail Merge中にドキュメントに挿入して、ドキュメントビルダのさまざまなメソッドを 次のコード例は、DocumentBuilderを使用して文書にHTMLを挿入することを示しています。
// 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(DocumentBuilderInsertHtml.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertHtml( | |
"<P align='right'>Paragraph right</P>" + | |
"<b>Implicit paragraph left</b>" + | |
"<div align='center'>Div center</div>" + | |
"<h1 align='left'>Heading 1 left.</h1>"); | |
doc.save(dataDir + "output.doc"); |
ハイパーリンクの挿入
DocumentBuilderを使用します。insertHyperlink文書にハイパーリンクを挿入します。 このメソッドは、ドキュメント内に表示されるリンクのテキスト、リンク先(URLまたはドキュメント内のブックマークの名前)、およびURL
がドキュメント内のブックマークの名前である場合にtrueにする必要があるブールパラメータの三つのパラメータを受け入れます。DocumentBuilder.insertHyperlinkは内部的にDocumentBuilderを呼び出します。insertField. このメソッドは常にURLの先頭と末尾にアポストロフィを追加します。 Font
プロパティを使用して、ハイパーリンク表示テキストのフォント書式を明示的に指定する必要があることに注意してください。 次のコード例では、DocumentBuilderを使用してドキュメントにハイパーリンクを挿入します。
// 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); | |
builder.write("Please make sure to visit "); | |
builder.getFont().setColor(Color.BLUE); | |
builder.getFont().setUnderline(Underline.SINGLE); | |
builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); | |
builder.getFont().clearFormatting(); | |
builder.write(" for more information."); | |
doc.save(getArtifactsDir() + "AddContentUsingDocumentBuilder.InsertHyperlink.docx"); |
目次の挿入
DocumentBuilder.insertTableOfContents メソッドを呼び出すと、ドキュメントの現在の位置に TOC
(目次) フィールドを挿入できます。DocumentBuilder.insertTableOfContents メソッドは、ドキュメントに TOC
フィールドのみを挿入します。目次を構築し、ページ番号に従って表示するには、フィールドの挿入後に両方の Document.UpdateFields メソッドを呼び出す必要があります。次のコード例は、ドキュメントに目次フィールドを挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(DocumentBuilderInsertTableOfContents.class); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u"); | |
builder.insertBreak(BreakType.PAGE_BREAK); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); | |
builder.writeln("Heading 1"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 1.1"); | |
builder.writeln("Heading 1.2"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1); | |
builder.writeln("Heading 2"); | |
builder.writeln("Heading 3"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 3.1"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3); | |
builder.writeln("Heading 3.1.1"); | |
builder.writeln("Heading 3.1.2"); | |
builder.writeln("Heading 3.1.3"); | |
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2); | |
builder.writeln("Heading 3.2"); | |
builder.writeln("Heading 3.3"); | |
doc.updateFields(); | |
doc.save(dataDir + "output.doc"); |
Oleオブジェクトの挿入
Oleオブジェクトが必要な場合はDocumentBuilder.insertOleObjectAsIconを呼び出します。
// 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); | |
builder.insertOleObject("http://www.aspose.com", "htmlfile", true, true, null); | |
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObject.docx"); |
Oleオブジェクトを挿入するときにファイル名と拡張子を設定する
OLEパッケージは、OLEハンドラーが不明な場合に埋め込まれたオブジェクトを格納するためのレガシーで"文書化されていない"方法です。 Windows 3.1、95、98などの初期のWindowsバージョンには、任意のタイプのデータを文書に埋め込むために使用できるPackager.exeアプリケーションがありました。 現在、このアプリケーションはWindowsから除外されていますが、MSWordや他のアプリケーションは、OLEハンドラーが欠落しているか不明な場合にデータを埋め込むた OlePackageクラスはOLE Packageプロパティへのアクセスを許可します。次のコード例は、OLE Packageのファイル名、拡張子、表示名を設定する方法を示しています。
// 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); | |
byte[] bs = FileUtils.readFileToByteArray(new File(getMyDir() + "Zip file.zip")); | |
try (ByteArrayInputStream stream = new ByteArrayInputStream(bs)) | |
{ | |
Shape shape = builder.insertOleObject(stream, "Package", true, null); | |
OlePackage olePackage = shape.getOleFormat().getOlePackage(); | |
olePackage.setFileName("filename.zip"); | |
olePackage.setDisplayName("displayname.zip"); | |
doc.save(getArtifactsDir() + "WorkingWithOleObjectsAndActiveX.InsertOleObjectWithOlePackage.docx"); | |
} |
OLEオブジェクトの生データへのアクセスを取得する
次のコード例は、OleFormat.GetRawData
()メソッドを使用してOLEオブジェクトの生データを取得する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Shape oleShape = (Shape) doc.getChild(NodeType.SHAPE, 0, true); | |
byte[] oleRawData = oleShape.getOleFormat().getRawData(); |
水平ルールを文書に挿入する
次のコード例は、DocumentBuilder.InsertHorizontalRule
メソッドを使用して水平ルール形状をドキュメントに挿入する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.writeln("Insert a horizontal rule shape into the document."); | |
builder.insertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.save(dataDir); |
図形の操作
インラインおよびフリーフローティング図形の挿入
DocumentBuilder.InsertShape
メソッドを使用して、タイプとサイズが指定されたインライン シェイプと、位置、サイズ、テキスト ラップ タイプが指定されたフリー フローティング シェイプをドキュメントに挿入できます。DocumentBuilder.InsertShape
メソッドを使用すると、ドキュメント モデルに DML シェイプを挿入できます。ドキュメントは、DML シェイプをサポートする形式で保存する必要があります。そうでない場合、ドキュメントの保存時に、このようなノードは VML シェイプに変換されます。次のコード例は、これらのタイプのシェイプをドキュメントに挿入する方法を示しています。
// 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); | |
//Free-floating shape insertion. | |
Shape shape = builder.insertShape(ShapeType.TEXT_BOX, | |
RelativeHorizontalPosition.PAGE, 100, | |
RelativeVerticalPosition.PAGE, 100, | |
50, 50, | |
WrapType.NONE); | |
shape.setRotation(30.0); | |
builder.writeln(); | |
//Inline shape insertion. | |
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50); | |
shape.setRotation(30.0); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir, so); |
スニップ角の長方形を作成します。
Aspose.Wordsを使用してスニップ角の長方形を作成できます。 形のタイプは次のとおりですSingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, とDiagonalCornersRounded。 DML図形は、これらの図形タイプでDocumentBuilder.InsertShape
メソッドを使用して作成されます。 これらの型を使用してVML図形を作成することはできません。 “Shape"クラスのパブリックコンストラクタを使用して図形を作成しようとすると、“NotSupportedException"例外が発生します。 次のコード例は、これらの種類の図形をドキュメントに挿入する方法を示しています。
// 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.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX); | |
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL); | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.save(dataDir, so); |
数学XMLを持つ図形を図形としてDOMにインポートします
LoadOptions.ConvertShapeToOfficeMath
プロパティを使用して、EquationXML を含む図形を Office Math オブジェクトに変換できます。このプロパティの既定値は、MS Word の動作に対応しています。つまり、数式 XML を含む図形は Office Math オブジェクトに変換されません。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
LoadOptions lo = new LoadOptions(); | |
lo.setConvertShapeToOfficeMath(true); | |
// Specify load option to use previous default behaviour i.e. convert math | |
// shapes to office math ojects on loading stage. | |
Document doc = new Document(dataDir + "OfficeMath.docx", lo); | |
// Save the document into DOCX | |
doc.save(dataDir + "ConvertShapeToOfficeMath_out.docx", SaveFormat.DOCX); |