Java (482)
Captions play multiple important roles in a document. They not only provide explanations for images or tables but also help in organizing the document structure, referencing specific content, and ensuring consistency and standardization. They serve as guides, summaries, and emphasis within the document, enhancing readability and assisting readers in better understanding and utilizing the information presented in the document. This article will demonstrate how to use Spire.Doc for Java to add or remove captions in Word documents within a Java project.
- Add Image Captions to a Word document in Java
- Add Table Captions to a Word document in Java
- Remove Captions from a Word document in Java
Install Spire.Doc for Java
First, 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.4.0</version>
</dependency>
</dependencies>
Add Image Captions to a Word document in Java
By using the DocPicture.addCaption(String name, CaptionNumberingFormat format, CaptionPosition captionPosition) method, you can easily add descriptive captions to images within a Word document. The following are the detailed steps:
- Create an object of the Document class.
- Use the Document.addSection() method to add a section.
- Add a paragraph using Section.addParagraph() method.
- Use the Paragraph.appendPicture(String filePath) method to add a DocPicture image object to the paragraph.
- Add a caption using the DocPicture.addCaption(String name, CaptionNumberingFormat format, CaptionPosition captionPosition) method, numbering the captions in CaptionNumberingFormat.Number format.
- Update all fields using the Document.isUpdateFields(true) method.
- Save the resulting document using the Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class addPictureCaption {
public static void main(String[] args) {
// Create a Word document object
Document document = new Document();
// Add a section to the document
Section section = document.addSection();
// Add a new paragraph and insert an image into it
Paragraph pictureParagraphCaption = section.addParagraph();
pictureParagraphCaption.getFormat().setAfterSpacing(10);
DocPicture pic1 = pictureParagraphCaption.appendPicture("Data\\1.png");
pic1.setHeight(100);
pic1.setWidth(100);
// Add a caption to the image
CaptionNumberingFormat format = CaptionNumberingFormat.Number;
pic1.addCaption("Image", format, CaptionPosition.Below_Item);
// Add another paragraph and insert another image into it
pictureParagraphCaption = section.addParagraph();
DocPicture pic2 = pictureParagraphCaption.appendPicture("Data\\2.png");
pic2.setHeight(100);
pic2.setWidth(100);
// Add a caption to the second image
pic2.addCaption("Image", format, CaptionPosition.Below_Item);
// Update all fields in the document
document.isUpdateFields(true);
// Save the document as a docx file
String result = "AddImageCaption.docx";
document.saveToFile(result, FileFormat.Docx_2016);
// Close and dispose the document object to release resources
document.close();
document.dispose();
}
}

Add Table Captions to a Word document in Java
Similar to adding captions to images, to add a caption to a table, you need to call the Table.addCaption(String name, CaptionNumberingFormat format, CaptionPosition captionPosition) method. The detailed steps are as follows:
- Create an object of the Document class.
- Use the Document.addSection() method to add a section.
- Create a Table object and add it to the specified section in the document.
- Use the Table.resetCells(int rowsNum, int columnsNum) method to set the number of rows and columns in the table.
- Add a caption using the Table.addCaption(String name, CaptionNumberingFormat format, CaptionPosition captionPosition) method, numbering the captions in CaptionNumberingFormat.Number format.
- Update all fields using the Document.isUpdateFields(true) method.
- Save the resulting document using the Document.saveToFile() method.
- Java
import com.spire.doc.*;
public class addTableCaption {
public static void main(String[] args) {
// Create a Word document object
Document document = new Document();
// Add a section to the document
Section section = document.addSection();
// Add a table to the section
Table tableCaption = section.addTable(true);
tableCaption.resetCells(3, 2);
// Add a caption to the table
tableCaption.addCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
// Add another table to the section
tableCaption = section.addTable(true);
tableCaption.resetCells(2, 3);
// Add a caption to the second table
tableCaption.addCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.Below_Item);
// Update all fields in the document
document.isUpdateFields(true);
// Save the document as a docx file
String result = "AddTableCaption.docx";
document.saveToFile(result, FileFormat.Docx_2016);
// Close and dispose the document object to release resources
document.close();
document.dispose();
}
}

Remove Captions from a Word document in Java
In addition to adding captions, Spire.Doc for Java also supports deleting captions from a Word document. The steps involved are as follows:
- Create an object of the Document class.
- Use the Document.loadFromFile() method to load a Word document.
- Create a custom method named DetectCaptionParagraph(Paragraph paragraph), to determine if the paragraph contains a caption.
- Iterate through all the Paragraph objects in the document using a loop and use the custom method, DetectCaptionParagraph(Paragraph paragraph), to identify the paragraphs that contain captions and delete them.
- Save the resulting document using the Document.saveToFile() method.
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class deleteCaptions {
public static void main(String[] args) {
// Create a Word document object
Document document = new Document();
// Load the sample.docx file
document.loadFromFile("Data/sample.docx");
Section section;
// Iterate through all sections
for (int i = 0; i < document.getSections().getCount(); i++) {
section = document.getSections().get(i);
// Iterate through paragraphs in reverse order
for (int j = section.getBody().getParagraphs().getCount() - 1; j >= 0; j--) {
// Check if the paragraph is a caption paragraph
if (DetectCaptionParagraph(section.getBody().getParagraphs().get(j))) {
// If it is a caption paragraph, remove it
section.getBody().getParagraphs().removeAt(j);
}
}
}
// Save the document after removing captions
String result = "RemoveCaptions.docx";
document.saveToFile(result, FileFormat.Docx_2016);
// Close and dispose the document object to release resources
document.close();
document.dispose();
}
// Method to detect if a paragraph is a caption paragraph
static Boolean DetectCaptionParagraph(Paragraph paragraph) {
Boolean tag = false;
Field field;
// Iterate through child objects of the paragraph
for (int i = 0; i < paragraph.getChildObjects().getCount(); i++) {
if (paragraph.getChildObjects().get(i).getDocumentObjectType().equals(DocumentObjectType.Field)) {
// Check if the child object is of type Field
field = (Field) paragraph.getChildObjects().get(i);
if (field.getType().equals(FieldType.Field_Sequence)) {
// Check if the Field type is FieldSequence, indicating a caption field
return true;
}
}
}
return tag;
}
}

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.
OCR (Optical Character Recognition) technology is the primary method to extract text from images. Spire.OCR for Java provides developers with a quick and efficient solution to scan and extract text from images in Java projects. This article will guide you on how to use Spire.OCR for Java to recognize and extract text from images in Java projects.
Obtaining Spire.OCR for Java
To scan and recognize text in images using Spire.OCR for Java, you need to first import the Spire.OCR.jar file along with other relevant dependencies into your Java project.
You can download Spire.OCR for Java from our website. If you are using Maven, you can add the following code to your project's pom.xml file to import the JAR file into your application.
<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.ocr</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
Please download the other dependencies based on your operating system:
Install Dependencies
Step 1: Create a Java project in IntelliJ IDEA.

Step 2: Go to File > Project Structure > Modules > Dependencies in the menu and add Spire.OCR.jar as a project dependency.

Step 3: Download and extract the other dependency files. Copy all the files from the extracted "dependencies" folder to your project directory.

Scanning and Recognizing Text from a Local Image
- Java
import com.spire.ocr.OcrScanner;
import java.io.*;
public class ScanLocalImage {
public static void main(String[] args) throws Exception {
// Specify the path to the dependency files
String dependencies = "dependencies/";
// Specify the path to the image file to be scanned
String imageFile = "data/Sample.png";
// Specify the path to the output file
String outputFile = "ScanLocalImage_out.txt";
// Create an OcrScanner object
OcrScanner scanner = new OcrScanner();
// Set the dependency file path for the OcrScanner object
scanner.setDependencies(dependencies);
// Use the OcrScanner object to scan the specified image file
scanner.scan(imageFile);
// Get the scanned text content
String scannedText = scanner.getText().toString();
// Create an output file object
File output = new File(outputFile);
// If the output file already exists, delete it
if (output.exists()) {
output.delete();
}
// Create a BufferedWriter object to write content to the output file
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
// Write the scanned text content to the output file
writer.write(scannedText);
// Close the BufferedWriter object to release resources
writer.close();
}
}
Specify the Language File to Scan and Recognize Text from an Image
- Java
import com.spire.ocr.OcrScanner;
import java.io.*;
public class ScanImageWithLanguageSelection {
public static void main(String[] args) throws Exception {
// Specify the path to the dependency files
String dependencies = "dependencies/";
// Specify the path to the language file
String languageFile = "data/japandata";
// Specify the path to the image file to be scanned
String imageFile = "data/JapaneseSample.png";
// Specify the path to the output file
String outputFile = "ScanImageWithLanguageSelection_out.txt";
// Create an OcrScanner object
OcrScanner scanner = new OcrScanner();
// Set the dependency file path for the OcrScanner object
scanner.setDependencies(dependencies);
// Load the specified language file
scanner.loadLanguageFile(languageFile);
// Use the OcrScanner object to scan the specified image file
scanner.scan(imageFile);
// Get the scanned text content
String scannedText = scanner.getText().toString();
// Create an output file object
File output = new File(outputFile);
// If the output file already exists, delete it
if (output.exists()) {
output.delete();
}
// Create a BufferedWriter object to write content to the output file
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
// Write the scanned text content to the output file
writer.write(scannedText);
// Close the BufferedWriter object to release resources
writer.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.
Comparison of PDF documents is essential for effective document management. By comparing PDF documents, users can easily identify differences in document content to have a more comprehensive understanding of them, which will greatly facilitate the user to modify and integrate the document content. This article will introduce how to use Spire.PDF for Java to compare PDF documents and find the differences.
Examples of the two PDF documents that will be used for comparison:

Install Spire.PDF for Java
First of all, you need to add the Spire.Pdf.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 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.pdf</artifactId>
<version>12.3.9</version>
</dependency>
</dependencies>
Compare Two PDF Documents
Spire.PDF for Java provides the PdfComparer class for users to create an object with two PDF documents for comparing. After creating the PdfComparer object, users can use PdfComparer.compare(String fileName) method to compare the two documents and save the result as a new PDF file.
The resulting PDF document displays the two original documents on the left and the right, with the deleted items in red and the added items in yellow.
The detailed steps for comparing two PDF documents are as follows:
- Create two objects of PdfDocument class and load two PDF documents using PdfDocument.loadFromFile() method.
- Create an object of PdfComparer class with the two documents.
- Compare the two documents and save the result as a new PDF document using PdfComparer.compare() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.comparison.PdfComparer;
public class ComparePDFPageRange {
public static void main(String[] args) {
//Create an object of PdfDocument class and load a PDF document
PdfDocument pdf1 = new PdfDocument();
pdf1.loadFromFile("Sample1.pdf");
//Create another object of PdfDocument class and load another PDF document
PdfDocument pdf2 = new PdfDocument();
pdf2.loadFromFile("Sample2.pdf");
//Create an object of PdfComparer class
PdfComparer comparer = new PdfComparer(pdf1,pdf2);
//Compare the two PDF documents and save the compare results to a new document
comparer.compare("ComparisonResult.pdf");
}
}

Compare a Specified Page Range of Two PDF Documents
Before comparing, users can use the PdfComparer.getOptions().setPageRanges() method to limit the page range to be compared. The detailed steps are as follows:
- Create two objects of PdfDocument class and load two PDF documents using PdfDocument.loadFromFile() method.
- Create an object of PdfComparer class with the two documents.
- Set the page range to be compared using PdfComparer.getOptions().setPageRanges() method.
- Compare the two documents and save the result as a new PDF document using PdfComparer.compare() method.
- Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.comparison.PdfComparer;
public class ComparePDFPageRange {
public static void main(String[] args) {
//Create an object of PdfDocument class and load a PDF document
PdfDocument pdf1 = new PdfDocument();
pdf1.loadFromFile("G:/Documents/Sample6.pdf");
//Create another object of PdfDocument class and load another PDF document
PdfDocument pdf2 = new PdfDocument();
pdf2.loadFromFile("G:/Documents/Sample7.pdf");
//Create an object of PdfComparer class
PdfComparer comparer = new PdfComparer(pdf1,pdf2);
//Set the page range to be compared
comparer.getOptions().setPageRanges(1, 1, 1, 1);
//Compare the two PDF documents and save the compare results to a new document
comparer.compare("ComparisonResult.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.