{"id":226085,"date":"2024-05-11T20:18:10","date_gmt":"2024-05-11T12:18:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/226085.html"},"modified":"2024-05-11T20:18:16","modified_gmt":"2024-05-11T12:18:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8java%e5%ae%9e%e7%8e%b0%e6%95%b0%e5%ad%a6%e5%85%ac%e5%bc%8f%e7%9a%84%e7%bc%96%e8%be%91%e5%b9%b6%e7%94%9f%e6%88%90word%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/226085.html","title":{"rendered":"\u5982\u4f55\u7528Java\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210word\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25145518\/69e65ff9-421a-428b-b6ee-35df4ba45b18.webp\" alt=\"\u5982\u4f55\u7528Java\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210word\u6587\u6863\" \/><\/p>\n<p><p>\u5728Java\u4e2d\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210Word\u6587\u6863\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Apache POI\u5e93\u3001JMathTex\uff08\u7528\u4e8e\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\uff09\u4ee5\u53caDocx4j\u8fd9\u6837\u7684\u5e93\u3002<strong>Apache POI<\/strong> \u5141\u8bb8\u64cd\u4f5cMicrosoft Word\u6587\u6863\uff0c<strong>JMathTex<\/strong> \u53ef\u751f\u6210\u516c\u5f0f\u7684\u56fe\u50cf\uff0c\u800c <strong>Docx4j<\/strong> \u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u6587\u6863\u64cd\u4f5c\u7279\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\u5b9e\u73b0\u5728Java\u4e2d\u7684\u6570\u5b66\u516c\u5f0f\u7f16\u8f91\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230Word\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u6240\u9700\u7684\u5e93\u5728\u9879\u76ee\u4e2d\u5df2\u5f15\u5165\u3002\u53ef\u4ee5\u901a\u8fc7Maven\u6216Gradle\u7b49\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\u6dfb\u52a0Apache POI\u548cDocx4j\u7684\u4f9d\u8d56\u3002JMathTex\u53ef\u80fd\u9700\u8981\u624b\u52a8\u4e0b\u8f7djar\u5305\u5e76\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u751f\u6210\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>JMathTex\u53ef\u4ee5\u7528\u4e8e\u5c06LaTeX\u5f62\u5f0f\u7684\u6570\u5b66\u516c\u5f0f\u8f6c\u6362\u4e3a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">\/\/ \u4f7f\u7528JMathTex\u5c06LaTeX\u516c\u5f0f\u751f\u6210\u4e3a\u5bf9\u5e94\u7684\u56fe\u50cf\u6587\u4ef6<\/p>\n<p>public BufferedImage createFormul<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>mage(String latexFormula) throws MathTeXException {<\/p>\n<p>    TeXFormula formula = new TeXFormula(latexFormula);<\/p>\n<p>    TeXIcon icon = formula.createTeXIcon(TeXConstants.STYLE_DISPLAY, 20);<\/p>\n<p>    BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);<\/p>\n<p>    Graphics2D graphics2D = image.createGraphics();<\/p>\n<p>    icon.paintIcon(null, graphics2D, 0, 0);<\/p>\n<p>    return image;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u65b9\u6cd5\u63a5\u6536LaTeX\u5b57\u7b26\u4e32\uff0c\u5e76\u5229\u7528JMathTex\u751f\u6210\u516c\u5f0f\u56fe\u50cf\uff0c\u53ef\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Apache POI\u6dfb\u52a0\u516c\u5f0f\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u83b7\u5f97\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u50cf\u540e\uff0c\u63a5\u4e0b\u6765\u4f7f\u7528Apache POI\u5e93\u5c06\u56fe\u50cf\u63d2\u5165\u5230Word\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">\/\/ \u4f7f\u7528Apache POI\u5728Word\u4e2d\u63d2\u5165\u6570\u5b66\u516c\u5f0f\u56fe\u50cf<\/p>\n<p>public void insertFormulaIntoWord(String imagePath, String outputDocPath) throws IOException, InvalidFormatException {<\/p>\n<p>    XWPFDocument document = new XWPFDocument();<\/p>\n<p>    FileInputStream is = new FileInputStream(imagePath);<\/p>\n<p>    XWPFParagraph paragraph = document.createParagraph();<\/p>\n<p>    XWPFRun run = paragraph.createRun();<\/p>\n<p>    run.addPicture(is, Document.PICTURE_TYPE_PNG, imagePath, Units.toEMU(200), Units.toEMU(200));<\/p>\n<p>    is.close();<\/p>\n<p>    FileOutputStream os = new FileOutputStream(outputDocPath);<\/p>\n<p>    document.write(os);<\/p>\n<p>    os.close();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\uff0c\u9996\u5148\u521b\u5efa\u4e86XWPFDocument\u5bf9\u8c61\uff0c\u968f\u540e\u521b\u5efa\u4e86\u5305\u542b\u516c\u5f0f\u56fe\u50cf\u7684\u6bb5\u843d\uff0c\u5e76\u6700\u7ec8\u5c06\u5176\u5199\u5165\u5230\u6307\u5b9a\u8def\u5f84\u7684Word\u6587\u6863\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Docx4j\u8fdb\u4e00\u6b65\u64cd\u4f5cWord\u6587\u6863<\/h3>\n<\/p>\n<p><p>Docx4j\u63d0\u4f9b\u4e86\u6bd4Apache POI\u66f4\u6df1\u5165\u7684\u6587\u6863\u64cd\u4f5c\u3002\u5982\u9700\u8981\u66f4\u591a\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff08\u6bd4\u5982\u590d\u6742\u7684\u683c\u5f0f\u8bbe\u7f6e\u3001\u7ed3\u6784\u6027\u7f16\u8f91\u7b49\uff09\uff0cDocx4j\u4f1a\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">\/\/ \u4f7f\u7528Docx4j\u8fdb\u4e00\u6b65\u64cd\u4f5cWord\u6587\u6863\uff0c\u63d2\u5165\u6570\u5b66\u516c\u5f0f<\/p>\n<p>public void insertFormulaWithDocx4j(String imagePath, String outputDocPath) throws Exception {<\/p>\n<p>    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();<\/p>\n<p>    File file = new File(imagePath);<\/p>\n<p>    BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, file);<\/p>\n<p>    Inline inline = imagePart.createImageInline(null, null, 0, 1, false, 500);<\/p>\n<p>    P paragraph = wordMLPackage.getMainDocumentPart().createParagraphOfText(&quot;&quot;);<\/p>\n<p>    R run = factory.createR();<\/p>\n<p>    paragraph.getContent().add(run);<\/p>\n<p>    Drawing drawing = factory.createDrawing();<\/p>\n<p>    run.getContent().add(drawing);<\/p>\n<p>    drawing.getAnchorOrInline().add(inline);<\/p>\n<p>    wordMLPackage.getMainDocumentPart().addObject(paragraph);<\/p>\n<p>    wordMLPackage.save(new File(outputDocPath));<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684WordprocessingMLPackage\u5b9e\u4f8b\uff0c\u7136\u540e\u6839\u636e\u516c\u5f0f\u56fe\u50cf\u6587\u4ef6\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u50cf\u90e8\u5206\uff08BinaryPartAbstractImage\uff09\uff0c\u6700\u540e\u5c06\u56fe\u50cf\u4f5c\u4e3a\u5185\u8054\u5143\u7d20\uff08Inline\uff09\u6dfb\u52a0\u5230\u6bb5\u843d\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u7ed3\u5408\u524d\u9762\u7684\u6b65\u9aa4\uff0c\u5b9e\u73b0\u4eceLaTeX\u7f16\u8f91\u516c\u5f0f\u5230\u751f\u6210\u5305\u542b\u516c\u5f0f\u7684Word\u6587\u6863\u7684\u6574\u4e2a\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class MathFormulaToWord {<\/p>\n<p>    \/\/ JMathTex\u751f\u6210\u516c\u5f0f\u56fe\u50cf<\/p>\n<p>    public static BufferedImage createFormulaImage(String latexFormula) throws Exception {<\/p>\n<p>        \/\/...\uff08\u6b64\u5904\u7701\u7565JMathTex\u751f\u6210\u56fe\u50cf\u7684\u4ee3\u7801\uff09<\/p>\n<p>    }<\/p>\n<p>    \/\/ \u4f7f\u7528Apache POI\u6216Docx4j\u63d2\u5165\u516c\u5f0f\u56fe\u50cf\u5230Word\u6587\u4ef6<\/p>\n<p>    public static void insertFormulaIntoWord(BufferedImage image, String outputDocPath) throws Exception {<\/p>\n<p>        \/\/...\uff08\u6b64\u5904\u7701\u7565Apache POI\u6216Docx4j\u64cd\u4f5cWord\u6587\u6863\u7684\u4ee3\u7801\uff09<\/p>\n<p>    }<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        try {<\/p>\n<p>            \/\/ LaTeX\u516c\u5f0f<\/p>\n<p>            String latexFormula = &quot;\\\\sum_{i=1}^{n} i = \\\\frac{n(n+1)}{2}&quot;;<\/p>\n<p>            \/\/ \u751f\u6210\u516c\u5f0f\u56fe\u50cf<\/p>\n<p>            BufferedImage image = createFormulaImage(latexFormula);<\/p>\n<p>            \/\/ \u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u4e34\u65f6\u6587\u4ef6<\/p>\n<p>            File tempImage = File.createTempFile(&quot;formula&quot;, &quot;.png&quot;);<\/p>\n<p>            ImageIO.write(image, &quot;png&quot;, tempImage);<\/p>\n<p>            \/\/ \u63d2\u5165\u516c\u5f0f\u56fe\u50cf\u5230Word\u6587\u6863<\/p>\n<p>            String outputDocPath = &quot;output.docx&quot;;<\/p>\n<p>            insertFormulaIntoWord(image, outputDocPath);<\/p>\n<p>            \/\/ \u5220\u9664\u4e34\u65f6\u6587\u4ef6<\/p>\n<p>            tempImage.delete();<\/p>\n<p>            System.out.println(&quot;\u6570\u5b66\u516c\u5f0f\u5df2\u7ecf\u88ab\u6210\u529f\u6dfb\u52a0\u81f3Word\u6587\u6863&quot;);<\/p>\n<p>        } catch (Exception e) {<\/p>\n<p>            e.printStackTrace();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u7efc\u5408\u793a\u4f8b\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u6d41\u7a0b\uff1a\u4eceLaTeX\u516c\u5f0f\u7f16\u8f91\u5230\u6700\u7ec8\u751f\u6210Word\u6587\u6863\u3002\u5b8c\u6210\u8fd9\u4e9b\u6b65\u9aa4\u540e\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6240\u9700\u6570\u5b66\u516c\u5f0f\u7684Word\u6587\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u9519\u8bef\u5904\u7406\u548c\u8fb9\u754c\u60c5\u51b5\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Java\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Java\u7f16\u5199\u6570\u5b66\u516c\u5f0f\u7f16\u8f91\u7684\u4ee3\u7801\u53ef\u4ee5\u4f9d\u8d56\u4e00\u4e9b\u5f00\u6e90\u5e93\uff0c\u5982MathML\u6216LaTeX\u89e3\u6790\u5668\u3002\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u6570\u5b66\u516c\u5f0f\u8f6c\u6362\u6210\u53ef\u7f16\u8f91\u7684\u683c\u5f0f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u6570\u5b66\u516c\u5f0f\u7684\u8bed\u6cd5\u548c\u89c4\u5219\uff0c\u5e76\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u6765\u89e3\u6790\u5b83\u4eec\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199Java\u4ee3\u7801\u6765\u89e3\u6790\u7528\u6237\u8f93\u5165\u7684\u6570\u5b66\u516c\u5f0f\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u6210\u9002\u5408\u7f16\u8f91\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528Java\u751f\u6210Word\u6587\u6863\u5e76\u5c06\u6570\u5b66\u516c\u5f0f\u63d2\u5165\u5176\u4e2d\uff1f<\/strong><\/p>\n<p>\u8981\u5c06\u5904\u7406\u8fc7\u7684\u6570\u5b66\u516c\u5f0f\u63d2\u5165Word\u6587\u6863\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Java\u4e2d\u7684Apache POI\u5e93\u6765\u64cd\u4f5cWord\u6587\u4ef6\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Apache POI\u5e93\u7684\u76f8\u5173\u4f9d\u8d56\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff0c\u5e76\u5728\u5176\u4e2d\u63d2\u5165\u6587\u672c\u548c\u6570\u5b66\u516c\u5f0f\u3002\u4e3a\u4e86\u63d2\u5165\u6570\u5b66\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528MathML\u6216LaTeX\u8bed\u6cd5\uff0c\u5e76\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u5c06\u5176\u8f6c\u6362\u4e3aWord\u6587\u6863\u53ef\u4ee5\u8bc6\u522b\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528Java\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210\u53ef\u7f16\u8f91\u7684Word\u6587\u6863\uff1f<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210\u53ef\u7f16\u8f91\u7684Word\u6587\u6863\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u89e3\u6790\u5e93\u548cWord\u6587\u6863\u7f16\u8f91\u5e93\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u89e3\u6790\u5e93\u5c06\u6570\u5b66\u516c\u5f0f\u8f6c\u6362\u4e3a\u53ef\u7f16\u8f91\u7684\u683c\u5f0f\u3002\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Word\u6587\u6863\u7f16\u8f91\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863\uff0c\u5e76\u5c06\u6570\u5b66\u516c\u5f0f\u63d2\u5165\u5176\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u751f\u6210\u7684\u6587\u6863\u4fdd\u5b58\u4e3a\u53ef\u7f16\u8f91\u7684Word\u6587\u6863\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u7ee7\u7eed\u7f16\u8f91\u6570\u5b66\u516c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u4e2d\u5b9e\u73b0\u6570\u5b66\u516c\u5f0f\u7684\u7f16\u8f91\u5e76\u751f\u6210Word\u6587\u6863\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Apache POI\u5e93\u3001JMathTex\uff08\u7528\u4e8e\u6570 [&hellip;]","protected":false},"author":3,"featured_media":226090,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/226085"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=226085"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/226085\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/226090"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=226085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=226085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=226085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}