java 给pdf文档页面设置页面权限

动态设置pdf文档的页面权限 例如一个pdf 第1.3.8页可以查看 其他不可以查看

下面代码 直接套 就能用了 改一下路径 参数这些

/**
     *
     * @param outPath 输入的newPdf文件 没有权限
     * @param inputpath 临时文件 过后就删
     * @param from 默认从第一页1开始
     * @param end 自己设置 第几页就开始隐藏
     * @param overPath
     * @return
     */
    public String jurisdiction(String outPath,String inputpath,Integer from,Integer end,String overPath){
        overPath="D:/test/newPdf2.pdf";
        from=1;//从第一页开始
        end=2;//到第二页结束 后面的页面隐藏
        //动态生成pdf页面的权限 1.3.8页面都可以
        List<Integer> list=new ArrayList<>();//动态生成
        list.add(1);
        list.add(2);
        list.add(3);
        //拿到要加权限的页面 从第几页开始
        Integer number=list.size();
        try {
            Integer rscount = CoverPdfUtils.splitPDFFile(outPath,inputpath, list);
            if(rscount==0){
                return "失败 生成权限参数为0";
            }
            //添加页面 并为页面设置样式 页面
            CoverPdfUtils.addjurisdiction(inputpath,number+1,overPath,"林鸟鸟","权限不足,无法查看");

            return inputpath;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

 上面是实现类 下面是工具类

有个PdfFileUtils.pdfInsertWatermark() 如果没有的可以看下我主页里面的pdf加水印方法就是

其实就是一个加水印的类 你也可以自己重新保存

//pdf页面权限访问
public class CoverPdfUtils {


    /**
     *@param count 要添加几页水印页面
     * @param inputPath 输入的路径
     * @param outPath 生成新的存放路径
     * @param textWatermark 输入档案馆的水印
     * @param textWaterpath 输入修改人的信息 和修改时间
     * @throws Exception
     */
    public static void addjurisdiction(String inputPath,Integer count,
                                       String outPath,String textWatermark,
                                       String textWaterpath) throws Exception {

        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile(inputPath);
        //只设置一页水印 水印内容 权限不足 灰色字体
        pdf.getPages().add();
        //保存文档
        pdf.saveToFile(inputPath);

        //设置文档页面大小
        PdfFileUtils.amendpage(inputPath, inputPath);

        pdf.close();
        //设置水印 生成新的权限文档
        PdfFileUtils.pdfInsertWatermark(count,inputPath,outPath,textWatermark,textWaterpath,1);

    }

    /**
     * 截取pdfFile的第from页至第end页,组成一个新的文件名
     * @param respdfFile  需要分割的PDF
     * @param savepath  新PDF
     *             //返回总页数
     */
    public static Integer splitPDFFile(String respdfFile,
                                    String savepath, List<Integer> pages) {
        Document document = null;
        PdfCopy copy = null;
        int n= 0;
        try {
            PdfReader reader = new PdfReader(respdfFile);
             n = reader.getNumberOfPages();
            if(pages.size()==0){
                for (int i=0;i<n;i++){
                    pages.add(i);
                }
            }
            ArrayList<String> savepaths = new ArrayList<String>();
//            String staticpath = respdfFile.substring(0, respdfFile.lastIndexOf("\\")+4);
            //String savepath = staticpath+ newFile;
            savepaths.add(savepath);
            document = new Document(reader.getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
            document.open();
            for (Integer list : pages){
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, list);
                copy.addPage(page);
            }
            document.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch(DocumentException e) {
            e.printStackTrace();
        }
        return n;
    }
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值