瀏覽代碼

get toc level

wcf 4 年之前
父節點
當前提交
a43402d857
共有 1 個文件被更改,包括 50 次插入29 次删除
  1. 50 29
      src/main/java/com/zglc/fm/utils/DocToPdf.java

+ 50 - 29
src/main/java/com/zglc/fm/utils/DocToPdf.java

@@ -2,6 +2,7 @@ package com.zglc.fm.utils;
 
 import com.aspose.words.*;
 import com.zglc.fm.entity.CatalogueEntity;
+import sun.applet.AppletAudioClip;
 
 import java.io.File;
 import java.io.InputStream;
@@ -12,46 +13,66 @@ import java.util.List;
 import java.util.Map;
 
 public class DocToPdf {
-//    public static void main(String[] args) throws Exception {
-//        loadLicense();
-//        //convertDoc2Pdf("/Users/wcf/Desktop/123.wps","/Users/wcf/Desktop");
-//        GetDoccatalogue("d:\\test.docx");
-//    }
+    public static void main(String[] args) throws Exception {
+        loadLicense();
+        //convertDoc2Pdf("/Users/wcf/Desktop/123.wps","/Users/wcf/Desktop");
+        GetDoccatalogue("d:\\test.docx",0);
+    }
 
     public static List<CatalogueEntity> GetDoccatalogue(String filePath,Integer bookId) throws Exception {
         List<CatalogueEntity> lst = new ArrayList<>();
-        Document document = new Document(filePath);
+        Document doc = new Document(filePath);
+        //Paragraph paragraph = (Paragraph) document.getChild(NodeType.PARAGRAPH, 1, true);
+        /*
+        for (Field field:  doc.getRange().getFields())
+        {
+            if (field.getType() == FieldType.FIELD_HYPERLINK)
+            {
+                FieldHyperlink hyperlink = (FieldHyperlink)field;
+                if (hyperlink.getSubAddress() != null && hyperlink.getSubAddress().startsWith("_Toc"))
+                {
+                    Paragraph tocItem = (Paragraph)field.getStart().getAncestor(NodeType.PARAGRAPH);
+                    System.out.println(tocItem.toString(SaveFormat.TEXT).trim());
+                    if (tocItem != null)
+                    {
+                        Bookmark bm = doc.getRange().getBookmarks().get(hyperlink.getSubAddress());
+                        // Get the location this TOC Item is pointing to
+                        Paragraph pointer = (Paragraph)bm.getBookmarkStart().getAncestor(NodeType.PARAGRAPH);
+                    }
+
+                }
+            }
+        }
+        */
         String tmp,txt,lastTmp;
         String title = "";
         tmp = "";
         lastTmp = "";
         int index ;
-        for(Object object: (Iterable)document.getChildNodes(NodeType.FIELD_START, true)) {
+        for(Object object: (Iterable)doc.getChildNodes(NodeType.FIELD_START, true)) {
             FieldStart field = (FieldStart )object;
             Paragraph tocItem = (Paragraph) field.getAncestor(NodeType.PARAGRAPH);
             if (tocItem != null) {
-                txt = tocItem.getText();
-                index = txt.indexOf("_Toc");
-                if (index <=0) continue;
-                tmp = txt.substring(index,txt.indexOf(" ",index));
-                if (tmp .compareTo( lastTmp)==0) continue;
-                lastTmp = tmp;
-                index = txt.indexOf("HYPERLINK");
-                index = txt.indexOf("\u0014",index);
-                title = txt.substring(index + 1,txt.indexOf("\t",index));
-                index = txt.indexOf("PAGEREF");
-                index = txt.indexOf("\u0014",index);
-                tmp = txt.substring(index + 1,txt.indexOf("\u0015",index));
-                CatalogueEntity catalogueEntity = new CatalogueEntity();
-                catalogueEntity.setBook_id(bookId);
-                catalogueEntity.setContents(title);
-                catalogueEntity.setPage_num(Integer.parseInt(tmp));
-                lst.add(catalogueEntity);
-                System.out.println(title + "  :" + tmp);
-               /*for (Run run : tocItem.getRuns()) {
-                    System.out.println(run.getText());
-                }*/
-
+                if (tocItem.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.TOC_1) {
+                    txt = tocItem.getText();
+                    index = txt.indexOf("_Toc");
+                    if (index <= 0) continue;
+                    tmp = txt.substring(index, txt.indexOf(" ", index));
+                    if (tmp.compareTo(lastTmp) == 0) continue;
+                    lastTmp = tmp;
+                    index = txt.indexOf("HYPERLINK");
+                    index = txt.indexOf("\u0014", index);
+                    title = txt.substring(index + 1, txt.indexOf("\t", index));
+                    index = txt.indexOf("PAGEREF");
+                    index = txt.indexOf("\u0014", index);
+                    tmp = txt.substring(index + 1, txt.indexOf("\u0015", index));
+                    CatalogueEntity catalogueEntity = new CatalogueEntity();
+                    catalogueEntity.setBook_id(bookId);
+                    catalogueEntity.setContents(title);
+                    catalogueEntity.setPage_num(Integer.parseInt(tmp));
+                    lst.add(catalogueEntity);
+                    System.out.println(title + "  :" + tmp);
+                }
             }
         }
         return lst;