本篇內(nèi)容主要講解“java怎么將word轉(zhuǎn)為pdf并自定義水印”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java怎么將word轉(zhuǎn)為pdf并自定義水印”吧!
創(chuàng)新互聯(lián)公司專注于商水網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供商水營銷型網(wǎng)站建設(shè),商水網(wǎng)站制作、商水網(wǎng)頁設(shè)計(jì)、商水網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造商水網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供商水網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
鏈接:https://pan.baidu.com/s/1xvxXA6Wr_HKdvTis8z1FKw
提取碼:790o
public class Word2PdfUtil { public static void main(String[] args) { doc2pdf("E:\\test.doc", "E:\\test.pdf"); } /** * word轉(zhuǎn)pdf */ public static void doc2pdf(String inPath, String outPath) { // 驗(yàn)證License if (!getLicense()) { return; } FileOutputStream os = null; try { System.out.println("開始轉(zhuǎn)換..."); // 新建一個(gè)空白pdf文檔 File file = new File(outPath); os = new FileOutputStream(file); //待轉(zhuǎn)換的文件,添加水印 Document doc = new Document(inPath); insertWatermarkText(doc, "我是水印"); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉(zhuǎn)換 doc.save(os, SaveFormat.PDF); System.out.println("轉(zhuǎn)換完成..."); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 驗(yàn)證簽名 * */ private static boolean getLicense() { boolean result = false; try { InputStream is = Word2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 給pdf生成水印 */ private static void insertWatermarkText(Document doc, String watermarkText) throws Exception { System.out.println("開始添加水印..."); Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT); // 水印內(nèi)容 watermark.getTextPath().setText(watermarkText); // 水印字體 watermark.getTextPath().setFontFamily("宋體"); // 水印寬度 watermark.setWidth(500); // 水印高度 watermark.setHeight(100); // 旋轉(zhuǎn)水印 watermark.setRotation(-40); // 水印顏色 watermark.getFill().setColor(Color.lightGray); watermark.setStrokeColor(Color.lightGray); watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE); watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE); watermark.setWrapType(WrapType.NONE); watermark.setVerticalAlignment(VerticalAlignment.CENTER); watermark.setHorizontalAlignment(HorizontalAlignment.CENTER); Paragraph watermarkPara = new Paragraph(doc); watermarkPara.appendChild(watermark); for (Section sect : doc.getSections()) { insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN); } System.out.println("結(jié)束添加水印..."); } private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception { HeaderFooter header = sect.getHeadersFooters() .getByHeaderFooterType(headerType); if (header == null) { header = new HeaderFooter(sect.getDocument(), headerType); sect.getHeadersFooters().add(header); } header.appendChild(watermarkPara.deepClone(true)); }
打開注冊(cè)表(regedit),找到HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft,右鍵改權(quán)限為完全許可
到此,相信大家對(duì)“java怎么將word轉(zhuǎn)為pdf并自定義水印”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!