Knowledgebase (2311)
Children categories
By inserting a page break into your Word document, you can end a page at the place you want and begin a new page at once without hitting the enter key repeatedly. In this article, we will demonstrate how to insert page breaks into a Word document in Java using Spire.Doc for Java library.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<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>14.1.3</version>
</dependency>
</dependencies>
Insert Page Break after a Specific Paragraph
The following are the steps to insert page break after a specific paragraph:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Get the desired section using Document.getSections().get(sectionIndex) method.
- Get the desired paragraph using Section.getParagraphs().get(paragraphIndex) method.
- Add a page break to the paragraph using Paragraph.appendBreak(BreakType.Page_Break) method.
- Save the result document using Document.saveToFile() method.
- 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){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");
//Get the first section
Section section = document.getSections().get(0);
//Get the 2nd paragraph in the section
Paragraph paragraph = section.getParagraphs().get(1);
//Append a page break to the paragraph
paragraph.appendBreak(BreakType.Page_Break);
//Save the result document
document.saveToFile("InsertPageBreak.docx", FileFormat.Docx_2013);
}
}

Insert Page Break after a Specific Text
The following are the steps to insert a page break after a specific text:
- Create a Document instance.
- Load a Word document using Document.loadFromFile() method.
- Find a specific text using Document.findString() method.
- Access the text range of the searched text using TextSelection.getAsOneRange() method.
- Get the paragraph where the text range is located using ParagraphBase.getOwnerParagraph() method.
- Get the position index of the text range in the paragraph using Paragraph.getChildObjects().indexOf() method.
- Initialize an instance of Break class to create a page break.
- Insert the page break after the searched text using Paragraph.getChildObjects().insert() method.
- Save the result document using Document.saveToFile() method.
- 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){
//Create a Document instance
Document document = new Document();
//Load a Word document
document.loadFromFile("Sample.docx");
//Search a specific text
TextSelection selection = document.findString("celebration", true, true);
//Get the text range of the seached text
TextRange range = selection.getAsOneRange();
//Get the paragraph where the text range is located
Paragraph paragraph = range.getOwnerParagraph();
//Get the position index of the text range in the paragraph
int index = paragraph.getChildObjects().indexOf(range);
//Create a page break
Break pageBreak = new Break(document, BreakType.Page_Break);
//Insert the page break after the searched text
paragraph.getChildObjects().insert(index + 1, pageBreak);
//Save the result document
document.saveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
After you enable the Track Changes feature in a Word document, it records all the edits in the document, such as insertions, deletions, replacements, and format changes. Track Changes is a great feature allowing you to see what changes have been made to a document. This tutorial shows how to get all revisions from a Word document by using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<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>14.1.3</version>
</dependency>
</dependencies>
Get All Revisions from Word
The detailed steps are as follows.
- Create a Document instance and load a sample Word document using Document.loadFromFile() method.
- Create a StringBuilder object and then using StringBuilder.append() method to log data.
- Traverse all the sections and every element under body in the section.
- Determine if the paragraph is an insertion revision or not using Paragraph.isInsertRevision() method. If yes, use Paragraph.getInsertRevision() method to get the insertion revision. Then get the revision type and author using EditRevision.getType() method and EditRevision.getAuthor() method.
- Determine if the paragraph is a delete revision or not using Paragraph.inDeleteRevision() method. If yes, use Paragraph.getDeleteRevision() method to get the delete revision. Then get the revision type and author using EditRevision.getType() method and EditRevision.getAuthor() method.
- Traverse all the elements in the paragraphs to get the text ranges' revisions.
- Write the content of StringBuilder to a txt document using FileWriter.write() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.formatting.revisions.*;
import java.io.FileWriter;
public class getRevisions {
public static void main(String[] args) throws Exception {
//Load the sample Word document
Document document = new Document();
document.loadFromFile("test file.docx");
//Create a StringBuilder object to get the insertions
StringBuilder insertRevision = new StringBuilder();
insertRevision.append("Insert revisions:"+"\n");
int index_insertRevision = 0;
//Create a StringBuilder object to get the deletions
StringBuilder deleteRevision = new StringBuilder();
deleteRevision.append("Delete revisions:"+"\n");
int index_deleteRevision = 0;
//Traverse all the sections
for (Section sec : (Iterable<Section>) document.getSections())
{
//Iterate through the element under body in the section
for(DocumentObject docItem : (Iterable<DocumentObject>)sec.getBody().getChildObjects())
{
if (docItem instanceof Paragraph)
{
Paragraph para = (Paragraph)docItem;
//Determine if the paragraph is an insertion revision
if (para.isInsertRevision())
{
index_insertRevision++;
insertRevision.append("Index: " + index_insertRevision+"\n");
//Get insertion revision
EditRevision insRevison = para.getInsertRevision();
//Get insertion revision type
EditRevisionType insType = insRevison.getType();
insertRevision.append("Type: " + insType+"\n");
//Get insertion revision author
String insAuthor = insRevison.getAuthor();
insertRevision.append("Author: " + insAuthor + "\n");
}
//Determine if the paragraph is a delete revision
else if (para.isDeleteRevision())
{
index_deleteRevision++;
deleteRevision.append("Index: " + index_deleteRevision +"\n");
EditRevision delRevison = para.getDeleteRevision();
EditRevisionType delType = delRevison.getType();
deleteRevision.append("Type: " + delType+ "\n");
String delAuthor = delRevison.getAuthor();
deleteRevision.append("Author: " + delAuthor + "\n");
}
//Iterate through the element in the paragraph
for(DocumentObject obj : (Iterable<DocumentObject>)para.getChildObjects())
{
if (obj instanceof TextRange)
{
TextRange textRange = (TextRange)obj;
//Determine if the textrange is an insertion revision
if (textRange.isInsertRevision())
{
index_insertRevision++;
insertRevision.append("Index: " + index_insertRevision +"\n");
EditRevision insRevison = textRange.getInsertRevision();
EditRevisionType insType = insRevison.getType();
insertRevision.append("Type: " + insType + "\n");
String insAuthor = insRevison.getAuthor();
insertRevision.append("Author: " + insAuthor + "\n");
}
else if (textRange.isDeleteRevision())
{
index_deleteRevision++;
deleteRevision.append("Index: " + index_deleteRevision +"\n");
//Determine if the textrange is a delete revision
EditRevision delRevison = textRange.getDeleteRevision();
EditRevisionType delType = delRevison.getType();
deleteRevision.append("Type: " + delType+"\n");
String delAuthor = delRevison.getAuthor();
deleteRevision.append("Author: " + delAuthor+"\n");
}
}
}
}
}
}
//Save to a .txt file
FileWriter writer1 = new FileWriter("insertRevisions.txt");
writer1.write(insertRevision.toString());
writer1.flush();
writer1.close();
//Save to a .txt file
FileWriter writer2 = new FileWriter("deleteRevisions.txt");
writer2.write(deleteRevision.toString());
writer2.flush();
writer2.close();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
RTF (Rich Text Format) is a proprietary file format developed by Microsoft for cross-platform document interchange. RTF files have good compatibility and they can be opened by most word processors on any operating system such as Unix, Macintosh, and Windows. In some cases, you may need to convert RTF to other file formats to meet different requirements. In this article, you will learn how to convert RTF to PDF programmatically using Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<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>14.1.3</version>
</dependency>
</dependencies>
Convert RTF to PDF in Java
The detailed steps are as follows.
- Create a Document instance.
- Load a sample RTF document using Document.loadFromFile() method.
- Save the RTF to PDF using Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class RTFToPDF {
public static void main(String[] args) {
//Create Document instance.
Document document = new Document();
//Load a sample RTF document
document.loadFromFile("sample.rtf", FileFormat.Rtf);
//Save the document to PDF
document.saveToFile("rtfToPdf.pdf", FileFormat.PDF);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.