如何使用Apache POI保护Word文档的某些部分 [英] How to protect parts of a word document with Apache POI
问题描述
我需要通过Java保护Word(2013)文档的某些部分,并使它们为只读. Apache POI有可能吗?如果是的话,怎么办? 我只发现保护整个文档的可能性.
I need to protect parts of my Word (2013) document via Java and make them read only. Is that possible with Apache POI? And if yes, how? I only found the possibility to protect the whole document.
(我不仅需要保护页眉和页脚,还需要保护正文部分的某些行.)
(I need to protect not only the header and footer but also some lines in the body part.)
推荐答案
您可以在Word
文档中实施多种保护.如果要强制执行只读保护,则可以使用 CTPerm .
There are multiple kinds of protection you can enforcing in an Word
document. If you are enforcing read only protection, then you can exclude ranges from protection by marking them using CTPermStart and CTPerm.
示例:
import java.io.*;
import org.apache.poi.wp.usermodel.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPermStart;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEdGrp;
public class CreateWordPartialProtected {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
// create header
XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
XWPFParagraph paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun run = paragraph.createRun();
run.setText("The page header:");
// create footer
XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
paragraph = footer.createParagraph();
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("Page ");
paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
run = paragraph.createRun();
run.setText(" of ");
paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");
// the body content
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("This body part is protected.");
paragraph = document.createParagraph();
// CTPermStart marking the start of unprotected range
CTPermStart ctPermStart = document.getDocument().getBody().addNewPermStart();
ctPermStart.setEdGrp(STEdGrp.EVERYONE);
ctPermStart.setId("123456"); //note the Id
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("This body part is not protected.");
// CTPerm marking the end of unprotected range
document.getDocument().getBody().addNewPermEnd().setId("123456"); //note the same Id
paragraph = document.createParagraph();
paragraph = document.createParagraph();
run=paragraph.createRun();
run.setText("This body part is protected again.");
paragraph = document.createParagraph();
document.enforceReadonlyProtection(); //enforce readonly protection
FileOutputStream out = new FileOutputStream("CreateWordPartialProtected.docx");
document.write(out);
out.close();
document.close();
}
}
此代码需要所有ooxml-schemas-*.jar
模式的完整jar
,如
This code needs the full jar
of all of the schemas ooxml-schemas-*.jar
as mentioned in FAQ-N10025.
如果您想强制执行填充表单保护,那么它将变得更加复杂,因为这将需要多个部分.
If you would want to enforce filling forms protection, then it would be more complex since then multiple sections would be necessary.
这篇关于如何使用Apache POI保护Word文档的某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!