poi word文档根据书签图片(记录一下)

maven

<dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>

正式代码

/**
     * 根据word文件流根据书签位置签章
     *
     * @param fileUrl docx文件地址
     * @param pdfName pdf文件名称
     * @param type 1=文书,2=证照
     * @return
     */
    public static String fileUrlToPdf(String fileUrl, String pdfName, Integer type) {
        URL url = null;
        InputStream is = null;
        HttpURLConnection httpUrl = null;
        try {
            url = new URL(fileUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();
            httpUrl.getInputStream();
            is = httpUrl.getInputStream();
            //加载Word测试文档
            Document doc = new Document();
            if (type == 1) {
                // 奇葩,用doc读不出来,用Auto能读出来shit
                doc.loadFromStream(is, FileFormat.Auto);
            } else {
                doc.loadFromStream(is, FileFormat.Docx);
            }
            //获取书签
            BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
            if (type == 1) {
                bookmarksNavigator.moveToBookmark("书签");
                if (null == bookmarksNavigator.getCurrentBookmark()) {
                    bookmarksNavigator.moveToBookmark("签章");
                }
            } else {
                bookmarksNavigator.moveToBookmark("签章");
                if (null == bookmarksNavigator.getCurrentBookmark()) {
                    bookmarksNavigator.moveToBookmark("书签");
                }
            }
            //如果成功获取书签再签章
            if (null != bookmarksNavigator.getCurrentBookmark()) {
                //用新的内容添加到原书签位置
                Paragraph paragraph = new Paragraph(doc);
                //添加电子章图片
                DocPicture picture = paragraph.appendPicture("cqjtj.png");
                paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); // 这个语句好像没用的
                //设置图片浮于文字下方
                picture.setTextWrappingStyle(TextWrappingStyle.Behind); // 有用
                // 调整图片水平位置
                Integer high = -50;
                Integer width = 0;
                picture.setHorizontalPosition(width);
                picture.setVerticalPosition(high);
                TextBodyPart bodyPart = new TextBodyPart(doc);

                bodyPart.getBodyItems().add(paragraph);  // 可以的
//        bodyPart.getBodyItems().insert(0,paragraph); // 可以的
//        bookmarksNavigator.replaceBookmarkContent(bodyPart); // 可以的-替换书签
//        bookmarksNavigator.replaceBookmarkContent(bodyPart,true);
//        bookmarksNavigator.replaceBookmarkContent(bodyPart,true,true);
                bookmarksNavigator.insertTextBodyPart(bodyPart);  // 在书签出插入
//        doc.saveToFile("C:\\Users\\11296\\Desktop\\word\\" + name, FileFormat.Docx);//输出word
                doc.saveToFile(pdfName, com.spire.doc.FileFormat.PDF);//输出pdf格式有页数限制,仅支持3页
                doc.close();

                StorageClient1 fastClient = EditFileUtil.getFastClient();
                File file1 = new File(pdfName);
                FileInputStream fis = new FileInputStream(file1);
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                //创建一个Buffer字符串
                byte[] buffer = new byte[1024];
                //每次读取的字符串长度,如果为-1,代表全部读取完毕
                int len = 0;
                //使用一个输入流从buffer里把数据读取出来
                while ((len = fis.read(buffer)) != -1) {
                    //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                    outStream.write(buffer, 0, len);
                }
                // 上传文件,返回相对路径
                String fastId = fastClient.upload_file1(outStream.toByteArray(), "pdf", null);
                fis.close();
                outStream.close();
                FileUtils.deleteFile(pdfName); // 删除文件
                return fastId;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpUrl != null) {
                httpUrl.disconnect();
            }
        }
    }

测试代码

/**
     * 
     * @param path 目录
     * @param name 名字
     * @throws IOException
     */
    public static void word(String path, String name) throws IOException {
//加载包含书签的Word文档
        Document doc = new Document();
        doc.loadFromFile(path + name);
//获取书签
        BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
        bookmarksNavigator.moveToBookmark("签章");

//用新的内容替换原书签内容
        Paragraph paragraph = new Paragraph(doc);
        //添加电子章图片
        DocPicture picture = paragraph.appendPicture("C:\\Users\\11296\\Desktop\\cqjtj.png");
        paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); // 这个语句好像没用的
//        BigDecimal w = new BigDecimal(picture.getWidth());
//        w = w.multiply(new BigDecimal(0.1));
//        BigDecimal h = new BigDecimal(picture.getHeight());
//        h = h.multiply(new BigDecimal(-0.1));
        //设置图片浮于文字下方
        picture.setTextWrappingStyle(TextWrappingStyle.Behind); // 有用
        Integer high = 0;
        Integer width = 0;
//        if (name.contains("船舶营业运输证注销登记证明书套打")) {
//
//        } else {
            high = -50;
//        }
        picture.setHorizontalPosition(width);
        picture.setVerticalPosition(high);
        TextBodyPart bodyPart = new TextBodyPart(doc);

        bodyPart.getBodyItems().add(paragraph);  // 可以的
//        bodyPart.getBodyItems().insert(0,paragraph); // 可以的
//        bookmarksNavigator.replaceBookmarkContent(bodyPart); // 可以的-替换书签
//        bookmarksNavigator.replaceBookmarkContent(bodyPart,true);
//        bookmarksNavigator.replaceBookmarkContent(bodyPart,true,true);
        bookmarksNavigator.insertTextBodyPart(bodyPart);  // 在书签出插入

//        doc.saveToFile("C:\\Users\\11296\\Desktop\\word\\" + name, FileFormat.Docx);//输出word
        doc.saveToFile("C:\\Users\\11296\\Desktop\\word\\" + name.split("\\.")[0] + ".pdf", com.spire.doc.FileFormat.PDF);//输出pdf格式有页数限制,仅支持3页
        doc.dispose();
        System.out.println("完成");
    }

你可能感兴趣的:(java)