Hello Chris,
On Wednesday 24 June 2009, 03:06, [email protected] wrote:
> Hello.... Hrm, I have read through your example... Thank you very much... I
> am still stuck... Just a newbie... I'm sure its a simple solution.. Just
> cant find it :-(
the problem with your code is that the document with the text frames has only
*one* paragraph, so there is no change to have page breaks == different pages
(a page break is a paragraph property, you only have one paragraph ==> then,
all text frames are anchored to the very same paragraph; then, when you get
the anchor and insert a page break, you only have one page because it is the
very same and only paragraph you are modifying).
The solution is to insert a new paragraph right after you insert a text frame,
like in my code:
private void insertTextFrames(XTextDocument xTextDocument){
try {
XMultiServiceFactory xDocFactory =
(XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, xTextDocument);
XText xText = xTextDocument.getText();
XTextCursor xTextCursor =
xText.createTextCursorByRange(xText.getStart());
// first insert the text frames
for (int i = 0; i < FRAMES_COUNT; i++) {
insertTextFrame(xDocFactory, xTextCursor);
insertParaBreak(xTextCursor);
}
} catch (Exception e) {
e.printStackTrace();
}
}
> Please see my code here below:
see the change I've made to your code (look at the /*=======>*/ in line 241).
The change only answers the subject of this thread (Re: I have five text frames
on the one page, can I move each frame to a new page?): now every frame is in
its own page. You will have to fix other things to achieve what you're looking
for.
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
package org.openoffice.sdk;
import com.sun.star.awt.Size;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.datatransfer.XTransferable;
import com.sun.star.datatransfer.XTransferableSupplier;
import com.sun.star.drawing.XShape;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XController;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.HoriOrientation;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.VertOrientation;
import com.sun.star.text.XPageCursor;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextFrame;
import com.sun.star.text.XTextFramesSupplier;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class TextDocumentStructure {
private XTextDocument xTextDocument_src;
private XTextDocument xTextDocument_dest;
private XMultiServiceFactory xWriterFactory;
private XComponent xWriterComponent_src;
private XComponent xWriterComponent_dest;
private Size page_size;
private Size frame_size;
public static void main(String args[]) {
TextDocumentStructure tds = new TextDocumentStructure();
tds.copyContents();
tds.moveTextFrames();
// tds.saveFile();
System.exit(1);
}
public TextDocumentStructure() {
try {
// get the remote office component context
XComponentContext xRemoteContext = Bootstrap.bootstrap();
if (xRemoteContext == null) {
System.err.println("ERROR: Could not bootstrap default Office.");
}
XMultiComponentFactory xRemoteServiceManager =
xRemoteContext.getServiceManager();
Object desktop =
xRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", xRemoteContext);
XComponentLoader xComponentLoader =
(XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
PropertyValue[] loadProps = new PropertyValue[0];
xWriterComponent_src =
xComponentLoader.loadComponentFromURL("private:factory/swriter",
"_blank", 0, loadProps);
xWriterComponent_dest =
xComponentLoader.loadComponentFromURL("private:factory/swriter",
"_blank", 0, loadProps);
xTextDocument_src =
(XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,
xWriterComponent_src);
xTextDocument_dest =
(XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,
xWriterComponent_dest);
// get internal service factory of the document
xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
XMultiServiceFactory.class, xWriterComponent_src);
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, xTextDocument_dest);
// get the NameAccess interface from the Style family collection
XNameAccess xNameAccess = xSupplier.getStyleFamilies();
XNameContainer xPageStyleCollection = (XNameContainer)
UnoRuntime.queryInterface(
XNameContainer.class, xNameAccess.getByName("PageStyles"));
// create a PropertySet to set the properties for the new Pagestyle
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xPageStyleCollection.getByName("Default"));
// Flip the size
page_size = (Size) xPropertySet.getPropertyValue("Size");
int height = page_size.Height;
page_size.Height = page_size.Width;
page_size.Width = height;
xPropertySet.setPropertyValue("Size", page_size);
// Switch the orientation
xPropertySet.setPropertyValue("IsLandscape", new Boolean(true));
xPropertySet.setPropertyValue("LeftMargin", new Short((short) 0));
xPropertySet.setPropertyValue("RightMargin", new Short((short) 0));
xPropertySet.setPropertyValue("TopMargin", new Short((short) 0));
xPropertySet.setPropertyValue("BottomMargin", new Short((short) 0));
xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, xTextDocument_src);
// get the NameAccess interface from the Style family collection
xNameAccess = xSupplier.getStyleFamilies();
xPageStyleCollection = (XNameContainer) UnoRuntime.queryInterface(
XNameContainer.class, xNameAccess.getByName(
"PageStyles"));
// create a PropertySet to set the properties for the new Pagestyle
xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xPageStyleCollection.getByName("Default"));
xPropertySet.setPropertyValue("LeftMargin", new Short((short) 0));
xPropertySet.setPropertyValue("RightMargin", new Short((short) 0));
xPropertySet.setPropertyValue("TopMargin", new Short((short) 0));
xPropertySet.setPropertyValue("BottomMargin", new Short((short) 0));
frame_size = new Size();
frame_size.Height = page_size.Height - 2000;
frame_size.Width = (page_size.Width / 2) - 2000;
// create some example data
XText xText = xTextDocument_src.getText();
createExampleData(xText);
XTextCursor xTextCursor = xText.createTextCursor();
xTextCursor.gotoEnd(false);
xText.insertControlCharacter(
xTextCursor,
com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH,
false);
//loadJpgImage(xText, "file:///Users/chrisf/Pictures/orthohedron1600.jpg", 4000, 4000);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
private void moveTextFrames() {
XTextFramesSupplier xTextFramesSupplier =
(XTextFramesSupplier) UnoRuntime.queryInterface(
XTextFramesSupplier.class, xWriterComponent_dest);
XNameAccess xNamedFrames = xTextFramesSupplier.getTextFrames();
XIndexAccess xIndexAccess = (XIndexAccess) UnoRuntime.queryInterface(
XIndexAccess.class, xNamedFrames);
XTextFrame xTextFrame;
for (int i = 0; i < xIndexAccess.getCount(); i++) {
try {
xTextFrame = (XTextFrame) UnoRuntime.queryInterface(
XTextFrame.class, xIndexAccess.getByIndex(i));
XTextRange xAnchor = xTextFrame.getAnchor();
if (xAnchor != null) {
XPropertySet xAnchorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xAnchor);
xAnchorProps.setPropertyValue("PageDescName", "Default");
//xAnchorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private XText getTextFrame(XText xText, int x, int y) {
try {
Object textFrame = xWriterFactory.createInstance("com.sun.star.text.TextFrame");
XTextFrame xTextFrame = (XTextFrame) UnoRuntime.queryInterface(
XTextFrame.class, textFrame);
XShape xShape = (XShape) UnoRuntime.queryInterface(
XShape.class, xTextFrame);
xShape.setSize(new Size(frame_size.Width, frame_size.Height));
XPropertySet xShapeProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextFrame);
xShapeProps.setPropertyValue("AnchorType", TextContentAnchorType.AT_PARAGRAPH);
// Setting the vertical and horizontal position
xShapeProps.setPropertyValue("FrameIsAutomaticHeight", new Boolean(false));
xShapeProps.setPropertyValue("SizeType", new Short((short) 1));
xShapeProps.setPropertyValue("VertOrient", new Short(VertOrientation.NONE));
xShapeProps.setPropertyValue("HoriOrient", new Short(HoriOrientation.NONE));
xShapeProps.setPropertyValue("VertOrientPosition", new Integer(1000));
xShapeProps.setPropertyValue("HoriOrientPosition", new Integer(1000));
xShapeProps.setPropertyValue("LeftBorderDistance", new Integer(200));
xShapeProps.setPropertyValue("RightBorderDistance", new Integer(200));
xShapeProps.setPropertyValue("TopBorderDistance", new Integer(200));
xShapeProps.setPropertyValue("BottomBorderDistance", new Integer(200));
xText.insertTextContent(xText.getEnd(), xTextFrame, false);
/*=======>*/xText.insertControlCharacter(xText.getEnd(), ControlCharacter.PARAGRAPH_BREAK, false);
XText xFrameText = (XText) UnoRuntime.queryInterface(XText.class, textFrame);
return xFrameText;
} catch (java.lang.Exception e) {
e.printStackTrace();
}
return null;
}
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);
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, xTextDocument_dest);
// get the NameAccess interface from the Style family collection
XNameAccess xNameAccess = xSupplier.getStyleFamilies();
XNameContainer xPageStyleCollection = (XNameContainer)
UnoRuntime.queryInterface(
XNameContainer.class, xNameAccess.getByName(
"ParagraphStyles"));
// create a PropertySet to set the properties for the new Pagestyle
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class,
xPageStyleCollection.getByName("Default"));
Float charHeight = (Float) xPropertySet.getPropertyValue("CharHeight");
Integer paraTopMargin = (Integer) xPropertySet.getPropertyValue("ParaTopMargin");
Integer paraBottomMargin = (Integer) xPropertySet.getPropertyValue("ParaBottomMargin");
int page_height = page_size.Width;
int page_width = page_size.Height;
int frame_height = frame_size.Height;
int frame_width = frame_size.Width;
float ratio_x = (float) frame_height / (float) page_height;
float ratio_y = (float) frame_width / (float) page_width;
float ratio = 1;
if (ratio_x < ratio_y) {
ratio = ratio_x;
} else {
ratio = ratio_y;
}
// Switch the orientation
short charHeight2 = (short) (charHeight.floatValue() * ratio);
short paraTopMargin2 = (short) (paraTopMargin.floatValue() * ratio);
short paraBottomMargin2 = (short) (paraBottomMargin.floatValue() * ratio);
xPropertySet.setPropertyValue("CharHeight", new Short(charHeight2));
xPropertySet.setPropertyValue("ParaTopMargin", new Short(paraTopMargin2));
xPropertySet.setPropertyValue("ParaBottomMargin", new Short(paraBottomMargin2));
// select the current page of document with the cursor
xPageCursor.jumpToLastPage();
xPageCursor.jumpToEndOfPage();
int currentPage = xPageCursor.getPage();
//getting the data supplier of our source doc
XTransferableSupplier xTransferableSupplier_sourceDoc;
XTransferableSupplier xTransferableSupplier_targetDoc;
XTextViewCursorSupplier xViewCursorSupplier_targetDoc;
XTextViewCursor xTextViewCursor_targetDoc;
XTransferable xTransferable;
do {
while (xPageCursor.getPage() == currentPage) {
if (!xTextViewCursor_sourceDoc.goLeft((short) 1, true)) {
break;
}
}
//getting the data supplier of our source doc
xTransferableSupplier_sourceDoc =
(XTransferableSupplier) UnoRuntime.queryInterface(
XTransferableSupplier.class,
xController_sourceDoc);
//saving the selected contents
xTransferable =
xTransferableSupplier_sourceDoc.getTransferable();
// Create the TextFrame
XText xText = xTextDocument_dest.getText();
xText = getTextFrame(xText, 1000, 1000);
//getting the data supplier of our target doc
xTransferableSupplier_targetDoc =
(XTransferableSupplier) UnoRuntime.queryInterface(
XTransferableSupplier.class,
xController_targetDoc);
//the cursor for the target document
xViewCursorSupplier_targetDoc =
(XTextViewCursorSupplier) UnoRuntime.queryInterface(
XTextViewCursorSupplier.class,
xController_targetDoc);
//going to the end of the source document
xTextViewCursor_targetDoc =
xViewCursorSupplier_targetDoc.getViewCursor();
xTextViewCursor_targetDoc.gotoRange(xText.getEnd(), false);
// inserting the source document there
xTransferableSupplier_targetDoc.insertTransferable(xTransferable);
// Insert a paragraph break.
xTextViewCursor_targetDoc.getText().insertControlCharacter(
xTextViewCursor_targetDoc, ControlCharacter.PARAGRAPH_BREAK, false);
// RESET cursor and page.
xTextViewCursor_sourceDoc.goRight((short) 1, false);
xPageCursor.jumpToPreviousPage();
xPageCursor.jumpToEndOfPage();
} while (--currentPage > 0);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
private void saveFile() {
try {
XStorable xStorable =
(XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument_dest);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "MS Word 97";
xStorable.storeAsURL("file:///Users/chrisf/Documents/test.doc", storeProps);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
protected void createExampleData(com.sun.star.text.XText xText) {
try {
for (int i = 0; i < 3000; i++) {
xText.insertString(xText.getEnd(), String.valueOf(i) + " ", false);
}
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) {
XNameContainer xBitmapContainer = null;
XTextContent xImage = null;
String internalURL = null;
try {
xBitmapContainer = (XNameContainer) UnoRuntime.queryInterface(
XNameContainer.class, xWriterFactory.createInstance(
"com.sun.star.drawing.BitmapTable"));
xImage = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class, xWriterFactory.createInstance(
"com.sun.star.text.TextGraphicObject"));
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xImage);
// helper-stuff to let OOo create an internal name of the graphic
// that can be used later (internal name consists of various checksums)
xBitmapContainer.insertByName("someID", strImgFileName);
internalURL = AnyConverter.toString(xBitmapContainer.getByName("someID"));
int page_height = page_size.Width;
int page_width = page_size.Height;
int frame_height = frame_size.Height;
int frame_width = frame_size.Width;
float ratio_x = (float) frame_height / (float) page_height;
float ratio_y = (float) frame_width / (float) page_width;
float ratio = 1;
if (ratio_x < ratio_y) {
ratio = ratio_x;
} else {
ratio = ratio_y;
}
xProps.setPropertyValue("AnchorType",
com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
xProps.setPropertyValue("GraphicURL", internalURL);
xProps.setPropertyValue("Width", (int) (width * ratio));
xProps.setPropertyValue("Height", (int) (height * ratio));
// inser the graphic at the cursor position
xText.insertTextContent(xText.getEnd(), xImage, false);
// remove the helper-entry
xBitmapContainer.removeByName("someID");
} catch (Exception e) {
System.out.println("Failed to insert Graphic");
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]