-
Notifications
You must be signed in to change notification settings - Fork 23
Appending_documents
Jochen Staerk edited this page Dec 21, 2015
·
1 revision
To load one writer document from URL and append it after another a ITextDocument , you can use the insertDocument method.
private void appendDocument(String url) { try { <THEDOCUMENT>.getViewCursorService().getViewCursor().getTextCursorFromStart().insertDocument(url) ; } catch (NOAException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
The method above might work for most cases, but sometimes it results in something ugly. The following snippet shows how to copy the content of one document into another.
This might become integrated in next NOA version somehow, but not sure (Markus Krüger).
public static void main(String[] args) { ... ... IDocumentService documentService = ...; String template = "template.odt"; String target = "target.odt"; //load source IDocument documentTemplate = documentService.loadDocument(template, DocumentDescriptor.DEFAULT_HIDDEN); ITextDocument textDocumentTemplate = (ITextDocument) documentTemplate; //marks the text in the document to be used as source IViewCursor templateViewCursor = textDocumentTemplate.getViewCursorService().getViewCursor(); templateViewCursor.getPageCursor().jumpToEndOfPage(); ITextRange end = templateViewCursor.getTextCursorFromEnd().getEnd(); templateViewCursor.getPageCursor().jumpToStartOfPage(); templateViewCursor.goToRange(end, true); //load target IDocumentDescriptor descriptor = DocumentDescriptor.DEFAULT; descriptor.setAsTemplate(true); IDocument document = documentService.loadDocument(target, descriptor); ITextDocument textDocument = (ITextDocument) document; //go to end of target document IViewCursor viewCursor = textDocument.getViewCursorService().getViewCursor(); IPageCursor pageCursor = viewCursor.getPageCursor(); pageCursor.jumpToEndOfPage(); //maybe insert a page break before ITextCursor textCursor = viewCursor.getTextCursorFromEnd(); textCursor.insertPageBreak(); //copy from soruce to target copy(textDocumentTemplate, textDocument); } private static void copy(ITextDocument sourceDoc, ITextDocument targetDoc) throws Exception { // the controllers XController xController_sourceDoc = sourceDoc.getXTextDocument().getCurrentController(); XController xController_targetDoc = targetDoc.getXTextDocument().getCurrentController(); // getting the data supplier of our source doc XTransferableSupplier xTransferableSupplier_sourceDoc = (XTransferableSupplier) UnoRuntime.queryInterface(XTransferableSupplier.class, xController_sourceDoc); // saving the selected contents XTransferable xTransferable = xTransferableSupplier_sourceDoc.getTransferable(); // getting the data supplier of our target doc XTransferableSupplier xTransferableSupplier_targetDoc = (XTransferableSupplier) UnoRuntime.queryInterface(XTransferableSupplier.class, xController_targetDoc); // inserting the source document there xTransferableSupplier_targetDoc.insertTransferable(xTransferable); }
previously, before version 2.0.8 of NOA, you had to use something like this (source: http://ubion.ion.ag/mainForumFolder/noa_forum/0022?b_start:int=0#0006):
public static final boolean dispatch(XDispatchProvider dispatchProvider, String command, PropertyValue[] params) { URL url = new URL(); url.Complete = ".uno:" + command; url.Main = ".uno:" + command; url.Path = command; url.Protocol = ".uno:"; XDispatch dispatch = dispatchProvider.queryDispatch(url, "", 0); if(dispatch != null) { dispatch.dispatch(url, params); return true; } else { return false; } } private void appendDocument(String url) { XTextDocument xText = <THEDOCUMENT>.getXTextDocument(); XController view = xText.getCurrentController(); XTextViewCursorSupplier supp = (XTextViewCursorSupplier) UnoRuntime.queryInterface( XTextViewCursorSupplier.class, view ); XPageCursor cursor = (XPageCursor) UnoRuntime.queryInterface( XPageCursor.class, supp.getViewCursor()); cursor.jumpToLastPage(); cursor.jumpToEndOfPage(); XFrame frame = parentWizard.getDocument().getXTextDocument().getCurrentController().getFrame(); XDispatchProvider xDispatchProvider = (XDispatchProvider)UnoRuntime.queryInterface(XDispatchProvider.class, frame); PropertyValue params[] = new PropertyValue[1]; params[0] = new PropertyValue(); params[0].Name = "Name"; params[0].Value = url; dispatch( xDispatchProvider, "InsertDoc", params ); }
This manual way you can still use if you have particular requirements, e.g. if you want to attach an imported document, like a HTML one, you could use
PropertyValue params[] = new PropertyValue[2]; params[0] = new PropertyValue(); params[0].Name = "Name"; params[0].Value = url; params[1] = new PropertyValue(); params[1].Name = "Filter"; params[1].Value = "HTML (StarWriter)";