Difference between revisions of "API/Samples/Java/Writer/GraphicsInserter"
(→General bootstrap loader) |
m |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | + | <noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Writer/API]]</noinclude> | |
− | + | Description: This example demo how to insert a picture from local file system to an {{AOo}} TextDocument. It's consist of four parts in the program. | |
− | + | #General bootstarp loader | |
− | + | #Conversion between local file system Path and URLs | |
− | + | #File and location validity check | |
− | + | #GraphicsObject services and its parameters | |
− | + | ||
Line 26: | Line 25: | ||
convertFromURL | convertFromURL | ||
− | < | + | <syntaxhighlight lang ="java"> |
/** | /** | ||
− | * Converts an URL into a system path using | + | * Converts an URL into a system path using AOo API |
* @param sURLPath | * @param sURLPath | ||
* @return | * @return | ||
Line 49: | Line 48: | ||
} | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
convertToURL | convertToURL | ||
− | < | + | <syntaxhighlight lang="java"> |
/** | /** | ||
− | * Converts a system path into an URL using | + | * Converts a system path into an URL using AOo API |
* @param sBase | * @param sBase | ||
* @param sSystemPath | * @param sSystemPath | ||
Line 76: | Line 75: | ||
} | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
The interface XfileIdentifierConverter specifies methods to convert between (file) URLs and file paths in system dependent notation. | The interface XfileIdentifierConverter specifies methods to convert between (file) URLs and file paths in system dependent notation. | ||
− | For more details information about URLs, please visit [ | + | For more details information about URLs, please visit [https://www.openoffice.org/ucb/docs/fileurl.html Be Careful with file URLs], file naming notations and conversion were discussed in detail. |
== File and location validity check == | == File and location validity check == | ||
− | The Interface of UCB – XSimpleFileAccess provides some basic methods to access a file/directory. Methods used here are “isFolder()” and “exists()”, to check if an URL represents a folder and to check if a file exists. For detail of specification of XsimpleFileAccess, please visit [ here]. | + | The Interface of UCB – XSimpleFileAccess provides some basic methods to access a file/directory. Methods used here are “isFolder()” and “exists()”, to check if an URL represents a folder and to check if a file exists. For detail of specification of XsimpleFileAccess, please visit [https://www.openoffice.org/api/docs/common/ref/com/sun/star/ucb/XSimpleFileAccess.html here]. |
− | + | ||
− | + | ||
== GraphicsObject services and its parameters == | == GraphicsObject services and its parameters == | ||
Line 93: | Line 90: | ||
Grahpics inserted into text document was a TextContent, which was created as “com.sun.star.text.TextGraphicObject” by Factory Services. | Grahpics inserted into text document was a TextContent, which was created as “com.sun.star.text.TextGraphicObject” by Factory Services. | ||
− | < | + | <syntaxhighlight lang="java"> |
// Querying for the interface XMultiServiceFactory on the XTextDocument | // Querying for the interface XMultiServiceFactory on the XTextDocument | ||
XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) | XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) | ||
Line 105: | Line 102: | ||
XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface( | XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface( | ||
XTextContent.class, oGraphic); | XTextContent.class, oGraphic); | ||
− | </ | + | </syntaxhighlight> |
Then it could be inserted into the document as a textcontent: | Then it could be inserted into the document as a textcontent: | ||
− | < | + | <syntaxhighlight lang="java"> |
// Getting the text | // Getting the text | ||
XText xText = xTextDoc.getText(); | XText xText = xTextDoc.getText(); | ||
Line 118: | Line 115: | ||
// Inserting the content | // Inserting the content | ||
xText.insertTextContent(xTextCursor, xTextContent, true); | xText.insertTextContent(xTextCursor, xTextContent, true); | ||
− | </ | + | </syntaxhighlight> |
Till now, it is a blank text content, like a empty picture holder in the text document. Thus, the properties of this graphicObject/ picture holder should be assigned, to tell the writer about what or where picture it should display(picture URLs), positions and sizes etc. | Till now, it is a blank text content, like a empty picture holder in the text document. Thus, the properties of this graphicObject/ picture holder should be assigned, to tell the writer about what or where picture it should display(picture URLs), positions and sizes etc. | ||
− | < | + | <syntaxhighlight lang="java"> |
// Querying for the interface XPropertySet on the graphic object | // Querying for the interface XPropertySet on the graphic object | ||
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( | XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( | ||
Line 144: | Line 141: | ||
// Setting the height | // Setting the height | ||
xPropSet.setPropertyValue("Height", new Integer(4000)); | xPropSet.setPropertyValue("Height", new Integer(4000)); | ||
− | </ | + | </syntaxhighlight> |
+ | |||
+ | |||
+ | Then, work has been done. | ||
+ | |||
+ | |||
+ | == Example project download == | ||
+ | |||
+ | [[Image:GraphicsInserter.zip]] |
Latest revision as of 13:13, 5 March 2021
Description: This example demo how to insert a picture from local file system to an Apache OpenOffice TextDocument. It's consist of four parts in the program.
- General bootstarp loader
- Conversion between local file system Path and URLs
- File and location validity check
- GraphicsObject services and its parameters
Contents
General bootstrap loader
General process of bootstrap loader was just like other examples, you may look at [Here]. In GraphicsInserter, the process was wrapped into several methods/levels. On top of that, you may quickly implement your methods like createDrawDocument(), createSpreadsheetDocument(), createImpressDocument() etc.
getMultiComponentFactory() method creates the remote office context( XcomponentContext type) from BootStarp.bootstarp(), then get the services manager(XMultiComponentFactory type). It prepared office context and service manager to upper level, the newDoccomponent() method.
newDoccomponent() will use office context and service manager to create Desktop service and query its XComponentLoader interface to load a document according to parameter passed from its higher level, in this case – createTextDocument().
createTextDocument() is for passing a document type as parameter, e.g. “swriter” to NewDocComponent(String docType) to create document and get it's reference of XtextDocument interface for later manipulation. (“swriter” will be appended after “private:factory/”, as a URLs, the 1st parameter of loadComponentFromURL.)
Conversion between local file system Path and URLs
convertFromURL
/** * Converts an URL into a system path using AOo API * @param sURLPath * @return */ private String convertFromURL(String sURLPath) { String sSystemPath = null; try { m_xMCF = getMultiComponentFactory(); XFileIdentifierConverter xFileConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface( XFileIdentifierConverter.class, m_xMCF.createInstanceWithContext( "com.sun.star.ucb.FileContentProvider", m_xContext)); sSystemPath = xFileConverter.getSystemPathFromFileURL(sURLPath); } catch (com.sun.star.uno.Exception e) { e.printStackTrace(System.err); } finally { return sSystemPath; } }
convertToURL
/** * Converts a system path into an URL using AOo API * @param sBase * @param sSystemPath * @return */ private String convertToURL(String sBase, String sSystemPath) { String sURL = null; try { m_xMCF = getMultiComponentFactory(); XFileIdentifierConverter xFileConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface( XFileIdentifierConverter.class, m_xMCF.createInstanceWithContext( "com.sun.star.ucb.FileContentProvider", m_xContext)); sURL = xFileConverter.getFileURLFromSystemPath( sBase, sSystemPath ); } catch (com.sun.star.uno.Exception e) { e.printStackTrace(); } finally { return sURL; } }
The interface XfileIdentifierConverter specifies methods to convert between (file) URLs and file paths in system dependent notation.
For more details information about URLs, please visit Be Careful with file URLs, file naming notations and conversion were discussed in detail.
File and location validity check
The Interface of UCB – XSimpleFileAccess provides some basic methods to access a file/directory. Methods used here are “isFolder()” and “exists()”, to check if an URL represents a folder and to check if a file exists. For detail of specification of XsimpleFileAccess, please visit here.
GraphicsObject services and its parameters
Grahpics inserted into text document was a TextContent, which was created as “com.sun.star.text.TextGraphicObject” by Factory Services.
// Querying for the interface XMultiServiceFactory on the XTextDocument XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc); // Creating the service GraphicObject Object oGraphic = xMSFDoc.createInstance( "com.sun.star.text.TextGraphicObject"); // Querying for the interface XTextContent on the GraphicObject XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface( XTextContent.class, oGraphic);
Then it could be inserted into the document as a textcontent:
// Getting the text XText xText = xTextDoc.getText(); // Getting the cursor on the document XTextCursor xTextCursor = xText.createTextCursor(); // Inserting the content xText.insertTextContent(xTextCursor, xTextContent, true);
Till now, it is a blank text content, like a empty picture holder in the text document. Thus, the properties of this graphicObject/ picture holder should be assigned, to tell the writer about what or where picture it should display(picture URLs), positions and sizes etc.
// Querying for the interface XPropertySet on the graphic object XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, oGraphic); // Setting the anchor type xPropSet.setPropertyValue("AnchorType", TextContentAnchorType.AT_PARAGRAPH); // Setting the graphic url xPropSet.setPropertyValue("GraphicURL", m_sGraphicFileURL); // Setting the horizontal position xPropSet.setPropertyValue("HoriOrientPosition", new Integer(5500)); // Setting the vertical position xPropSet.setPropertyValue("VertOrientPosition", new Integer(4200)); // Setting the width xPropSet.setPropertyValue("Width", new Integer(4400)); // Setting the height xPropSet.setPropertyValue("Height", new Integer(4000));
Then, work has been done.