用java實現word轉pdf


 

摘要:如何用java實現word文檔轉pdf呢

最近在網上看了很多資料,遇到了很多頭疼的問題,看了各類大神寫的方法,最初想要研究的是在線預覽word

現在來看,不太現實,除了微軟研究的一套在線預覽的url,但是滿足的條件有點扯淡,有興趣的可以去查資料

還有就是各類各樣的收費平台,來看看這種轉換的處理方法吧

  • 實現功能
    • docx文檔轉換為PDF
    • 轉換之后排版不混亂
  • 使用工具(Jar包)
    • aspose-words-15.11.0.jar(用於PDF轉換 )
  • 環境
    • JDK1.8
  • 代碼
 
    public static void main(String[] args) {
        doc2pdf("/Users/lzl/Desktop/a.docx","/Users/lzl/Desktop/test.pdf");
    }
    public static void doc2pdf(String inPath, String outPath) {
        FileOutputStream os =null;
        try {
            File file = new File(outPath); // 新建一個空白pdf文檔
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是將要被轉化的word文檔
            //insertWatermarkText(doc, "四葉草的詩雨");
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

  加水印(升級版)

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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM