I am developing an application that opens one or more .odt documents, and lets N different clients to see and edit those documents, not the same part at the same time. The point is, I need to copy a part or a whole document to another new document, so another client can edit it.
In order to copy the document I get one by one ever paragraph in the original document and copy it to the new editable document. I have had no problems so far with the text (thanks to stackoverflow, by the way), but I can't get to know if the current paragraph is text or a graphic, and copy that graphic; actually, the graphics are interpreted as blank paragraphs.
I have already seen several answers, including export images and graphics in doc files to images in openoffice, which was somewhat helpful, but doesn't match my needs, as I need to know where the image is and put it in the new document in the same order as the original one.
Opening the document, before doing anything with it, I have this code to process it:
XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface( XTextDocument.class,xComp );
XText xText = xTextDocument.getText();//Get the text from the open document
XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)UnoRuntime.queryInterface( XEnumerationAccess.class,xText );
XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration();//Get every paragraph in the document
try
{
while ( xParagraphEnumeration.hasMoreElements() )
{
XTextContent xTextElement = (XTextContent)UnoRuntime.queryInterface( XTextContent.class,xParagraphEnumeration.nextElement() );
aParagraphs.add(xTextElement);//Store the paragraphs
XServiceInfo xServiceInfo = (XServiceInfo)UnoRuntime.queryInterface( XServiceInfo.class,xTextElement );
System.out.println("Document(Document,Index - Supported Services");
String [] aS = xServiceInfo.getSupportedServiceNames();//What are your supported services?
for( int i=0 ; i<aS.length ; i++ )
System.out.println("Document(Document,Index) - " + aS[i]);
if( xServiceInfo.supportsService("com.sun.star.text.TextGraphicObject") )//Are you a graphic?
System.out.println("Document(Document,Index) - This is a graphic.");
else if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") )//Are you text??
{
//b.copyParagraphFormat(j,xTextCursor);
//xText.insertTextContent(xTextCursor, b.getParagraphs().get(j), false);
//xText.insertString(xTextCursor, b.getParagraphs().get(j).getAnchor().getString(), false);
System.out.println("Document(Document,Index) - This is text.");
}
else //Are you anything else?
System.out.println("Document(Document,Index) - This isn't either graphic nor text??");
}
}
Processing a document with some paragraphs and an image, the result is:
Document(Document,Index - Supported Services
Document(Document,Index) - com.sun.star.text.TextContent
Document(Document,Index) - com.sun.star.text.Paragraph
Document(Document,Index) - com.sun.star.style.CharacterProperties
Document(Document,Index) - com.sun.star.style.CharacterPropertiesAsian
Document(Document,Index) - com.sun.star.style.CharacterPropertiesComplex
Document(Document,Index) - com.sun.star.style.ParagraphProperties
Document(Document,Index) - com.sun.star.style.ParagraphPropertiesAsian
Document(Document,Index) - com.sun.star.style.ParagraphPropertiesComplex
Document(Document,Index) - This is text
Not only for the text paragraphs (which is correct), but for the image, too. Finally, my question is: how can I get to know if a given XTextContent is text or graphic?? Or maybe, the question should be: how can I get the code to process the graphic as a graphic and not as a paragraph (meaning, text)??
Sorry for my English and the long post, and thank you in advance.