Microsoft Word provides Track Changes for users to add, delete or change the content of documents and keeps traces of these changes. When you agree the changes, you can choose to accept Track Changes to make the document keep the final text information, while if you disagree the changes, you can restore the document to its original state by rejecting Track Changes.
In this article, I’ll introduce how to accomplish the functions above in Java applications using Free Spire.Doc for Java library. Before running codes, please download the package from the link, and manually add Spire.doc.jar to IDEA. Or reference it using the following Maven configurations.
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Enable Track Changes
import com.spire.doc.*;
public class OpenTrackChanges {
public static void main(String[] args) {
//Create Word document.
Document document = new Document();
//Load the file from disk.
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");
//Enable the track changes.
document.setTrackChanges(true);
//Save to file.
document.saveToFile("output/enableTrackChanges.docx", FileFormat.Docx_2013);
}
}
Output
Accept
or Reject Track Changes
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
public class SetTrackChanges {
public static void main(String[] args) {
//Create Word document.
Document document = new Document();
//Load the file from disk.
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Accept or Reject TrackChanges.docx");
//Get the first section and the paragraph we want to accept/reject the changes.
Section sec = document.getSections().get(0);
Paragraph para = sec.getParagraphs().get(0);
//Accept the changes or reject the changes.
//para.getDocument().acceptChanges();
para.getDocument().rejectChanges();
//Save to file.
document.saveToFile("output/acceptOrRejectTrackedChanges.docx", FileFormat.Docx_2013);
}
}
Output
No comments:
Post a Comment