チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»docjavapagesetup

Displaying items by tag: docjavapagesetup

Word ドキュメントに改ページを挿入することで、Enter キーを連打することなく、好きな場所でページを終了し、新しいページを一度に開始することができます。今回は、Spire.Doc for Java ライブラリを使用して、Java で Word ドキュメントに改ページを挿入する方法を説明します。

Spire.Doc for 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>10.12.2</version>
    </dependency>
</dependencies>

特定の段落後に改ページを挿入する

特定の段落後に改ページを挿入する手順は以下の通りです。

  • Document のインスタンスを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getSections().get(sectionIndex) メソッドを使用して、目的のセクションを取得します。
  • Section.getParagraphs().get(paragraphIndex) メソッドを使用して、目的の段落を取得します。
  • Paragraph.appendBreak(BreakType.Page_Break) メソッドを使用して、段落に改ページを追加します。
  • Document.saveToFile() メソッドを使用して、結果ドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.FileFormat;

public class InsertPageBreakAfterParagraph {
    public static void main(String[] args){

        //Documentのインスタンスを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/自分を見失う.docx");

        //最初のセクションを取得する
        Section section = document.getSections().get(0);

        //セクションの3段落目を取得する
        Paragraph paragraph = section.getParagraphs().get(2);

        //段落に改ページを付加する
        paragraph.appendBreak(BreakType.Page_Break);

        //結果ドキュメントを保存する
        document.saveToFile("改ページの挿入.docx", FileFormat.Docx_2013);
    }
}

Java:Word ドキュメントに改ページを挿入する方法

特定のテキストの後に改ページする

特定のテキストの後に改ページを挿入する手順は次のとおりです。

  • Document のインスタンスを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.findString() メソッドを使用して、特定のテキストを検索します。
  • TextSelection.getAsOneRange() メソッドを使用して、検索されたテキストのテキスト範囲にアクセスします。
  • ParagraphBase.getOwnerParagraph() メソッドを使用して、テキスト範囲がある段落を取得します。
  • Paragraph.getChildObjects().indexOf() メソッドを使用して、段落内のテキスト範囲の位置インデックスを取得します。
  • Break のインスタンスを作成し、改ページを作成します。
  • Paragraph.getChildObjects().insert() メソッドを使用して、検索されたテキストの後に改ページを挿入します。
  • Document.saveToFile() メソッドを使用して、結果ドキュメントを保存します。
  • Java
import com.spire.doc.Break;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;

public class InsertPageBreakAfterText {
    public static void main(String[] args){

        //Documentのインスタンスを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("C:/自分を見失う.docx");

        //特定のテキストを検索する
        TextSelection selection = document.findString("精神疾患", true, true);
        //検索されたテキストのテキスト範囲を取得する
        TextRange range = selection.getAsOneRange();
        //テキスト範囲がある段落を取得する
        Paragraph paragraph = range.getOwnerParagraph();
        //段落内のテキスト範囲の位置インデックスを取得する
        int index = paragraph.getChildObjects().indexOf(range);

        //改ページを作成する
        Break pageBreak = new Break(document, BreakType.Page_Break);
        //検索されたテキストの後に改ページを挿入する
        paragraph.getChildObjects().insert(index + 1, pageBreak);

        //結果ドキュメントを保存する
        document.saveToFile("テキストの後に改ページを挿入.docx", FileFormat.Docx_2013);
    }
}

Java:Word ドキュメントに改ページを挿入する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページ設定
Tagged under

Word ドキュメントのページ番号は、ページの順序とページ数を示すために各ページに付けられます。ページ番号によって、ドキュメント作成者は文書の内容を管理しやすくなり、ユーザーはドキュメント内の特定の内容をすばやく見つけることができるため、閲覧速度と閲覧体験が向上します。この記事では、Spire.Doc for Java を使って Word ドキュメントにページ番号を追加する方法を紹介します。

Spire.Doc for 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.6.0</version>
    </dependency>
</dependencies>

Word ドキュメントにページ番号を追加する

Word ドキュメントのページ番号は、特定のタイプのフィールドを使用して表示されます。例えば、Page フィールドは現在のページのページ番号を表示し、NumPages フィールドはドキュメント内の総ページ数を表示し、SectionPages フィールドはセクション内の総ページ数を表示します。

Spire.Doc for Java には、Paragraph.appendField(String fieldName, FieldType fieldType) メソッドがあり、Page フィールド (FieldType.Field_Page)、NumPages フィールド (FieldType.Field_Num_Pages)、SectionPages フィールド (FieldType.Field_Section_Pages) など、さまざまなタイプのフィールドをWord文書に追加できます。

以下の手順では、Spire.Doc for Java を使用して、Word ドキュメントのフッターに Page フィールドと NumPages フィールドを追加し、現在のページ番号とドキュメント内の総ページ数を表示する方法を説明します。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getSections().get() メソッドを使用して最初のセクションを取得します。
  • Section.getHeadersFooters().getFooter() メソッドを使用して、最初のセクションのフッターを取得します。
  • フッターに段落を追加し、Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを使用して、段落に Page フィールドと NumPages フィールドを追加します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersWholeDocument {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("サンプル.docx");

        //ドキュメントの最初のセクションを取得する
        Section section = document.getSections().get(0);

        //最初のセクションのフッターを取得する
        HeaderFooter footer = section.getHeadersFooters().getFooter();

        //フッターにPageフィールドとNumPagesフィールドを追加し、ページ番号フォーマットを設定する
        Paragraph footerParagraph = footer.addParagraph();
        footerParagraph.appendField("ページ番号", FieldType.Field_Page);
        footerParagraph.appendText(" / ");
        footerParagraph.appendField("ページ数", FieldType.Field_Num_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        footerParagraph.getStyle().getCharacterFormat().setFontSize(16);

        //ドキュメントを保存する
        document.saveToFile("ドキュメントのページ番号.docx");
    }
}

Java:Word ドキュメントにページ番号を追加する方法

各セクションのページ番号の追加と再配置

ページ番号の再配置を行うと、前のセクションのページ番号から続けるのではなく、各セクションの特定の番号からページ番号を開始することができます。

Word ドキュメントの各セクションでページ番号付けを再配置するには、ドキュメント内のすべてのセクションをループし、各セクションに Page フィールドと SectionPages フィールドを追加する必要があります。そして、Section.getPageSetup().setRestartPageNumbering(true) メソッドを使用してページ番号付けの再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを使用して各セクションの開始ページ番号を設定します。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • ドキュメント内のセクションをループします。
  • Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを呼び出して、各セクションに Page フィールドと SectionPages フィールドを追加します。
  • Section.getPageSetup().setRestartPageNumbering(true) メソッドを呼び出して、ページ番号の再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを呼び出して、各セクションの開始ページ番号を設定します。
  • Document.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersEachSection {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("サンプル.docx");

        //ドキュメントのセクション数を取得する
        int s = document.getSections().getCount();

        //ドキュメントのセクションをループする
        for (int i = 0; i < s; i++) {

            //フッターにPageとSectionPagesフィールドを追加し、ページ番号の書式を設定する
            HeaderFooter footer = document.getSections().get(i).getHeadersFooters().getFooter();
            Paragraph footerParagraph = footer.addParagraph();
            footerParagraph.appendField("ページ番号", FieldType.Field_Page);
            footerParagraph.appendText(" / ");
            footerParagraph.appendField("ページ数", FieldType.Field_Section_Pages);
            footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            footerParagraph.getStyle().getCharacterFormat().setFontSize(16);

            //ページ番号の開始ページを再設定し、開始ページ番号を設定する
            if (i == s-1)
                break;
            else {
                document.getSections().get(i + 1).getPageSetup().setRestartPageNumbering(true);
                document.getSections().get(i + 1).getPageSetup().setPageStartingNumber(1);
            }
        }

        //ドキュメントを保存する
        document.saveToFile("各セクションのページ番号.docx");
        document.dispose();
    }
}

Java:Word ドキュメントにページ番号を追加する方法

特定のセクションにページ番号を追加する

デフォルトでは、セクションのフッターにページ番号を挿入すると、後続のセクションは自動的に前のセクションにリンクし、ページ番号を表示し続けます。特定のセクションだけにページ番号を追加したい場合は、後続のセクションと前のセクションのリンクを解除し、後続のセクションのフッターの内容を削除する必要があります。詳しい手順は以下の通りです。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getSections().get() メソッドを使用して、ドキュメントの 2 番目のセクションを取得します。
  • Section.getPageSetup().setRestartPageNumbering(true) メソッドを呼び出してページ番号の再配置を有効にし、Section.getPageSetup().setPageStartingNumber(int value) メソッドを呼び出してセクションの開始ページ番号を設定します。
  • Paragraph.appendField(String fieldName, FieldType fieldType) メソッドを呼び出して、Page フィールドと SectionPages フィールドをセクションに追加します。
  • Section.getHeadersFooters().getFooter().setLinkToPrevious(false) メソッドを使用して、2 番目のセクションから後続のセクションのリンクを解除します。
  • 後続セクションのフッターの内容を削除する。
  • Doucment.saveToFile() メソッドを使用してドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class addPageNumbersSpecificSection {
    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("サンプル.docx");

        //ドキュメントの2番目のセクションを取得する
        Section section = document.getSections().get(1);

        //2番目のセクションのフッターを取得する
        HeaderFooter footer = section.getHeadersFooters().getFooter();

        //2番目のセクションからページを再配置し、開始ページ番号を1に設定する
        section.getPageSetup().setRestartPageNumbering(true);
        section.getPageSetup().setPageStartingNumber(1);

        //フッターにPageフィールドとSectionPagesフィールドを追加し、ページ番号の書式を設定する
        Paragraph footerParagraph = footer.addParagraph();
        footerParagraph.appendField("ページ番号", FieldType.Field_Page);
        footerParagraph.appendText(" / ");
        footerParagraph.appendField("ページ数", FieldType.Field_Section_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        footerParagraph.getStyle().getCharacterFormat().setFontSize(16);

        //後続のセクションと2番目のセクションのリンクを解除し、後続のセクションのフッターの内容を削除する
        document.getSections().get(2).getHeadersFooters().getFooter().setLinkToPrevious(false);
        for (int i = 2; i < document.getSections().getCount(); i++) {
            document.getSections().get(i).getHeadersFooters().getFooter().getChildObjects().clear();
            document.getSections().get(i).getHeadersFooters().getFooter().addParagraph();
        }

        //ドキュメントを保存する
        document.saveToFile("特定のセクションのページ番号.docx");
    }
}

Java:Word ドキュメントにページ番号を追加する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページ設定
Tagged under

綴じ代(とじしろ)は、文書の既存のページ余白にスペースを加えることで、装丁時にテキストが隠れないように保証するための機能です。この機能は、重要な公式文書や書籍、試験などを装丁する場合に非常に役立ちます。この記事では、Spire.Doc for Java を使用して Word 文書のページ左側に綴じ代を設定する方法について説明します。

Spire.Doc for 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>

Word で綴じ代を設定する

文書のページ余白を調整して装丁スペースを増やすよりも、綴じ代を直接設定する方が効果的です。以下は、Word 文書で綴じ代を設定する手順です。

  • Document インスタンスを作成します。
  • Document.loadFromFile() メソッドを使用して Word 文書をロードします。
  • Document.getSection().get() メソッドを使用して特定のセクションを取得します。
  • Section.getPageSetup().setGutter() メソッドを使用して、指定されたセクションの綴じ代を設定します。
  • Document.saveToFile() メソッドを使用して結果文書を保存します。
  • Java
import com.spire.doc.*;
import java.io.IOException;

public class addGutter {
    public static void main(String[] args) throws IOException {
        //Documentインスタンスを作成する
        Document document = new Document();

        //Word 文書をロードする
        document.loadFromFile("input.docx");

        //最初のセクションを取得する
        Section section = document.getSections().get(0);

        //綴じ代を設定する
        section.getPageSetup().setGutter(100f);

        //結果文書を保存する
        document.saveToFile("addGutter.docx", FileFormat.Docx);
    }
}

Java:Word で綴じ代を設定する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページ設定
Tagged under

余白とは、Word ドキュメントにおいて、ページの主要なコンテンツとページの端との間にあるスペースのことです。 Word ドキュメントを作成する際、通常、テキストや画像などのコンテンツは余白の内側に挿入する必要があり、ヘッダーやフッターのコンテンツは余白の中に配置することができる。 Microsoft Word では、余白のデフォルト値は1インチですが、ユーザーが必要に応じて余白を設定することも可能です。 この記事では、Spire.Doc for Java を使用して Word ドキュメントにの余白を設定する方法について説明します。

Spire.Doc for 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>10.12.2</version>
    </dependency>
</dependencies>

Word ドキュメントの余白を設定する

Spire.Doc for Java に含まれる MarginsF クラスは、ページのマージン設定を表します。Section.getPageSetup().getMargins() メソッドでページのマージン設定を取得し、MarginsF クラス以下のメソッドで上下左右のマージンを設定することが可能です。 詳しい手順は以下の通りです。

  • Document のオブジェクトを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getSections().get() メソッドを使用して、ドキュメントの最初のセクションを取得します。
  • Section.getPageSetup().getMargins() メソッドを使用して、最初のセクションの余白を取得します。
  • MarginsF.setTop()MarginsF.setBottom()MarginsF.setLeft()MarginsF.setRight() メソッドを使用して、上下左右の余白を設定します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.MarginsF;

public class setPageMargins {
    public static void main(String []args){

        //Documentのオブジェクトを作成する
        Document document = new Document();

        //Wordドキュメントを読み込む
        document.loadFromFile("生まれてこのように.docx");

        //ドキュメントの最初のセクションを取得する
        Section section = document.getSections().get(0);

        //最初のセクションの余白を取得する
        MarginsF pageMargin = section.getPageSetup().getMargins();

        //最初のセクションの上下左右の余白を設定する
        pageMargin.setTop(17.9f);
        pageMargin.setBottom(17.9f);
        pageMargin.setLeft(17.9f);
        pageMargin.setRight(17.9f);

        //ドキュメントを保存する
        document.saveToFile("余白の設定.docx", FileFormat.Docx_2013);
    }
}

Java:Word ドキュメントの余白を設定する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページ設定
Tagged under

通常、Word ドキュメントの既定のページサイズは「信紙」(8.5×11インチ)、既定のページ方向は「縦」になっています。多くの場合、この一般的なページ設定でほとんどのユーザーのニーズを満たすことができますが、時には、申込書、証明書、パンフレットなど、別の文書を設計するためにページサイズや向きを調整する必要があることもあります。この記事では、Spire.Doc for Java を使用して、Word ドキュメントのページサイズとページの向きをプログラムによって変更する方法を説明します。

Spire.Doc for Java をインストールします

まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <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 ドキュメントのページサイズとページの向きを変更する

詳細な手順は以下の通りです。

  • Document クラスのインスタンスを作成します。
  • Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。
  • Document.getSections().get() メソッドを使用して、最初のセクションを取得します。
  • Section.getPageSetup().setPageSize() メソッドを使用して、既定のページサイズを変更します。
  • Section.getPageSetup().setOrientation() メソッドを使用して、既定のページの向きを変更します。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;

public class ChangePageSizeAndOrientation {
    public static void main(String[] args) {
        //Documentクラスのインスタンスを作成する
        Document doc= new Document();

        //Wordドキュメントを読み込む
        doc.loadFromFile("実例.docx");

        //最初のセクションを取得する
        Section section = doc.getSections().get(0);

        //ページサイズをA3に変更する
        section.getPageSetup().setPageSize(PageSize.A3);

        //ページの向きを横向きに変更する
        section.getPageSetup().setOrientation(PageOrientation.Landscape);

        //ドキュメントを保存する
        doc.saveToFile("結果.docx",FileFormat.Docx_2013);
    }
}

Java:Word ドキュメントのページサイズとページの向きを変更する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Published in ページ設定
Tagged under