国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

如何使用java實(shí)現word文檔轉pdf并添加水印

發(fā)布時(shí)間:2021-09-27 17:50 來(lái)源:億速云 閱讀:0 作者:小新 欄目: 開(kāi)發(fā)技術(shù)

這篇文章主要介紹了如何使用java實(shí)現word文檔轉pdf并添加水印,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。

具體如下:

前段時(shí)間,項目需要自動(dòng)生成word文檔,用WordFreeMarker生成word文檔后,又要求生成的文檔能在瀏覽器瀏覽,思來(lái)想去,把word文檔轉成pdf就好了,于是乎研究了一下。

將word文檔轉化為PDF是項目中常見(jiàn)的需求之一,目前主流的方法可以分為兩大類(lèi),一類(lèi)是利用各種Office應用進(jìn)行轉換,譬如Microsoft Office、WPS以及LiberOffice,另一種是利用各種語(yǔ)言提供的對于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。需要到的兩個(gè)jar包(所需jar包下載。)。

上代碼:

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import org.slf4j.Logger;import com.aspose.words.Document;import com.aspose.words.SaveFormat;/** * * @ClassName: Word2PdfUtilt* @Description: PDF轉換工具類(lèi)* @author mzl* @date 2018年7月30日 下午5:26:44* */public class Word2PdfUtilt {    protected static Logger log = Log.get();    public static void main(String[] args) {    doc2pdf("D:\\Workspaces\\小花測試_20180731094521.doc","D:\\Workspaces\\test.pdf");  }  public static void doc2pdf(String inPath, String outPath) {      FileOutputStream os =null;    try {      File file = new File(outPath); // 新建一個(gè)空白pdf文檔      os = new FileOutputStream(file);      Document doc = new Document(inPath); // Address是將要被轉化的word文檔      doc.save(os, SaveFormat.PDF);    } catch (Exception e) {      e.printStackTrace();    }finally{        if(os!=null){            try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }        }    }  }}

轉成PDF格式后,圖片等樣式均沒(méi)有亂。

以上就是生產(chǎn)PDF的步驟了,接下來(lái)添加水?。?/p>

/*** * @Title: insertWatermarkText* @Description: PDF生成水印* @author mzl* @param doc* @param watermarkText* @throws Exception* @throws*/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");}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));}

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

亚洲性线免费观看视频成熟| 18禁极品美女裸露网站| 综合无码一区二区三区| 永久免费无码国产| 亚洲AV无码久久忘忧草| FREECHINESE内射少妇|