《Java知識應用》Java下Linux系統下word轉PDF


僅供學習,如需商用請聯系開發商:https://apireference.aspose.com/java/words

Jar包下載路徑:

鏈接: https://pan.baidu.com/s/1s79xMoTZ33sySXBmpoXFIg 提取碼: nnmr 復制這段內容后打開百度網盤手機App,操作更方便哦

案例:

license.xml 文件內容

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
  </Data>
  <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>
</License>

代碼如下:

import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.*;

public class Doc2Pdf {

    /*****
     * 需要引入jar包:aspose-words-15.8.0-jdk16.jar
     * @param args
     */
    public static void main(String[] args) {
        doc2pdf("src/demo/knowledgepoints/DocToPdf/PDF/2019年全國I卷理科數學高考真題.docx","src/demo/knowledgepoints/DocToPdf/PDF/pdf1.pdf");
    }

    public static boolean getLicense() {
        boolean result = false;
        try {
            File file = new File("src/demo/knowledgepoints/DocToPdf/imp/license.xml"); // 新建一個空白pdf文檔
            InputStream is = new FileInputStream(file); // license.xml找個路徑放即可。
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void doc2pdf(String inPath, String outPath) {
        if (!getLicense()) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一個空白pdf文檔
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是將要被轉化的word文檔
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互轉換
            long now = System.currentTimeMillis();
            System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果展示:

轉化后:


免責聲明!

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



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