【问题标题】:How to allow more rows to be created in apache POI with read only protection?如何允许在具有只读保护的 apache POI 中创建更多行?
【发布时间】:2022-07-07 17:00:48
【问题描述】:

我正在创建一个 docx 文件,该文件需要只读保护,但某些字段除外。对于这些领域,我已经解决了这个问题:

pInit.getCTP().addNewPermStart();
...
pEnd.getCTP().addNewPermEnd();

但现在我想允许向 XWPFTable 添加新行,但是这一行:

document.enforceReadonlyProtection(wordLockedPass, HashAlgorithm.sha1);

阻止此功能,我不知道该怎么办。

提前致谢!

【问题讨论】:

    标签: java apache-poi xwpf


    【解决方案1】:

    如果表格位于可编辑区域(PermStartPermEnd 之间),那么它将是可编辑的,包括添加行。

    PermStartPermEnd 可以插入到文档正文中使用

    // 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
    

    PermStartPermEnd 之间的所有正文元素都可以在受保护的文档中进行编辑。对于 PermStartPermEnd 之间的表也是如此。

    如果只有部分表格可以编辑,那么PermStartPermEnd 也可以插入表格中。例如,如果只有表的最后一行是可编辑的,而新行是可插入的,那么:

    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 文档。第二个是在标题行之后的表格中。因此,该标题行受到保护。最后一行位于该可编辑区域中。所以这一行是可编辑的,并且可以在该行的上方和下方插入新的行。

    【讨论】:

    • 它不允许我创建新行,这段代码只是让我创建另一个删除保护的行:(
    • @user19495148:无法复制。对我有用,请参阅我的答案下的补充。
    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多