java删除某些段落word
java删除某些段落word
批注是一种常用于对特定文档内容进行注解的工具或方法,起到解释说明、标记指正的作用。在本篇文章中,将介绍如何操作Word批注的方法,包括:
1. 添加批注:添加文本到批注、插入图片到批注;
1.1 给指定段落添加批注
1.2 给指定字符串添加批注
2. 回复批注;
. 修改或替换批注:用文本替换批注中的文本内容、用文本替换批注中的图片、用图片替换批注中的图片;
4. 删除批注:删除指定批注中的所有内容、删除指定批注中的指定内容
5. 读取批注
5.1 读取批注中的文本
5.2 读取批注中的图片
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:通过获取jar包,并解压。解压后,将lib文件夹下的Spire.Doc.jar文件导入java程序。
方法2:通过添加maven依赖导入到maven项目,参考导入步骤。
Java示例代码
【示例1】给段落添加批注(文本、图片)import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
public class AddComment {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document(test.docx);
//获取指定段落
Section sec = doc.getSecti().get(0);
Paragraph para= sec.getParagraphs().get();
//插入文本到批注
Comment comment = para.appendComment(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样。);
comment.getFormat().setAuthor(审校组);
//插入图片到批注
comment.getBody().addParagraph().appendPicture(tp.png);
//保存文档
doc.saveToFile(AddComment.docx, FileFormat.Docx_2010);
}
}
批注添加效果:
【示例2】给指定字符串添加批注import com.spire.doc.*;
import com.spire.doc.documents.CommentMark;
import com.spire.doc.documents.CommentMarkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.Comment;
public class AddCommentToCharacters {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document();
doc.loadFromFile(test.docx);
//查指定字符串
TextSelection[] selecti = doc.findAllString(皱状厚膜, true, false);
//获取关键字符串所在段落
Paragraph para = selecti[0].getAsOneRange().getOwnerParagraph();
int index = para.getChildObjects().indexOf(selecti[0].getAsOneRange());
//添加批注ID
CommentMark start = new CommentMark(doc);
start.setCommentId(1);
start.setType(CommentMarkType.Comment_Start);
CommentMark end = new CommentMark(doc);
end.setType(CommentMarkType.Comment_End);
end.setCommentId(1);
//添加批注内容
String str = 给指定字符串添加批注;
Comment comment = new Comment(doc);
comment.getFormat().setCommentId(1);
comment.getBody().addParagraph().appendText(str);
comment.getFormat().setAuthor(作者:);
comment.getFormat().setInitial(CM);
para.getChildObjects().insert(index, start);
para.getChildObjects().insert(index 1, selecti[0].getAsOneRange());
para.getChildObjects().insert(index 2,end);
para.getChildObjects().insert(index , comment);
//保存文档
doc.saveToFile(字符串批注.docx,FileFormat.Docx_201);
doc.dispose();
}
}
批注添加效果:
【示例】回复批注import com.spire.doc.*;
import com.spire.doc.fields.Comment;
public class ReplyComment {
public static void main(String[] args) throws Exception{
//加载测试文档
Document doc = new Document(AddComment.docx);
//获取指定批注
Comment comment = doc.getComments().get(0);
//回复批注
Comment relyC= new Comment(doc);
relyC.getFormat().setAuthor(实验组);
relyC.getBody().addParagraph().appendText(已完成。);
comment.replyToComment(relyC);
//保存文档
doc.saveToFile(ReplyComment.docx,FileFormat.Docx_2010);
}
}
批注回复效果:
【示例4】修改或替换批注import com.spire.doc.*;
public class ModifyComment {
public static void main(String[] args){
//加载含有批注的测试文档
Document doc = new Document(sample.docx);
//获取第一个批注中的第一段,用文本替换原有批注中的文本
doc.getComments().get(0).getBody().getParagraphs().get(0).replace(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样。,参照以下实验方法!,false,false);
//获取第一个批注中的第二段,用文本替换原有批注中的图片
doc.getComments().get(0).getBody().getParagraphs().get(1).setText(请上报管理科!);
//获取第一个批注中的第三段,删除原有图片,再调用方法添加新图片(用图片替换图片)
doc.getComments().get(0).getBody().getParagraphs().get(2).getChildObjects().removeAt(0);
doc.getComments().get(0).getBody().getParagraphs().get(2).appendPicture(2.png);
//保存文档
doc.saveToFile(ModifyComment.docx,FileFormat.Docx_2010);
}
}
修改或替换结果:
【示例5】删除批注import com.spire.doc.*;
import com.spire.doc.FileFormat;
public class DeleteComment{
public static void main(String[] args) {
//加载测试文档
Document doc = new Document(AddComment.docx);
//调用方法删除指定批注(删除批注中的所有内容)
doc.getComments().removeAt(0);
//删除指定批注中的指定段落(删除批注中的部分内容)
doc.getComments().get(0).getBody().getParagraphs().get(1).getChildObjects().removeAt(0);
//保存文档
doc.saveToFile(DeleteComment, FileFormat.Docx_2010);
}
}
批注删除效果:
【示例6】读取批注中的文本import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
import com.spire.doc.fields.TextRange;
public class ReadComment {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document();
doc.loadFromFile(测试文档.docx);
//实例化String类型变量
String text = ;
/*//获取指定批注中的文本
Comment comment = doc.getComments().get(0);
Paragraph paragraph = comment.getBody().getParagraphs().get(0);
for(Object obj:paragraph.getChildObjects()) {
if(obj instanceof TextRange){
TextRange textRange = (TextRange) obj;
text = text textRange.getText();
}
}*/
//遍历所有批注
for(int i = 0;i
Comment comment = doc.getComments().get(i);
//遍历所有批注中的段落
for(int j= 0;j
Paragraph paragraph = comment.getBody().getParagraphs().get(j);
//遍历段落中的对象
for (Object object : paragraph.getChildObjects()) {
//读取文本
if (object instanceof TextRange) {
TextRange textRange = (TextRange) object;
text = text textRange.getText();
}
}
}
}
//输入文本内容
println(text);
}
}
批注文本读取结果:
【示例7】读取批注中的图片import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.Comment;
import com.spire.doc.fields.DocPicture;
import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class ExtractImgsInComment {
public static void main(String[] args) throws IOException{
//加载测试文档
Document doc = new Document();
doc.loadFromFile(AddComment.docx);
//创建ArrayList数组对象
ArrayList images = new ArrayList();
//遍历所有批注
for(int i = 0;i
Comment comment = doc.getComments().get(i);
//遍历所有批注中的段落
for(int j= 0;j
Paragraph paragraph = comment.getBody().getParagraphs().get(j);
//遍历段落中的对象
for (Object object : paragraph.getChildObjects()) {
//获取图片对象
if(object instanceof DocPicture){
DocPicture picture = (DocPicture) object;
images.add(picture.getImage());
}
}
}
}
//提取图片,并指定图片格式
for (int z = 0; z
File file = new File(String.format(图片-%d.png, z));
ImageIO.write((RenderedImage) images.get(z), PG, file);
}
}
}
批注图片读取结果:
(本文完)
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:mac brew npm安装教程
推荐阅读
留言与评论(共有 15 条评论) |
本站网友 高中教育 | 25分钟前 发表 |
FileFormat.Docx_2010);}}批注回复效果:【示例4】修改或替换批注import com.spire.doc.*;public class ModifyComment {public static void main(String[] args){//加载含有批注的测试文档Document doc = new Document(sample.docx);//获取第一个批注中的第一段,用文本替换原有批注中的文本doc.getComments().get(0).getBody().getParagraphs().get(0).replace(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样 | |
本站网友 深圳二手房出售 | 18分钟前 发表 |
z));ImageIO.write((RenderedImage) images.get(z) | |
本站网友 生猪养殖 | 15分钟前 发表 |
FileFormat.Docx_201);doc.dispose();}}批注添加效果:【示例】回复批注import com.spire.doc.*;import com.spire.doc.fields.Comment;public class ReplyComment {public static void main(String[] args) throws Exception{//加载测试文档Document doc = new Document(AddComment.docx);//获取指定批注Comment comment = doc.getComments().get(0);//回复批注Comment relyC= new Comment(doc);relyC.getFormat().setAuthor(实验组);relyC.getBody().addParagraph().appendText(已完成 | |
本站网友 kekoukele | 7分钟前 发表 |
FileFormat.Docx_2010);}}批注删除效果:【示例6】读取批注中的文本import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.Comment;import com.spire.doc.fields.TextRange;public class ReadComment {public static void main(String[] args) {//加载测试文档Document doc = new Document();doc.loadFromFile(测试文档.docx);//实例化String类型变量String text = ;/*//获取指定批注中的文本Comment comment = doc.getComments().get(0);Paragraph paragraph = comment.getBody().getParagraphs().get(0);for(Object obj | |
本站网友 投资收益率多少合适 | 24分钟前 发表 |
FileFormat.Docx_2010);}}批注回复效果:【示例4】修改或替换批注import com.spire.doc.*;public class ModifyComment {public static void main(String[] args){//加载含有批注的测试文档Document doc = new Document(sample.docx);//获取第一个批注中的第一段,用文本替换原有批注中的文本doc.getComments().get(0).getBody().getParagraphs().get(0).replace(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样 | |
本站网友 北京个人写真 | 0秒前 发表 |
paragraph.getChildObjects()) {//读取文本if (object instanceof TextRange) {TextRange textRange = (TextRange) object;text = text textRange.getText();}}}}//输入文本内容println(text);}}批注文本读取结果:【示例7】读取批注中的图片import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.Comment;import com.spire.doc.fields.DocPicture;import javax.imageio.ImageIO;import java.awt.image.RenderedImage;import java.io.File;import java.io.IOException;import java.util.ArrayList;public class ExtractImgsInComment {public static void main(String[] args) throws IOException{//加载测试文档Document doc = new Document();doc.loadFromFile(AddComment.docx);//创建ArrayList数组对象ArrayList images = new ArrayList();//遍历所有批注for(int i = 0;iComment comment = doc.getComments().get(i);//遍历所有批注中的段落for(int j= 0;j Paragraph paragraph = comment.getBody().getParagraphs().get(j);//遍历段落中的对象for (Object object | |
本站网友 5944免费空间 | 13分钟前 发表 |
comment);//保存文档doc.saveToFile(字符串批注.docx | |
本站网友 失眠怎么办 | 14分钟前 发表 |
end);para.getChildObjects().insert(index | |
本站网友 红枣的营养价值 | 21分钟前 发表 |
用图片替换批注中的图片;4. 删除批注:删除指定批注中的所有内容 | |
本站网友 摸摸女 | 6分钟前 发表 |
解压后,将lib文件夹下的Spire.Doc.jar文件导入java程序 | |
本站网友 一键还原奥运版 | 2分钟前 发表 |
解压后,将lib文件夹下的Spire.Doc.jar文件导入java程序 | |
本站网友 上海骨科医院 | 22分钟前 发表 |
Java示例代码【示例1】给段落添加批注(文本 | |
本站网友 安贞医院怎么样 | 4分钟前 发表 |
图片)import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.Comment;public class AddComment {public static void main(String[] args) {//加载测试文档Document doc = new Document(test.docx);//获取指定段落Section sec = doc.getSecti().get(0);Paragraph para= sec.getParagraphs().get();//插入文本到批注Comment comment = para.appendComment(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样 | |
本站网友 win7官方 | 26分钟前 发表 |
图片)import com.spire.doc.*;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.Comment;public class AddComment {public static void main(String[] args) {//加载测试文档Document doc = new Document(test.docx);//获取指定段落Section sec = doc.getSecti().get(0);Paragraph para= sec.getParagraphs().get();//插入文本到批注Comment comment = para.appendComment(请在试验中将包含以下特征的实验样本记录在册,并整理好周记录报表,供后续观察取样 |