1. 程式人生 > >利用Aspose.Words處理Word文檔之間的轉換和內容操作

利用Aspose.Words處理Word文檔之間的轉換和內容操作

tcl class image web swf 應用程序 rap 內容操作 ges

一、概述:Aspose.Words是一個商業.NET類庫,可以使得應用程序處理大量的文件任務
Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情況下生成、修改、轉換和打印文檔。
二、功能簡介:
1、Aspose.Words具有高質量的文件格式轉換功能,可以和Doc,OOXL,RTF,TXT等格式互相轉換。
2、通過豐富的API以編程方式訪問所有的文檔元素和格式,允許創建,修改,提取,復制,分割,加入,和替換文件內容。
3、可以在服務器端轉換整個文檔或者頁面為PDF,XPS,SWF格式,同樣可以轉換文檔頁面為圖像格式,或者.NET Graphics對象,這些功能和Microsoft.Word是一樣的。
4、Aspose.Words可以在沒有安裝Microsoft Office的機器上工作。
三、實例演示:

        public static void word2pdf(String Address) {
                getLicense();
                try {
                    File file = new File("D:\\red_ant_file\\20180919\\20180919.pdf"); // 新建一個空白pdf文檔
                    FileOutputStream os = new FileOutputStream(file);
                    com.aspose.words.Document doc = new com.aspose.words.Document(Address); // Address是將要被轉化的word文檔
                    for (Section sect : doc.getSections()) {  
                        removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_PRIMARY);  
                        removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_FIRST);  
                        removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_EVEN);  
                    } 
                    doc.save(os, SaveFormat.PDF); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換
                    os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        /**word轉pdf
             */
            public static boolean getLicense() {
                boolean result = false;
                try {
                    // wordlicense.xml應放在..\WebContent\WEB-INF\classes路徑下
                    InputStream is = Test.class.getClassLoader().getResourceAsStream("wordlicense.xml"); 
                    com.aspose.words.License aposeLic = new com.aspose.words.License();
                    aposeLic.setLicense(is);
                    result = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }

調用:

    String aposeword_Source_path = "D:\\red_ant_file\\20180919\\Extjs4.0_教程.doc";
    AllServiceIsHere.word2pdf(aposeword_Source_path);

技術分享圖片

運行之後:

技術分享圖片

利用Aspose.Words處理Word文檔之間的轉換和內容操作