如果表格位于可编辑区域(PermStart 和 PermEnd 之间),那么它将是可编辑的,包括添加行。
PermStart 和 PermEnd 可以插入到文档正文中使用
// CTPermStart marking the start of unprotected range
CTPermStart ctPermStart = document.getDocument().getBody().addNewPermStart();
ctPermStart.setEdGrp(STEdGrp.EVERYONE);
ctPermStart.setId("123456"); //note the Id
和
// CTPerm marking the end of unprotected range
document.getDocument().getBody().addNewPermEnd().setId("123456"); //note the same Id
PermStart 和 PermEnd 之间的所有正文元素都可以在受保护的文档中进行编辑。对于 PermStart 和 PermEnd 之间的表也是如此。
如果只有部分表格可以编辑,那么PermStart 和PermEnd 也可以插入表格中。例如,如果只有表的最后一行是可编辑的,而新行是可插入的,那么:
XWPFTable table = ...;
...
// CTPermStart marking the start of unprotected range
ctPermStart = table.getCTTbl().addNewPermStart();
ctPermStart.setEdGrp(STEdGrp.EVERYONE);
ctPermStart.setId("789012"); //note the Id
XWPFTableRow row = table.createRow();
// CTPerm marking the end of unprotected range
table.getCTTbl().addNewPermEnd().setId("789012"); //note the same Id
...
完整示例:
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();
XWPFTable table = document.createTable(1, 3);
table.setWidth("100%");
table.getRow(0).getCell(0).setText("Column 1");
table.getRow(0).getCell(1).setText("Column 2");
table.getRow(0).getCell(2).setText("Column 3");
// CTPermStart marking the start of unprotected range
ctPermStart = table.getCTTbl().addNewPermStart();
ctPermStart.setEdGrp(STEdGrp.EVERYONE);
ctPermStart.setId("789012"); //note the Id
XWPFTableRow row = table.createRow();
// CTPerm marking the end of unprotected range
table.getCTTbl().addNewPermEnd().setId("789012"); //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("passwd", org.apache.poi.poifs.crypt.HashAlgorithm.sha1); //enforce readonly protection
FileOutputStream out = new FileOutputStream("CreateWordPartialProtected.docx");
document.write(out);
out.close();
document.close();
}
}
此代码使用apache poi 5.2.2 进行测试和工作。它生成具有两个可编辑区域的 Word 文档。第二个是在标题行之后的表格中。因此,该标题行受到保护。最后一行位于该可编辑区域中。所以这一行是可编辑的,并且可以在该行的上方和下方插入新的行。