pdf文件预览 浏览器窗口名修改

pdf文件【后端修改】

1,效果图

pdf文件预览 浏览器窗口名修改_第1张图片

2,后端代码


    public static void renamePdfTitle(File file, String fileName, HttpServletResponse response) {
        if (file != null && file.exists()) {
            try (OutputStream outputStream = response.getOutputStream();
                 FileInputStream fileInputStream = new FileInputStream(file);
                 //加载pdf附件到PDF流中
                 PDDocument document = PDDocument.load(fileInputStream);
                 MyConnect connect = new MyConnect();
            ) {
                response.setCharacterEncoding("UTF-8");
                String showName = URLEncoder.encode(fileName, "UTF-8");
                response.setHeader("Content-Disposition", "inline;fileName=" + showName + ";fileName*=UTF-8''" + showName);
                //从PDF流中获得PDF文档属性对象
                PDDocumentInformation info = document.getDocumentInformation();
                //设置PDF文档属性对象的文件名称(最重要的环节)
                info.setTitle(fileName);
                document.setDocumentInformation(info);
                //修改完直接输出到响应体中
                document.save(outputStream);

            } catch (Exception e) {
                log.error("viewPdfFile:", e);
            }
        }
    }

详情指路:https://blog.csdn.net/shy_1762538422/article/details/113663650?spm=1001.2014.3001.5506

word文件【前端修改】

1,前端代码:


    var win = window.open(url, "", "width=100");
    settimeout(function() {
        win.document.title = “标题”;
    },500);
    

注意:不适用pdf文件的标题修改,这个前端代码待验证

你可能感兴趣的:(Java,java)