Reputation: 49
I have an assignment to read and write to a .doc file and must be able to read the font setting for each word. I'm currently using Aspose
word for Java in my development, the write to word and including the font setting to each word is running. The only problem is sometimes when i try to pick a .doc
file and read it using the code below, it returns nothing from the System.out.print
. But also sometimes it came but with only few words rather than the whole content.
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);
}
}
Am i heading toward a right direction by using this code for reading each word and each word font setting? Or maybe Aspose can't handle these kind of processings? Please help, thank you for your time.
Upvotes: 0
Views: 1952
Reputation: 1090
You can use Aspose.Words for Java API to get text and Font name of each Run in Document by using the following code:
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());
}
I work with Aspose as Developer Evangelist.
Upvotes: 3