本文實例講述了java實現word文檔轉pdf並添加水印的方法。分享給大家供大家參考,具體如下:
前段時間,項目需要將上傳的Word文檔在瀏覽器瀏覽,思來想去,把word文檔轉成pdf就好了,於是乎研究了一下。
將word文檔轉化為PDF是項目中常見的需求之一,目前主流的方法可以分為兩大類,一類是利用各種Office應用進行轉換,譬如Microsoft Office、WPS以及LiberOffice,另一種是利用各種語言提供的對於Office文檔讀取的接口(譬如Apache POI,jacob,docx4j,openoffice),這些要么收費,要么要裝插件,要么轉換之后樣式走形,亂碼等等。
我使用Aspose.Words for Java 可以導出復雜WORD PDF HTML 多種數據格式
官方下載地址:http://www.aspose.com/java/word-component.aspx
我所用的版本是aspose-words-14.9.0-jdk16。分享給大家:
鏈接:https://pan.baidu.com/s/1xt7P9YFjoHmPCzQHJV65Xg&shfl=sharepset
提取碼:1zdt
廢話不多說,直接上代碼:
/** * * @param originalFile doc文件 * @param toFilePath 文件夾路徑 * @param fileName 文件名 * @param type 文件類型 * @return * @throws Exception */ public String file2pdf(File originalFile, String toFilePath,String fileName,String type,String contractBillcode) throws Exception { String htmFileName; //獲取轉換成PDF之后文件名 if(".doc".equals(type)){ htmFileName = fileName+".pdf"; }else if(".docx".equals(type)){ htmFileName = fileName+".pdf"; }else{ return null; } //通過轉換之后的PDF文件名,創建PDF文件 File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName); //獲取文件輸出流 FileOutputStream os = new FileOutputStream(htmlOutputFile); //獲取Doc文檔對象模型 Document doc = new Document(toFilePath+ File.separatorChar + fileName+type); //為doc文檔添加水印 insertWatermarkText(doc, "Smile、斌"); //將doc文旦轉換成PDF文件並輸出到之前創建好的pdf文件中 doc.save(os, SaveFormat.PDF); //關閉輸出流 if(os!=null){ os.close(); } return htmFileName; }
以上就將一個word文檔轉換成了PDF文件,接下來就是添加水印部分了
廢話不多說,上代碼
/** * 為word文檔添加水印 * @param doc word文檔模型 * @param watermarkText 需要添加的水印字段 * @throws Exception */ private static void insertWatermarkText(Document doc, String watermarkText) throws Exception { 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("Watermark Set"); }
/** * 在頁眉中插入水印 * @param watermarkPara * @param sect * @param headerType * @throws Exception */ 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)); }
效果圖如下,我添加的水印為我的博客昵稱:Smile、斌

希望本文所述對大家java程序設計有所幫助。
