Hi Guys,
I have a DOC file. I want to create a sub-document from it. Say, I have a
DOC of 5 pages. I want to create a DOC out of it which could be of any of
the following depending upon the user requirement -
1> Pages 1-3
2> Pages 2-4
3> Pages 4-5
User can mention the page range.
I have written a code for it which takes care of all the above mentioned 3
cases. But the problem is that sometimes it creates the sub-document with
all the pages (i.e. 5) despite being given the page range. Please take a
look at the attached code snippet -
In the code you will find 3 variables -
"isStartPageRange" -> indicates case <1>
"isMidPageRange"-> indicates case <2>
"isEndPageRange"-> indicates case <3>
public String createSubArtifact(
String artifactFileName,
String pageRange,
int pageCount) {
String subArtifactFileName = null;
XComponent xComponent = null;
boolean isStartPageRange = false;
boolean isMidPageRange = false;
boolean isEndPageRange = false;
String[] arr = pageRange.split("[-]");
int startIndex = 0;
int endIndex = 0;
if (arr != null && arr.length == 2) {
startIndex = Integer.parseInt(arr[0]);
endIndex = Integer.parseInt(arr[1]);
}
int diff = endIndex - startIndex;
if (startIndex <= endIndex) {
if (startIndex == 1 && endIndex < pageCount) {
isStartPageRange = true;
startIndex = endIndex;
} else if (startIndex > 1 && endIndex ==
pageCount) {
isEndPageRange = true;
} else if (startIndex > 1 && endIndex < pageCount)
{
isMidPageRange = true;
}
}
if (!isStartPageRange && !isEndPageRange &&
!isMidPageRange) {
System.out.println("Invalid page range given.");
System.exit(0);
}
try {
//Load a given document
PropertyValue[] prop = new PropertyValue[1];
prop[0] = new PropertyValue();
prop[0].Name = "Hidden";
prop[0].Value = Boolean.TRUE;
String artifactFilePath = "xxxxxxxxxxxxxxx";//get
the file path
System.out.println(
"Original artifact file path : " +
artifactFilePath);
xComponent = "xxxxxxxxxxxxxxxxx";//get the file
path
XTextDocument xTextDocument =
(XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class,
xComponent);
XController xController =
xTextDocument.getCurrentController();
XTextViewCursorSupplier xTextViewCursorSupplier =
(XTextViewCursorSupplier)
UnoRuntime.queryInterface(
XTextViewCursorSupplier.class,
xController);
XTextViewCursor xTextViewCursor =
xTextViewCursorSupplier.getViewCursor();
XPageCursor xPageCursor =
(XPageCursor) UnoRuntime.queryInterface(
XPageCursor.class,
xTextViewCursor);
XFrame xFrame = xController.getFrame();
XDispatchProvider xDispatchProvider =
(XDispatchProvider)
UnoRuntime.queryInterface(
XDispatchProvider.class,
xFrame);
Object objDispatchHelper =
ooAdapter.getXMultiServiceFactory().createInstance(
"com.sun.star.frame.DispatchHelper");
XDispatchHelper xDispatchHelper =
(XDispatchHelper)
UnoRuntime.queryInterface(
XDispatchHelper.class,
objDispatchHelper);
xPageCursor.jumpToPage((short)startIndex);
if (isStartPageRange) {
xPageCursor.jumpToEndOfPage();
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:EndOfDocumentSel",
"",
0,
new PropertyValue[0]);
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:Delete",
"",
0,
new PropertyValue[0]);
} else if (isEndPageRange) {
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:StartOfDocumentSel",
"",
0,
new PropertyValue[0]);
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:Delete",
"",
0,
new PropertyValue[0]);
} else if (isMidPageRange) {
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:StartOfDocumentSel",
"",
0,
new PropertyValue[0]);
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:Delete",
"",
0,
new PropertyValue[0]);
endIndex = startIndex + diff +
xPageCursor.getPage();
xPageCursor.jumpToPage((short) endIndex);
xPageCursor.jumpToPreviousPage();
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:EndOfDocumentSel",
"",
0,
new PropertyValue[0]);
xDispatchHelper.executeDispatch(
xDispatchProvider,
".uno:Delete",
"",
0,
new PropertyValue[0]);
}
//save sub artifact into disk
subArtifactFileName = "tt.doc";
String subArtifactFilePath = ooTempDirPath +
subArtifactFileName;
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "MS Word 97";
XStorable xStorable =
(XStorable) UnoRuntime.queryInterface(
XStorable.class,
xComponent);
xStorable.storeAsURL(subArtifactFilePath,
storeProps);
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (xComponent != null) {
xComponent.dispose();
}
}
return subArtifactFileName;
}
One interesting thing is that sub documents can be created from simple
documents. But for complex documents (e.g. documents having header,
footer, long tables spanning multiple pages, hidden blank pages), the code
fails to produce desired results.
Could anybody please tell me where could be the mistake? Any suggestion
will be of immense help to me.
Thanks & Regards
Prasenjit Paik