チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
Word ドキュメントの透かしは、半透明のテキストまたは画像の背景です。透かしは、そのドキュメントが機密文書であること、または単なる草稿であることを読者に思い出させるためにテキストの透かしを使用したり、会社の写真で著作権を宣言したりするなど、ドキュメントに関する重要な何かを強調するために使用されます。時には透かしがドキュメントの読みやすさに影響することもあり、透かしを除去したい場合、Spire.Doc for Java が大いに役立ちます。この記事では、Spire.Doc for Java を使用して Word ドキュメントから透かしを削除する詳細な手順を示します。
まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>10.9.8</version>
</dependency>
</dependencies>
Spire.Doc for Java が提供する Document.setWatermark() メソッドを使用して透かしを null に設定することで、Word ドキュメントから透かしを削除することができます。 テキスト透かしも画像透かしもこの方法で削除できます。詳しい手順は以下の通りです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class removeWordWatermark {
public static void main(String[] args) {
//Documentのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//透かしをnullに設定する
doc.setWatermark(null);
//ドキュメントを保存する
doc.saveToFile("透かしの削除.docx", FileFormat.Auto);
doc.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
繰り返し透かしは、複数行の透かしとも呼ばれ、Word ドキュメントのページ上に一定の間隔で複数回表示される透かしの一種です。通常の透かしに比べ、繰り返し透かしは削除や隠蔽が難しいため、不正コピーや不正配布の防止に効果的です。この記事では、Spire.Doc for Java を使用して、プログラムによって Word ドキュメントに繰り返しテキストと画像の透かしを挿入する方法を紹介します。
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>11.6.0</version>
</dependency>
</dependencies>
Word ドキュメントのヘッダーに指定した間隔で繰り返しワードアートを追加することで、Word ドキュメントに繰り返しテキスト透かしを挿入することができます。詳しい手順は次のとおりです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class insertRepeatingTextWatermark {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//ShapeObjectクラスのオブジェクトを作成し、アートフォントのテキストを設定する
ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
shape.getWordArt().setText("オリジナル");
//テキスト透かしの回転角度と、垂直方向と水平方向の繰り返し回数を設定する
double rotation = 315;
int ver = 5;
int hor = 3;
//ワードアートの書式を設定する
shape.setWidth(60);
shape.setHeight(20);
shape.setVerticalPosition(30);
shape.setHorizontalPosition(20);
shape.setRotation(rotation);
shape.getWordArt().setFontFamily("HarmonyOS Sans SC");
shape.setFillColor(Color.BLUE);
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(Color.CYAN);
shape.setStrokeWeight(1);
//ドキュメントのセクションをループする
for (Section section : (Iterable extends Section>) doc.getSections()
) {
//セクションのヘッダーを取得する
HeaderFooter header = section.getHeadersFooters().getHeader();
//ヘッダーに段落を追加する
Paragraph paragraph = header.addParagraph();
for (int i = 0; i < ver; i++) {
for (int j = 0; j < hor; j++) {
//ヘッダーに段落を追加する
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition((float) (section.getPageSetup().getPageSize().getHeight()/ver * i + Math.sin(rotation) * shape.getWidth()/2));
shape.setHorizontalPosition((float) ((section.getPageSetup().getPageSize().getWidth()/hor - shape.getWidth()/2) * j));
paragraph.getChildObjects().add(shape);
}
}
}
//ドキュメントを保存する
doc.saveToFile("繰り返しテキスト透かし.docx", FileFormat.Auto);
doc.dispose();
}
}
同様に、ヘッダーに一定の間隔で繰り返し画像を追加することで、Wordドキュメントに繰り返し画像透かしを挿入することができます。詳しい手順は次のとおりです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
public class insertRepeatingPictureWatermark {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document doc = new Document();
//Wordドキュメントを読み込む
doc.loadFromFile("サンプル.docx");
//画像を読み込む
DocPicture pic = new DocPicture(doc);
pic.loadImage("透かし.png");
//画像のテキストの折り返しを背面に設定する
pic.setTextWrappingStyle(TextWrappingStyle.Behind);
//垂直方向と水平方向の繰り返し回数を設定する
int ver = 4;
int hor = 3;
//ドキュメントのセクションをループする
for (Section section : (Iterable extends Section>) doc.getSections()
) {
//セクションのヘッダーを取得する
HeaderFooter header = section.getHeadersFooters().getHeader();
//ヘッダーに段落を追加する
Paragraph paragraph = header.addParagraph();
for (int i = 0; i < ver; i++) {
for (int j = 0; j < hor; j++) {
//ヘッダーに画像を追加する
pic = (DocPicture) pic.deepClone();
pic.setVerticalPosition((float) ((section.getPageSetup().getPageSize().getHeight()/ver) * i));
pic.setHorizontalPosition((float) (section.getPageSetup().getPageSize().getWidth()/hor - pic.getWidth()/2) * j);
paragraph.getChildObjects().add(pic);
}
}
}
//ドキュメントを保存する
doc.saveToFile("繰り返し画像透かし.docx", FileFormat.Auto);
doc.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
段落と文字の背景色は、Word ドキュメントにおけるドキュメント設計の重要な要素であります。適切な段落と文字の背景色は、特定の段落と文字を強調したり、文字のコントラストを高めて読みやすくしたり、レイアウトの隙間を埋めて組版を支援したりする役割を果たします。そのため、Word ドキュメントを作成する際には、段落やテキストの背景色を合理的に使用することが非常に重要です。この記事では、Spire.Doc for Java を使用して、Java で段落とテキストの背景色を設定する方法を説明します。
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>11.4.2</version>
</dependency>
</dependencies>
段落の背景色を設定するには、段落を取得し、Paragraph.getFormat().setBackColor() メソッドを使用して背景色を変更する必要があります。詳しい手順は以下の通りです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import java.awt.*;
public class SetParagraphBackgroundColor {
public static void main(String[] args) {
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/サンプル.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//2段落目を取得する
Paragraph paragraph = section.getParagraphs().get(1);
//この段落の背景色を淡灰色に設定する
paragraph.getFormat().setBackColor(Color.LIGHT_GRAY);
//ドキュメントを保存する
document.saveToFile("段落の背景色.docx", FileFormat.Docx_2013);
document.dispose();
}
}
Spire.Doc for Java は、Word ドキュメント内の特定のテキストの出現箇所をすべて検索する Document.findAllString() メソッドと、特定のテキストの背景色を設定する TextRange.getCharacterFormat ().setTextBackgroundColor() メソッドを提供します。既存のテキストの背景色を設定する詳しい手順は以下の通りです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetTextBackgroundColor {
public static void main(String[] args) {
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/サンプル.docx");
//背景色を設定するテキストを探す
TextSelection[] textSelections = document.findAllString("時間を超越した宇宙", false, true);
//出現するすべてのテキストに背景色を設定する
for (TextSelection selection : textSelections){
//出現箇所をテキスト範囲として取得する
TextRange textRange = selection.getAsOneRange();
//出現するテキストの背景色を設定する
textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
}
//テキストの最初の出現箇所の背景色を設定する
//TextRange textRange = textSelections[0].getAsOneRange();
//textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
//ドキュメントを保存する
document.saveToFile("文字の背景色.docx", FileFormat.Docx_2013);
document.dispose();
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
ウォーターマークは、ドキュメントの背景にある淡い画像またはテキストです。文書を他の人に送信する必要がある場合は、文書にウォータマークを追加することで、他人が文書を不正に使用しないようにしたり、文書が機密、下書き、サンプルなどであることを読者に注意したりすることができます。この記事では、Spire.Doc for Java を使用して Word ドキュメントにテキストと画像ウォータマークを追加する方法を説明します。
まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>10.9.8</version>
</dependency>
</dependencies>
以下に詳細な操作方法を示します。
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class WordTextWatermark {
public static void main(String[] args) {
//Documentインスタンスを作成する
Document document = new Document();
//サンプルドキュメントをロードする
document.loadFromFile("Sample.docx");
//最初のセクションを取得する
Section section = document.getSections().get(0);
//TextWatermarkインスタンスを作成する
TextWatermark txtWatermark = new TextWatermark();
//テキストウォータマークの書式を設定する
txtWatermark.setText("機密");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
//ドキュメントにテキストウォーターマークを追加する
section.getDocument().setWatermark(txtWatermark);
//結果文書を保存する
document.saveToFile("out/result.docx", FileFormat.Docx);
}
}
以下に詳細な操作方法を示します。
import com.spire.doc.*;
public class WordImageWatermark {
public static void main(String[] args) throws Exception{
//Documentインスタンスを作成する
Document document = new Document();
//サンプルドキュメントをロードする
document.loadFromFile("Sample.docx");
//PictureWatermarkインスタンスを作成する
PictureWatermark picture = new PictureWatermark();
//画像ウォータマークの書式を設定する
picture.setPicture("logo.png");
picture.setScaling(100);
picture.isWashout(false);
//サンプルドキュメントにイメージウォーターマークを追加する
document.setWatermark(picture);
//結果文書を保存する
document.saveToFile("out/result2.docx",FileFormat.Docx );
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメントの背景は、デフォルトで白紙になっています。パンフレット、招待状、配布資料、マーケティング資料などの文書では、白紙の背景はあまりにも退屈でしょう。見栄えの良い背景は、読み手を惹きつける大きな魅力となります。したがって、背景として色を追加したり、画像を挿入したりすることで、文書をより魅力的にすることができます。この記事では、Spire.Doc for Java を使用して Word ドキュメントに背景色や背景画像を設定する方法を紹介します。
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>10.7.10</version>
</dependency>
</dependencies>
Word ドキュメントに背景色を追加するのはとても簡単です。背景の種類を「Color」に設定し、背景となる色を選択するだけです。詳しい手順は、以下の通りです。
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//Document クラスのオブジェクトを作成する
Document document= new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/実例.docx");
//背景の種類を「Color」に設定する
document.getBackground().setType(BackgroundType.Color);
//背景色を設定する
document.getBackground().setColor(Color.orange);
//ドキュメントを保存する
document.saveToFile("背景色の追加.docx", FileFormat.Docx);
}
}
グラデーションの背景を追加するには、より多くの手順が必要です。背景の種類を「グラデーション」に設定し、2色を選択し、バリエーションとグラデーションの種類を設定する必要があります。詳しい手順は、以下の通りです。
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import com.spire.doc.documents.GradientShadingStyle;
import com.spire.doc.documents.GradientShadingVariant;
import java.awt.*;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//Document クラスのオブジェクトを作成する
Document document= new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/実例.docx");
//背景の種類を「Gradient」に設定する
Background background = document.getBackground();
background.setType(BackgroundType.Gradient);
//2つの色を選択する
background.getGradient().setColor1(Color.white);
background.getGradient().setColor2(Color.orange);
//バリエーションを選択する
background.getGradient().setShadingVariant(GradientShadingVariant.Shading_Down);
//シェーディングの種類を選択する
background.getGradient().setShadingStyle(GradientShadingStyle.Horizontal);
//ドキュメントを保存する
document.saveToFile("グラデーション背景の追加.docx", FileFormat.Docx);
}
}
Word ドキュメントに背景画像を挿入するには、背景の種類を「Picture」に設定し、背景として画像を挿入する必要があります。詳しい手順は、以下の通りです。
import com.spire.doc.*;
import com.spire.doc.documents.BackgroundType;
import java.awt.*;
import java.io.*;
public class addBackgroundColor {
public static void main(String[] args) throws IOException {
//Document クラスのオブジェクトを作成する
Document document= new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/実例.docx");
//背景の種類を「Picture」に設定する
document.getBackground().setType(BackgroundType.Picture);
//背景画像を挿入する
document.getBackground().setPicture("C:/背景.jpg");
//ドキュメントを保存する
document.saveToFile("背景画像の挿入.docx", FileFormat.Docx);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。