天天看點

【java】使用Aspose word将word轉為pdf,并自定義水印

1、依賴jar包 及 license.xml

  • 連結:https://pan.baidu.com/s/1xvxXA6Wr_HKdvTis8z1FKw
  • 提取碼:790o

2、代碼實作:

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));
    }
           

3、注:如果提示 javaSoft注冊權限問題

打開系統資料庫(regedit),找到HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft,右鍵改權限為完全許可

轉載于:https://my.oschina.net/zhang2xiang/blog/3089355