{"id":4151,"date":"2023-02-23T14:51:23","date_gmt":"2023-02-23T06:51:23","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4151"},"modified":"2023-02-23T14:51:23","modified_gmt":"2023-02-23T06:51:23","slug":"java-doc%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4151","title":{"rendered":"Java doc\u5904\u7406"},"content":{"rendered":"\n<p>\u633a\u597d\u7528\u7684\uff0c\u5907\u5fd8<\/p>\n\n\n\n<p><a href=\"https:\/\/www.cnblogs.com\/Yesi\/archive\/2021\/04\/01\/14605280.html\">Java \u8bfb\u53d6Word\u6587\u672c\/\u6bb5\u843d\u683c\u5f0f\u5c5e\u6027 &#8211; E-iceblue &#8211; \u535a\u5ba2\u56ed (cnblogs.com)<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import com.spire.doc.*;\nimport com.spire.doc.documents.Paragraph;\nimport com.spire.doc.documents.TextSelection;\nimport com.spire.doc.fields.TextRange;\n\nimport java.awt.*;\n\npublic class GetTextFormat {\n    public static void main(String[] args) {\n        \/\/\u52a0\u8f7dWord\u6e90\u6587\u6863\n        Document doc = new Document();\n        doc.loadFromFile(\"test.docx\");\n\n        \/\/\u83b7\u53d6\u6bb5\u843d\u6570\u91cf\n        int count = doc.getSections().get(0).getParagraphs().getCount();\n        System.out.println(\"\u603b\u5171\u542b\u6709\u6bb5\u843d\u6570:\" + count);\n\n        \/\/\u67e5\u627e\u6307\u5b9a\u6587\u672c\n        TextSelection textSelections = doc.findString(\"\u4e1c\u91ce\u572d\u543e\", false, true);\n        \/\/\u83b7\u53d6\u5b57\u4f53\u540d\u79f0\n        String fontname = textSelections.getAsOneRange().getCharacterFormat().getFontName();\n        \/\/\u83b7\u53d6\u5b57\u4f53\u5927\u5c0f\n        float fontsize = textSelections.getAsOneRange().getCharacterFormat().getFontSize();\n        System.out.println(\"\u5b57\u4f53\u540d\u79f0:\" + fontname +\"\\n\"\n            +\"\u5b57\u4f53\u5927\u5c0f\uff1a\"+fontsize);\n\n\n        \/\/\u83b7\u53d6\u7b2c\u4e8c\u6bb5\n        Paragraph paragraph2 = doc.getSections().get(0).getParagraphs().get(1);\n        \/\/\u83b7\u53d6\u6bb5\u843d\u884c\u8ddd\n        float linespage = paragraph2.getFormat().getLineSpacing();\n        System.out.println(\"\u6bb5\u843d\u884c\u8ddd\uff1a\" + linespage);\n\n        \/\/\u904d\u5386\u6bb5\u843d\u4e2d\u7684\u5b50\u5bf9\u8c61\n        for (int z = 0; z &lt; paragraph2.getChildObjects().getCount(); z++)\n        {\n            Object obj2 = paragraph2.getChildObjects().get(z);\n\n            \/\/\u5224\u5b9a\u662f\u5426\u4e3a\u6587\u672c\n            if (obj2 instanceof TextRange)\n            {\n                TextRange textRange2 = (TextRange) obj2;\n\n                \/\/\u83b7\u53d6\u6587\u672c\u989c\u8272\n                Color textcolor = textRange2.getCharacterFormat().getTextColor();\n                if (!(textcolor.getRGB() == 0))\n                {\n                    System.out.println(\"\u6587\u672c\u989c\u8272\uff1a\" + textRange2.getText() + textcolor.toString());\n                }\n\n                \/\/\u83b7\u53d6\u5b57\u4f53\u52a0\u7c97\u6548\u679c\n                boolean isbold = textRange2.getCharacterFormat().getBold();\n                if (isbold == true)\n                {\n                    System.out.println(\"\u52a0\u7c97\u6587\u672c\uff1a\" + textRange2.getText());\n                }\n\n                \/\/\u83b7\u53d6\u5b57\u4f53\u503e\u659c\u6548\u679c\n                boolean isitalic = textRange2.getCharacterFormat().getItalic();\n                if (isitalic == true)\n                {\n                    System.out.println(\"\u503e\u659c\u6587\u672c\uff1a\" + textRange2.getText());\n                }\n\n                \/\/\u83b7\u53d6\u6587\u672c\u80cc\u666f\n                String text = textRange2.getText();\n                Color highlightcolor = textRange2.getCharacterFormat().getHighlightColor();\/\/\u83b7\u53d6\u6587\u672c\u7684\u9ad8\u4eae\u989c\u8272\uff08\u5373\u7a81\u51fa\u663e\u793a\u989c\u8272\uff09\n                if (!(highlightcolor.getRGB() == 0 ))\n                {\n                    System.out.println(\"\u6587\u672c\u9ad8\u4eae\uff1a\" + text + highlightcolor.toString());\/\/\u8f93\u51fa\u9ad8\u4eae\u7684\u6587\u672c\u548c\u989c\u8272\n                }\n\n                Color textbackgroundcolor = textRange2.getCharacterFormat().getTextBackgroundColor();\/\/\u83b7\u53d6\u6587\u5b57\u80cc\u666f\uff08\u5e95\u7eb9\uff09\n                if (!(textbackgroundcolor.getRGB()==0))\n                {\n                    System.out.println(\"\u6587\u672c\u80cc\u666f\uff1a\" + text + textbackgroundcolor.toString());\/\/\u8f93\u51fa\u6709\u80cc\u666f\u7684\u6587\u672c\u548c\u989c\u8272\n                }\n\n            }\n        }\n\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u633a\u597d\u7528\u7684\uff0c\u5907\u5fd8 Java \u8bfb\u53d6Word\u6587\u672c\/\u6bb5\u843d\u683c\u5f0f\u5c5e\u6027 &#8211; E-iceblue &#8211;  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[274],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4151"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4151"}],"version-history":[{"count":5,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions"}],"predecessor-version":[{"id":4156,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4151\/revisions\/4156"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}