【问题标题】:How to read text and font from .doc using Aspose Word for Java如何使用 Aspose Word for Java 从 .doc 读取文本和字体
【发布时间】:2017-07-29 17:00:01
【问题描述】:

我有一个读取和写入 .doc 文件的任务,并且必须能够读取 字体设置 表示每个单词。我目前在我的开发中使用Aspose word for Java,写入 word 并包括每个单词的字体设置正在运行。唯一的问题是有时当我尝试选择一个 .doc 文件并使用下面的代码读取它时,它不会从 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 无法处理这些处理?请帮忙,感谢您的宝贵时间。

【问题讨论】:

  • 我不熟悉这个框架,但如果你的意思是 设置 像字体大小、系列、粗体/斜体等,那么我看不到这些部分在您的代码中,据我所知,您只是阅读了文本。

标签: java ms-word ms-office aspose aspose.words


【解决方案1】:

您可以使用Aspose.Words for Java API 获取每个 Run in Document 的文本和字体名称,使用以下代码:

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 合作,担任开发人员宣传员。

【讨论】:

  • 如果是字体间距,我也可以得到吗?
  • 您可以使用 Font.Spacing 属性来检索或设置字符之间的间距(以磅为单位)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多