Java (486)
Transferring content between Microsoft Word documents is a frequent task for many users. Whether you need to consolidate information spread across multiple files or quickly reuse existing text and other elements, the ability to effectively copy and paste between documents can save you time and effort.
In this article, you will learn how to copy content from one Word document to another using Java and Spire.Doc for Java.
- Copy Specified Paragraphs from One Word Document to Another
- Copy a Section from One Word Document to Another
- Copy the Entire Document and Append it to Another
- Create a Copy of a Word Document
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.10.6</version>
</dependency>
</dependencies>
Copy Specified Paragraphs from One Word Document to Another in Java
Spire.Doc for Java provides a flexible way to copy content between Microsoft Word documents. This is achieved by cloning individual paragraphs and then adding those cloned paragraphs to a different document.
To copy specific paragraphs from one Word document to another, you can follow these steps:
- Load the source document into a Document object.
- Load the target document into a separate Document object.
- Identify the paragraphs you want to copy from the source document.
- Create copies of those selected paragraphs using Paragraph.deepClone() method
- Add the cloned paragraphs to the target document using ParagraphCollection.add() method.
- Save the updated target document to a new Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
public class CopyParagraphs {
public static void main(String[] args) {
// Create a Document object
Document sourceDoc = new Document();
// Load the source file
sourceDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
// Get a specific section
Section section = sourceDoc.getSections().get(0);
// Get the specified paragraphs from the source file
Paragraph p1 = section.getParagraphs().get(2);
Paragraph p2 = section.getParagraphs().get(3);
// Create another Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Get the last section
Section lastSection = targetDoc.getLastSection();
// Add the paragraphs from the source file to the target file
lastSection.getParagraphs().add((Paragraph)p1.deepClone());
lastSection.getParagraphs().add((Paragraph)p2.deepClone());
// Save the target file to a different Word file
targetDoc.saveToFile("CopyParagraphs.docx", FileFormat.Docx_2019);
// Dispose resources
sourceDoc.dispose();
targetDoc.dispose();
}
}

Copy a Section from One Word Document to Another in Java
When copying content between Microsoft Word documents, it's important to consider that a section can contain not only paragraphs, but also other elements like tables. To successfully transfer an entire section from one document to another, you need to iterate through all the child objects within the section and add them individually to a specific section in the target document.
The steps to copy a section between different Word documents are as follows:
- Create Document objects to load the source file and the target file, respectively.
- Get the specified section from the source document.
- Iterate through the child objects within the section.
- Clone a specific child object using DocumentObject.deepClone() method.
- Add the cloned child objects to a designated section in the target document using DocumentObjectCollection.add() method.
- Save the updated target document to a new file.
- Java
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
public class CopySection {
public static void main(String[] args) {
// Create a Document object
Document sourceDoc = new Document();
// Load the source file
sourceDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");
// Get the specified section from the source file
Section section = sourceDoc.getSections().get(0);
// Create another Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Get the last section of the target file
Section lastSection = targetDoc.getLastSection();
// Iterate through the child objects in the selected section
for (int i = 0; i < section.getBody().getChildObjects().getCount(); i++) {
// Get a specific child object
DocumentObject childObject = section.getBody().getChildObjects().get(i);
// Add the child object to the last section of the target file
lastSection.getBody().getChildObjects().add(childObject.deepClone());
}
// Save the target file to a different Word file
targetDoc.saveToFile("CopySection.docx", FileFormat.Docx_2019);
// Dispose resources
sourceDoc.dispose();
targetDoc.dispose();
}
}

Copy the Entire Document and Append it to Another in Java
Copying the full contents from one Microsoft Word document into another can be achieved using the Document.insertTextFromFile() method. This method enables you to seamlessly append the contents of a source document to a target document.
The steps to copy an entire document and append it to another are as follows:
- Create a Document object to represent the target file.
- Load the target file from the given file path.
- Insert the content of a different Word document into the target file using Document.insertTextFromFile() method.
- Save the updated target file to a new Word document.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class CopyEntireDocument {
public static void main(String[] args) {
// Specify the path of the source document
String sourceFile = "C:\\Users\\Administrator\\Desktop\\source.docx";
// Create a Document object
Document targetDoc = new Document();
// Load the target file
targetDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Insert content of the source file to the target file
targetDoc.insertTextFromFile(sourceFile, FileFormat.Docx);
// Save the target file to a different Word file
targetDoc.saveToFile("CopyEntireDocument.docx", FileFormat.Docx_2019);
// Dispose resources
targetDoc.dispose();
}
}
Create a Copy of a Word Document in Java
Spire.Doc for Java provides a straightforward way to create a duplicate of a Microsoft Word document by using the Document.deepClone() method.
To make a copy of a Word document, follow these steps:
- Create a Document object to relisent the source document.
- Load a Word file from the given file path.
- Create a copy of the document using Document.deepClone() method.
- Save the cloned document to a new Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class DuplicateDocument {
public static void main(String[] args) {
// Create a new document object
Document sourceDoc = new Document();
// Load a Word file
sourceDoc.loadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");
// Clone the document
Document newDoc = sourceDoc.deepClone();
// Save the cloned document as a docx file
newDoc.saveToFile("Copy.docx", FileFormat.Docx);
// Dispose resources
sourceDoc.dispose();
newDoc.dispose();
}
}
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.
Column charts, also known as bar charts, provide a visual comparison of data points across different categories. Whether you're summarizing sales figures, tracking project milestones, or visualizing survey results, column charts in Word provide a powerful way to translate complex data into an accessible, engaging format within your written materials.
In this article, you will learn how to create a clustered column chart and a stacked column chart in a Word document 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.10.6</version>
</dependency>
</dependencies>
Create a Clustered Column Chart in Word in Java
To insert a chart into a Microsoft Word document, you can use the Paragraph.appendChart(ChartType chartType, float width, float height) method. The ChartType enumeration provides various pre-defined chart types available in MS Word. To create a clustered column chart, you would specify the chart type as Column.
The steps to add a clustered column chart to a Word document using Java are as follows:
- Create a Document object.
- Add a section and a paragraph to the document.
- Add a clustered column chart to the paragraph using Paragraph.appendChart() method.
- Add series to the chart using Chart.getSeries().add() method.
- Set the chart title using Chart.getTilte().setText() method.
- Set other attributes of the chart using the methods available in the Chart object.
- Save the document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.*;
public class CreateClusteredColumnChart {
public static void main(String[] args) {
// Create a Document object
Document document = new Document();
// Add a section
Section section = document.addSection();
// Add a paragraph
Paragraph paragraph = section.addParagraph();
// Add a column chart
ShapeObject shape = paragraph.appendChart(ChartType.Column, 490, 250);
// Get the chart
Chart chart = shape.getChart();
// Clear the default data
chart.getSeries().clear();
// Add a series including series name, category names, and series values to chart
chart.getSeries().add("June",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 5000, 8000, 9000 });
// Add two more series
chart.getSeries().add("July",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 4000, 5000, 7000 });
chart.getSeries().add("August",
new String[] { "Cuba", "Mexico", "France"},
new double[] { 3500, 7000, 5000 });
// Set the chart title
chart.getTitle().setText("Sales by Country");
// Set the number format of the Y-axis
chart.getAxisY().getNumberFormat().setFormatCode("#,##0");
// Set the legend position
chart.getLegend().setPosition(LegendPosition.Bottom);
// Save to file
document.saveToFile("ClusteredColumnChart.docx", FileFormat.Docx_2019);
// Dispose resources
document.dispose();
}
}

Create a Stacked Column Chart in Word in Java
Creating a stacked column chart in a Word document follows a similar process to the clustered column chart. The only difference is specifying the chart type as Column_Stacked instead of Column.
The detailed steps to add a stacked column chart are:
- Create a Document object.
- Add a section and a paragraph to the document.
- Add a stacked column chart to the paragraph using Paragraph.appendChart() method.
- Add series to the chart using Chart.getSeries().add() method.
- Set the chart title using Chart.getTilte().setText() method.
- Set other attributes of the chart using the methods available in the Chart object.
- Save the document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;
import com.spire.doc.fields.shapes.charts.Chart;
import com.spire.doc.fields.shapes.charts.ChartType;
import com.spire.doc.fields.shapes.charts.LegendPosition;
public class CreateStackedColumnChart {
public static void main(String[] args) {
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add a paragraph
Paragraph paragraph = section.addParagraph();
//Add a stacked column chart
ShapeObject shape = paragraph.appendChart(ChartType.Column_Stacked, 490, 250);
//Get the chart
Chart chart = shape.getChart();
//Clear the default data
chart.getSeries().clear();
//Add a series including series name, category names, and series values to chart
chart.getSeries().add("Store A",
new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
new double[] { 2500, 4600, 2800, 5100 });
//Add another series
chart.getSeries().add("Store B",
new String[] { "Diet Coke", "Mountain Dew", "Diet Pesi", "Cherry Coke" },
new double[] { 4100, 3200, 3800, 4000 });
//Set the chart title
chart.getTitle().setText("Store Wise Soda Soft Drink Sales");
//Set the number format of the Y-axis
chart.getAxisY().getNumberFormat().setFormatCode("#,##0");
//Set the legend position
chart.getLegend().setPosition(LegendPosition.Bottom);
//Save to file
document.saveToFile("StackedColumnChart.docx", FileFormat.Docx_2019);
// Dispose resources
document.dispose();
}
}

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.
Editing a Word document is a common task that many people encounter in their daily lives, whether it's for work, school, or personal projects. From correcting spelling and grammar errors to rearranging content and formatting the document, the ability to edit a Word document efficiently is a valuable skill.
In this article, you will learn how to programmatically edit or modify a Word document using Spire.Doc for Java.
- Modify Text in a Word Document
- Change Formatting of Text in a Word Document
- Add New Elements to a Word Document
- Remove Paragraphs from a Word Document
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.10.6</version>
</dependency>
</dependencies>
Modify Text in a Word Document in Java
To retrieve the paragraph from a particular section, you can use the Section.getParagraphs().get() method. Once you have the target paragraph, you can then update its text content by calling the Paragraph.setText() method and passing in the new text you want to assign.
The following are the steps modify text in a Word document using Java:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.getSections().get() method.
- Get a specific paragraph using Section.getParagraphs().get() method.
- Reset the text of the paragraph using Paragraph.setText() method.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
public class ModifyText {
public static void main(String[] args) {
// Create a new document object
Document document = new Document();
// Load an existing Word file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Get a specific section
Section section = document.getSections().get(0);
// Get a specific paragraph
Paragraph paragraph = section.getParagraphs().get(0);
// Modify the text of the paragraph
paragraph.setText("The title has been modified");
// Save the document to a different Word file
document.saveToFile("ModifyText.docx", FileFormat.Docx);
// Dispose resource
document.dispose();
}
}

Change Formatting of Text in a Word Document in Java
To modify the formatting of specific text within a paragraph, you first need to access the target paragraph object. Once you have the paragraph, you can then iterate through its child elements to locate the individual text ranges.
For each text range found, you can update the formatting by using the methods under the CharacterFormat object. This allows you to set properties like font name, size, color, and other text-level formatting options for the selected text.
The steps to change text formatting in a Word document are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.getSections().get() method.
- Get a specific paragraph using Section.getParagraphs().get() method.
- Iterate through the child objects in the paragraph.
- Determine if a child object is a text range.
- Get a specific text range.
- Reset the text formatting using the methods under the CharacterFormat object.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class ChangeTextFormatting {
public static void main(String[] args) {
// Create a new document object
Document document = new Document();
// Load an existing Word file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Get a specific section
Section section = document.getSections().get(0);
// Get a specific paragraph
Paragraph paragraph = section.getParagraphs().get(1);
// Iterate through the child objects in the paragraph
for (int i = 0; i < paragraph.getChildObjects().getCount(); i++)
{
// Determine if a child object is text range
if (paragraph.getChildObjects().get(i) instanceof TextRange)
{
// Get a specific text range
TextRange textRange = (TextRange)paragraph.getChildObjects().get(i);
// Reset font name for it
textRange.getCharacterFormat().setFontName("Corbel Light");
// Reset font size for it
textRange.getCharacterFormat().setFontSize(11);
// Reset text color for it
textRange.getCharacterFormat().setTextColor(Color.blue);
// Apply italic to the text range
textRange.getCharacterFormat().setItalic(true);
}
}
// Save the document to a different Word file
document.saveToFile("ChangeFont.docx", FileFormat.Docx);
// Dispose resource
document.dispose();
}
}

Add New Elements to a Word Document in Java
When working with Word documents, the paragraph serves as the foundational unit for incorporating diverse elements like text, images, lists, and charts. To introduce a new paragraph within a specific section, you can leverage the Section.addParagraph() method.
Once the paragraph has been added, you can then proceed to add various other elements to it by utilizing the methods available within the Paragraph object.
The following are the steps to add new elements (text and images) to a Word document using Java:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.getSections() method.
- Add a paragraph to the section using Section.addParagraph() method.
- Add text to the paragraph using Paragraph.appendText() method.
- Add an image to the paragraph using Paragraph.appendPicture() method.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
public class AddNewElements {
public static void main(String[] args) {
// Create a new document object
Document document = new Document();
// Load an existing Word file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Get the last section
Section lastSection = document.getLastSection();
// Add a paragraph to the section
Paragraph paragraph = lastSection.addParagraph();
// Add text to the paragraph
paragraph.appendText("This text and the image shown below are added programmatically using Spire.Doc for Java.");
// Add an image to the paragraph
paragraph.appendPicture("C:\\Users\\Administrator\\Desktop\\logo.png");
// Create a paragraph style
ParagraphStyle style = new ParagraphStyle(document);
style.setName("FontStyle");
style.getCharacterFormat().setFontName("Times New Roman");
style.getCharacterFormat().setFontSize(12);
document.getStyles().add(style);
// Apply the style to the paragraph
paragraph.applyStyle(style.getName());
// Save the document to a different Word file
document.saveToFile("AddNewElements.docx", FileFormat.Docx);
// Dispose resource
document.dispose();
}
}

Remove Paragraphs from a Word Document in Java
To remove a specific paragraph from the collection of paragraphs within a document, you can call the ParagraphCollection.removeAt() method. This method takes the index of the paragraph you wish to remove as an argument, allowing you to selectively delete the desired paragraph from the document.
The steps to remove paragraphs from a Word document using Java are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.getSections().get() method.
- Remove a specific paragraph from the section using ParagraphCollection.removeAt() method.
- Save the updated document to a different Word file.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
public class RemoveParagraph {
public static void main(String[] args) {
// Create a new document object
Document document = new Document();
// Load an existing Word file
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");
// Get a specific section
Section section = document.getSections().get(0);
// Remove a specific paragraph
section.getParagraphs().removeAt(0);
// Save the document to a different Word file
document.saveToFile("RemoveParagraph.docx", FileFormat.Docx);
// Dispose resource
document.dispose();
}
}
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.