// jedem Fall (auch bei file-Dokumenten) anstatt der
// Originaldatei eine tempor�re Kopie genutzt, da diese
// unm�glich von jemandem bearbeitet werden kann.
String fileName = rawDocument.getContentAsFile(true).getAbsolutePath();
Documents docs = mWordApplication.getDocuments();
Document doc = docs.open(new Variant(fileName),
new Variant(false), // confirmConversions
new Variant(true)); // readOnly
// iterate through the sections
StringBuffer content = new StringBuffer(DEFAULT_BUFFER_SIZE);
Sections sections = doc.getSections();
for (int i = 1; i <= sections.getCount(); i++) {
Section sec = sections.item(i);
// Get the header of the first section as title
if (i == 1) {
int headerFirstPage = WdHeaderFooterIndex.wdHeaderFooterFirstPage;
HeaderFooter firstHeader = sec.getHeaders().item(headerFirstPage);
String title = firstHeader.getRange().getText();
setTitle(title);
}
// Get the text
sec.getRange().select();
content.append(getSelection(mWordApplication) + "\n");
}
// iterate through the shapes
Shapes shapes = doc.getShapes();
for (int i = 1; i <= shapes.getCount(); i++) {
Shape shape = shapes.item(new Variant(i));
appendShape(shape, content);
}
// iterate through the paragraphs and extract the headlines
StringBuffer headlines = null;
if ((mHeadlineStyleNameSet != null) && (! mHeadlineStyleNameSet.isEmpty())) {
Paragraphs paragraphs = doc.getParagraphs();
for (int i = 1; i <= paragraphs.getCount(); i++) {
Paragraph paragraph = paragraphs.item(i);
// Get the name of the style for this paragraph
// NOTE: See the Style class for getting other values from the style
Object styleDispatch = paragraph.getFormat().getStyle().getDispatch();
String formatName = Dispatch.get(styleDispatch, "NameLocal").toString();
if (mHeadlineStyleNameSet.contains(formatName)) {
// This paragraph is a headline -> add it to the headlines StringBuffer
// Extract the text
paragraph.getRange().select();
String text = getSelection(mWordApplication);
text = removeBinaryStuff(text);
// Add it to the headlines
if (headlines == null) {
headlines = new StringBuffer();
}
headlines.append(text + "\n");
if (mLog.isDebugEnabled()) {
mLog.debug("Extracted headline: '" + text + "'");
}
}
}
}
// Read the document properties
readProperties(doc);
// Set the extracted text and the headlines
setCleanedContent(content.toString());
if (headlines != null) {
setHeadlines(headlines.toString());
}
// Dokument schlie�en (ohne Speichern)
doc.close(new Variant(false));
}
catch (ComFailException exc) {
throw new RegainException("Using COM failed.", exc);
}
}