操作aspose版本
aspose-words-21.4
官网下载地址
https://releases.aspose.com/words/java/
问题
1,每次操作文档的时候都要重新保存,不然文件损坏,打开的文件就是乱码的
记录一些简单的功能设置:
Document doc = new Document();
//设置表格超过页面边距,看不到数据,为兼容模式
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2019);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(readHTML("D:\\1.html", StandardCharsets.UTF_8));
//获取所有的段落
for (Section section : doc.getSections()) {
Body body = section.getBody();
for (Paragraph paragraph : body.getParagraphs()) {
double firstLineIndent = paragraph.getParagraphFormat().getFirstLineIndent();
if (!paragraph.getParagraphFormat().isHeading() && firstLineIndent > 0.0) {
//设置首行 为2字符
paragraph.getParagraphFormat().setCharacterUnitFirstLineIndent(2);
}
}
}
//获取所有图片
NodeCollection<Shape> shapes = (NodeCollection<Shape>) doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : shapes)
{
if (shape.hasImage())
{
Paragraph paragraph = (Paragraph)shape.getAncestor(NodeType.PARAGRAPH);
if (paragraph != null)
{
//设置无首行缩进
paragraph.getParagraphFormat().setFirstLineIndent(0);
}
}
}
doc.save("D:\\钻井地质设计2.doc",SaveOptions.createSaveOptions(SaveFormat.DOC));
读取doc文件 转字符串内容
public static String readHTML(String path, Charset utf8) throws Exception {
StringBuilder sb = new StringBuilder();
File file = new File(path);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
取消表单重复标题代码
Document doc = new Document("cs2.doc");
for (Table t : (Iterable<Table>)doc.getChildNodes(NodeType.TABLE, true)) {
for (Row r : t.getRows()) {
r.getRowFormat().setHeadingFormat(false);
}
}
doc.save("out.doc");
设置每页重复表头
//设置换页重复表头
for (Table t : (Iterable<Table>) document.getChildNodes(NodeType.TABLE, true)) {
t.getFirstRow().getRowFormat().setHeadingFormat(true);
}
设目录字体,行距为固定值20
ParagraphCollection firstParagraph = doc.getSections().get(1).getBody().getParagraphs();
for (Paragraph param:firstParagraph){
for (Run r :param.getRuns()){
if (!r.getText().contains("目录")){
r.getParentParagraph().getParagraphFormat().setLineSpacingRule( LineSpacingRule.EXACTLY);
r.getParentParagraph().getParagraphFormat().setLineSpacing( 20);
r.getFont().setName("宋体");
}
}
}
PDF加密,设置内容不可编辑,只可打印
Document doc = new Document("E:\\Temp\\cs.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "password", PdfEncryptionAlgorithm.RC_4_128);
encryptionDetails.setPermissions(PdfPermissions.PRINTING);
saveOptions.setEncryptionDetails(encryptionDetails);
doc.save("E:\\cs.pdf", saveOptions);
方法名 | 描述 |
---|---|
ALLOW_ALL | 允许对 PDF 文档进行所有操作。 |
CONTENT_COPY | 通过非受控操作从文档中复制或提取文本和图形CONTENT_COPY_FOR_ACCESSIBILITY. |
CONTENT_COPY_FOR_ACCESSIBILITY | 提取文本和图形(以支持残障用户的可访问性或用于其他目的)。 |
DISALLOW_ALL | 禁止对 PDF 文档的所有操作。 |
DOCUMENT_ASSEMBLY | 组合文档(插入、旋转或删除页面并创建文档大纲项目或缩略图),即使MODIFY_CONTENTS清楚了。 |
FILL_IN | 填写现有的交互式表单字段(包括签名字段),即使MODIFY_CONTENTS清楚了。 |
HIGH_RESOLUTION_PRINTING | 根据依赖于实现的算法,将文档打印为可以生成 PDF 内容的忠实数字副本的表示。 |
MODIFY_ANNOTATIONS | 添加或修改文本注释,填写交互式表单域,并且,如果MODIFY_CONTENTS也可以设置、创建或修改交互式表单域(包括签名域)。 |
MODIFY_CONTENTS | 通过非由控制的操作修改文档的内容MODIFY_ANNOTATIONS, FILL_IN, 和DOCUMENT_ASSEMBLY. |
PRINTING | 打印文档(可能不是最高质量级别,取决于是否HIGH_RESOLUTION_PRINTING也已设置)。 |