Hello Chris,
On Monday 22 June 2009, 09:46, Chris Fleischmann wrote:
> Hello folks, I have TextFrames, that are created like so (at the beginning
> of the process):
...
> This method seems to work, it creates the textframe with a specific size,
> at a specific x/y location.
>
> I am now attempting to iterate over the "said" textframes like so:
...
> Secondly, is there a way to create a new page between each cycle so that I
> can move the textframe to the new page with the same, x/y origin?
you can "move" each text frame to a new page by inserting a page break at the
frame's anchor (IIRC the frame will have an anchor as long as it is not
anchored at the page).
See the Java code attached.
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
/*
* TextFramePageBreak.java
*
*/
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.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNameAccess;
import com.sun.star.drawing.XShape;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.style.BreakType;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.SizeType;
import com.sun.star.text.TextContentAnchorType;
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.uno.UnoRuntime;
/**
*
* @author ariel
*/
public class TextFramePageBreak {
private static final int FRAMES_COUNT = 5;
XComponentContext m_xContext;
/** Creates a new instance of TextFramePageBreak */
public TextFramePageBreak(XComponentContext xContext) {
m_xContext = xContext;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int nRet = 0;
try {
// get the remote office component context
XComponentContext xContext = Bootstrap.bootstrap();
if (xContext == null) {
System.err.println("ERROR: Could not bootstrap default Office.");
nRet = -1;
}
TextFramePageBreak demo = new TextFramePageBreak(xContext);
demo.firstInsertThenAccess();
} catch (java.lang.Exception e) {
e.printStackTrace();
} finally {
System.exit(nRet);
}
}
protected void firstInsertThenAccess(){
try {
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, newDocComponent(m_xContext, "swriter"));
// this simulates a situation where we want to access already inserted
// objects in order to set a page break after them
insertTextFrames(xTextDocument);
accessTextFrames(xTextDocument);
} catch (Exception e) {
e.printStackTrace();
}
}
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();
}
}
private void accessTextFrames(XTextDocument xTextDocument){
try {
// access them and insert a page break
XTextFramesSupplier xTextFramesSupplier = (XTextFramesSupplier)
UnoRuntime.queryInterface(XTextFramesSupplier.class, xTextDocument);
XNameAccess xNamedTextFrames = xTextFramesSupplier.getTextFrames();
XIndexAccess xIndexedTextFrames = (XIndexAccess) UnoRuntime.queryInterface(
XIndexAccess.class, xNamedTextFrames);
for (int i = 0; i < xIndexedTextFrames.getCount(); i++) {
XTextFrame xTextFrame = (XTextFrame) UnoRuntime.queryInterface(
XTextFrame.class, xIndexedTextFrames.getByIndex(i));
XTextRange xAnchor = xTextFrame.getAnchor();
// IIRC when anchored at the page, there is no anchor (sic.)
// (and of course, the text content CAN NOT be a text table)
if ( xAnchor != null ){
try {
XPropertySet xAnchorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xAnchor);
xAnchorProps.setPropertyValue("PageDescName", "Default");
// this also works, but results in an extra page
//xAnchorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
} catch (Exception e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//=========================================================================
//=========================================================================
private void insertText(XTextRange xTextRange, String sStr) {
xTextRange.getText().insertString(xTextRange, sStr, false);
}
private void insertParaBreak(XTextRange xTextRange) {
try {
xTextRange.getText().insertControlCharacter(
xTextRange, ControlCharacter.PARAGRAPH_BREAK, false);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void insertTextContent(XTextRange xTextRange, XTextContent xContent) {
try {
xTextRange.getText().insertTextContent(xTextRange, xContent, false);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void insertTextFrame(XMultiServiceFactory xDocFactory, XTextRange xTextRange) {
try {
XTextFrame xTextFrame = createTextFrame(xDocFactory,
12000, 4000, TextContentAnchorType.AT_PARAGRAPH, SizeType.MIN);
insertTextContent(xTextRange, xTextFrame);
XText xFrameText = xTextFrame.getText();
insertText(xFrameText.getEnd(), "Some text inside the frame.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private XTextFrame createTextFrame(
XMultiServiceFactory xDocFactory,
int nWidth, int nHeight, TextContentAnchorType aAnchorType, short nSizeType){
XTextFrame xTextFrame = null;
try {
xTextFrame = (XTextFrame) UnoRuntime.queryInterface(
XTextFrame.class, xDocFactory.createInstance(
"com.sun.star.text.TextFrame"));
XShape xShape = (XShape) UnoRuntime.queryInterface(XShape.class, xTextFrame);
xShape.setSize(new Size(nWidth, nHeight));
XPropertySet xFrameProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xTextFrame);
xFrameProps.setPropertyValue("AnchorType", aAnchorType);
xFrameProps.setPropertyValue("SizeType", new Short(nSizeType));
} catch (Exception ex){
ex.printStackTrace();
} finally {
return xTextFrame;
}
}
private XComponent newDocComponent(XComponentContext xContext, String sDocType){
XComponent xComponent = null;
try {
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
xComponent = xComponentLoader.loadComponentFromURL(
"private:factory/" + sDocType,
"_blank", FrameSearchFlag.ALL, new PropertyValue[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
return xComponent;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]