如何使用 Aspose Word for Java 从 .doc 中读取文本和字体

How to read text and font from .doc using Aspose Word for Java

我有一个任务要读取和写入 .doc 文件并且必须能够读取 每个字的字体设置。我目前在我的开发中使用 Aspose 字作为 Java,写入字并包括每个字的字体设置是 运行。唯一的问题是有时当我尝试选择一个 .doc 文件并使用下面的代码读取它时,它 returns 与 System.out.print。但也有的时候它来了,但只有几个字,而不是全部内容。

final JFileChooser fc = new JFileChooser();
            HomeForm form = new HomeForm();
             if (evt.getSource() == jButton2)
             {
                int returnVal = fc.showOpenDialog(HomeForm.this);
                File file = fc.getSelectedFile();
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                        JOptionPane.showMessageDialog(null, "File " +file.getName()+" choosed", "Alert", JOptionPane.CLOSED_OPTION);
                        jTextField1.setText(file.getName());

                        String dataDir = file.getPath();
                        String filename = file.getName();

                    try {
                        InputStream in = new FileInputStream(dataDir);
                        Document doc = new Document(in);
                        System.out.println(file.getName());;
                        System.out.println(doc.getText());
                        in.close();FileInputStream(file.getAbsolutePath());Logger.getLogger(HomeForm.class.getName()).log(Level.SEVERE, null, ex);InputStreamReader(fis, Charset.forName("UTF-8"));
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(HomeForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (Exception ex) {
                        Logger.getLogger(HomeForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    } else {
                        JOptionPane.showMessageDialog(null, "File choose canceled", "Alert", JOptionPane.CLOSED_OPTION);
                    }
                 }

使用此代码阅读每个单词和每个单词字体设置,我是否朝着正确的方向前进?或者也许 Aspose 无法处理此类处理?请帮忙,谢谢你的时间。

您可以使用 Aspose.Words for Java API 获取文档中每个 运行 的文本和字体名称,方法是使用以下代码:

Document doc = new Document("D:\temp\in.doc");

for(Run run : (Iterable) doc.getChildNodes(NodeType.RUN, true)) {
    System.out.println(run.getText());
    System.out.println(run.getFont().getName());
}

我在 Aspose 工作,担任开发人员推广员。