« Return to Thread: Extract all content from a page of a document and add to a textframe in another document

Re: Extract all content from a page of a document and add to a textframe in another document

by Chris Fleischmann :: Rate this Message:

Reply to Author | View in Thread

I have since found that I can extract the contents of the page (albeit a little too manual for my liking), like so:

private void copyContents() {
        try {
            //the controllers
            XController xController_sourceDoc = xTextDocument_src.getCurrentController();
            XController xController_targetDoc = xTextDocument_dest.getCurrentController();

            // the cursor for the source document
            XTextViewCursorSupplier xViewCursorSupplier_sourceDoc =
                    (XTextViewCursorSupplier)
                    UnoRuntime.queryInterface(XTextViewCursorSupplier.class,
                    xController_sourceDoc);

            //selecting the whole source document
            XTextViewCursor xTextViewCursor_sourceDoc =
                    xViewCursorSupplier_sourceDoc.getViewCursor();

            XPageCursor xPageCursor =
                    (XPageCursor) UnoRuntime.queryInterface(
                    XPageCursor.class,
                    xTextViewCursor_sourceDoc);

            // select the current page of document with the cursor
            xPageCursor.jumpToLastPage();
            xPageCursor.jumpToEndOfPage();

            int currentPage = xPageCursor.getPage();

            while (xPageCursor.getPage() == currentPage) {
                if (!xTextViewCursor_sourceDoc.goLeft((short)1, true)) {
                    break;
                }
            }

            //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);

            //the cursor for the target document
            XTextViewCursorSupplier xViewCursorSupplier_targetDoc =
                    (XTextViewCursorSupplier)
                    UnoRuntime.queryInterface(XTextViewCursorSupplier.class,
                    xController_targetDoc);

            //going to the end of the source document
            XTextViewCursor xTextViewCursor_targetDoc =
                    xViewCursorSupplier_targetDoc.getViewCursor();
                    xTextViewCursor_targetDoc.gotoEnd(false);

            //inserting the source document there
            xTransferableSupplier_targetDoc.insertTransferable(xTransferable);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }


Now obviously I can expland the while loop to iterate over "every" page as I am doing above... Now to see if i can add it to a textframe (autosizing as appropriate).

If there is a better approach, please do let me know.

Chris Fleischmann wrote:
Folks, perhaps this has already been answered, or perhaps cannot be done, but I am attempting to add content to a Writer / XText... adding content that is either an image or normal text (with different font attributes; bold, italics, underline etc)... Once the page is created, I would then like to be able to extract all of the contents from the newly created pages, and with a new blank document, transfer each page in to a textframe that I programmatically create shrinking the "page content" to fit the text frame...

Does any one know how I would go about that/this?

So far I have simple functions like;

protected void createExampleData(com.sun.star.text.XText xText) {

        try {
            xText.setString("This is an example sentence");

            com.sun.star.text.XWordCursor xWordCursor =
                    (com.sun.star.text.XWordCursor) UnoRuntime.queryInterface(
                    com.sun.star.text.XWordCursor.class, xText.getStart());

            xWordCursor.gotoNextWord(false);
            xWordCursor.gotoNextWord(false);
            xWordCursor.gotoEndOfWord(true);

            com.sun.star.beans.XPropertySet xPropertySet =
                    (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
                    com.sun.star.beans.XPropertySet.class, xWordCursor);
            xPropertySet.setPropertyValue("CharWeight",
                    new Float(com.sun.star.awt.FontWeight.BOLD));
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    protected void loadJpgImage(XText xText, String strImgFileName, int width, int height)
            throws Exception, java.lang.Exception {
        // insert RectangleShape and get shape text, then manipulate text
        Object textGraphicObject = xWriterFactory.createInstance("com.sun.star.text.TextGraphicObject");

        XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, textGraphicObject);

        XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, textGraphicObject);

        xPropertySet.setPropertyValue("AnchorType", TextContentAnchorType.AT_PARAGRAPH);

        xText.insertTextContent(xText.getEnd(), xTextContent, false);

        // Setting the vertical and horizontal position
        //xPropertySet.setPropertyValue("HoriOrientPosition", new Integer(x));
        //xPropertySet.setPropertyValue("VertOrientPosition", new Integer(y));
        xPropertySet.setPropertyValue("Width", new Integer(width));
        xPropertySet.setPropertyValue("Height", new Integer(height));

        xPropertySet.setPropertyValue("GraphicURL", strImgFileName);

        Object xBitmapContainerObject = xWriterFactory.createInstance("com.sun.star.drawing.BitmapTable");

        XNameContainer xBitmapContainer = (XNameContainer)
                UnoRuntime.queryInterface(XNameContainer.class, xBitmapContainerObject);

        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(strImgFileName.getBytes(), 0, strImgFileName.length());

        String internalName = new BigInteger(1, md.digest()).toString(16);

        xBitmapContainer.insertByName(internalName, strImgFileName);

        String internalURL = (String)(xBitmapContainer.getByName(internalName));

        xPropertySet.setPropertyValue("GraphicURL", internalURL);

        xBitmapContainer.removeByName(internalName);
    }

To create sample text, and to load images in to the document...

And now I would like to be able to extract such content/the page, to then insert the content in to a textFrame of another document. Is this possible?

 « Return to Thread: Extract all content from a page of a document and add to a textframe in another document