您好,登錄后才能下訂單哦!
本篇內容主要講解“java怎么將word轉為pdf并自定義水印”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java怎么將word轉為pdf并自定義水印”吧!
鏈接: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轉pdf */ public static void doc2pdf(String inPath, String outPath) { // 驗證License if (!getLicense()) { return; } FileOutputStream os = null; try { System.out.println("開始轉換..."); // 新建一個空白pdf文檔 File file = new File(outPath); os = new FileOutputStream(file); //待轉換的文件,添加水印 Document doc = new Document(inPath); insertWatermarkText(doc, "我是水印"); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 doc.save(os, SaveFormat.PDF); System.out.println("轉換完成..."); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 驗證簽名 * */ 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); // 水印內容 watermark.getTextPath().setText(watermarkText); // 水印字體 watermark.getTextPath().setFontFamily("宋體"); // 水印寬度 watermark.setWidth(500); // 水印高度 watermark.setHeight(100); // 旋轉水印 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("結束添加水印..."); } 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)); }
打開注冊表(regedit),找到HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft,右鍵改權限為完全許可
到此,相信大家對“java怎么將word轉為pdf并自定義水印”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。