| View previous topic :: View next topic |
| Author |
Message |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Thu Mar 30, 2006 7:35 am Post subject: Writer: convert horizontal rules to page break. |
|
|
Hi, i'm working on a html - > pdf converter based on a model in Java.
After spending sometimes in this forum i found almost everything, but now I'd like to force a page break whenever a <HR/> occurs.
I know how to make a new page break at the cursor's position, but i didn't found out how to locate horizontal rules in the writer. I looked into graphicsobjets and paragraphes enumerations but didn't found the HRs.
Any ideas? |
|
| Back to top |
|
 |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Fri Mar 31, 2006 1:45 am Post subject: |
|
|
Apparently the properties ParaBottomMargin at 499 and ParaIsCharacterDistance at true for TextElements supporting the paragraphe service seems to caraterise HRs.
I'll try to implement that in java. |
|
| Back to top |
|
 |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Fri Mar 31, 2006 5:05 am Post subject: |
|
|
Can someone help me to delete the HRs i have founded in paragraphes?
I managed to find them and add a page break.
Here is the function:
| Code: | protected void applyPageBreak(XText mxDoc) throws java.lang.Exception
{
try
{
// Get an enumeration of paragraphs.
XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
XEnumerationAccess.class, mxDoc );
XEnumeration xParaEnum = xParaAccess.createEnumeration();
// Creat a text cursor and go the beginning.
XTextCursor xTextCursor = mxDoc.createTextCursor();
xTextCursor.gotoStart(false);
// Set up a paragraph cursor and point it to the first paragarph.
// when the enumeration gets a new element, select that paragraph using the cursor.
XParagraphCursor xParagraphCursor = (XParagraphCursor) UnoRuntime.queryInterface(
XParagraphCursor.class, xTextCursor );
while ( xParaEnum.hasMoreElements() )
{
xParaEnum.nextElement(); // This line edited 11/21/2003, after original post.
if (xParagraphCursor.isStartOfParagraph() & xParagraphCursor.isEndOfParagraph() )
{
//"Empty paragraphe"
// query the XPropertySet interface
XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xParagraphCursor);
if( AnyConverter.toLong(xCursorProps.getPropertyValue("ParaBottomMargin")) == 499
&& AnyConverter.toBoolean(xCursorProps.getPropertyValue("ParaIsCharacterDistance")) == true
&& AnyConverter.toBoolean(xCursorProps.getPropertyValue("ParaIsConnectBorder")) == false)
// it's an Horizontal Rule
{
xCursorProps.setPropertyValue("BreakType", new Integer(com.sun.star.style.BreakType.PAGE_AFTER_value));
mxDoc.insertControlCharacter(xParagraphCursor, ControlCharacter.PARAGRAPH_BREAK, false);
xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xParagraphCursor);
xCursorProps.setPropertyValue("BreakType", new Integer(com.sun.star.style.BreakType.NONE_value));
}
}
else
{
xParagraphCursor.gotoEndOfParagraph(true); //select the current paragraph.
System.out.println("xTextCursor.getString()=" + xTextCursor.getString());
}
xParagraphCursor.gotoNextParagraph(false);
}
}
catch ( Exception e )
{
e.printStackTrace( System.out );
}
} |
|
|
| Back to top |
|
 |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Fri Mar 31, 2006 5:35 am Post subject: |
|
|
grrr.
Removing HRs in paragraphes works in vb, but i can't get it to work in java (got an execption)
Here is the VB code:
| Code: | REM ***** BASIC *****
sub removeHrs
Dim oTextEnum As Object, oTextElement As Object, oText As Object
oDocument = ThisComponent
oText = oDocument.Text
oTextEnum =oText.createEnumeration
While oTextEnum.hasMoreElements()
oTextElement = oTextEnum.nextElement()
If oTextElement.supportsService("com.sun.star.text.Paragraph") Then
if oTextElement.ParaBottomMargin = 499 And oTextElement.ParaIsCharacterDistance then
[b]oText.removeTextContent(oTextElement)[/b]
AddPageBreak 'function to add a page break
end if
End If
Wend
end sub |
I thought the equivalent in java would be:
| Code: | XTextContent xtc = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xParagraphCursor);
mxDoc.removeTextContent(xtc); |
but I get an excecption when removing: at $Proxy7.removeTextContent(Unknown Source) |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8979 Location: Lexinton, Kentucky, USA
|
Posted: Fri Mar 31, 2006 6:51 am Post subject: |
|
|
| In Basic I would normally use the equivalent of Find & Replace to deal with this. Once a cursor has the <HR/> selected I would use Cursor.setString("") to eliminate the characters, then insert the page break. |
|
| Back to top |
|
 |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Fri Mar 31, 2006 7:05 am Post subject: |
|
|
Well actually the writer never have the string "<HR>" within its paragraphs, because i use the insertDocumentFromURL method to load the html file in the writer.
So i don't have to do any formating, all html tags gets translated at loading time.
Thx anyway. |
|
| Back to top |
|
 |
Roswell_ General User

Joined: 30 Mar 2006 Posts: 7
|
Posted: Fri Mar 31, 2006 7:33 am Post subject: |
|
|
Ok, finally I get it to work.
Here is a working methods, they'r probably more elegant, especially the way i detect HRs.
| Code: | protected void applyPageBreak(XText mxDoc) throws java.lang.Exception
{
try
{
// Get an enumeration of paragraphs.
XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
XEnumerationAccess.class, mxDoc );
XEnumeration xParaEnum = xParaAccess.createEnumeration();
// Creat a text cursor and go the beginning.
XTextCursor xTextCursor = mxDoc.createTextCursor();
xTextCursor.gotoStart(false);
// Set up a paragraph cursor and point it to the first paragarph.
// when the enumeration gets a new element, select that paragraph using the cursor.
XParagraphCursor xParagraphCursor = (XParagraphCursor) UnoRuntime.queryInterface(
XParagraphCursor.class, xTextCursor );
boolean removePrev=false;
while ( xParaEnum.hasMoreElements() )
{
Object paragraph = xParaEnum.nextElement();
XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface( XServiceInfo.class, paragraph);
if(xInfo.supportsService("com.sun.star.text.Paragraph"))
{
XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, paragraph);
if( AnyConverter.toLong(xCursorProps.getPropertyValue("ParaBottomMargin")) == 499
&& AnyConverter.toBoolean(xCursorProps.getPropertyValue("ParaIsCharacterDistance")) == true
&& AnyConverter.toBoolean(xCursorProps.getPropertyValue("ParaIsConnectBorder")) == false)
// it's an Horizontal Rule
{
//remove the HR
XTextContent xtc = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, paragraph);
mxDoc.removeTextContent(xtc);
if(xParaEnum.hasMoreElements() )
{
paragraph = xParaEnum.nextElement();
xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, paragraph);
xCursorProps.setPropertyValue("BreakType", new Integer(com.sun.star.style.BreakType.PAGE_BEFORE_value));
mxDoc.insertControlCharacter(xParagraphCursor, ControlCharacter.PARAGRAPH_BREAK, false);
xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xParagraphCursor);
xCursorProps.setPropertyValue("BreakType", new Integer(com.sun.star.style.BreakType.NONE_value));
}
}
}
xParagraphCursor.gotoNextParagraph(false);
}
}
catch ( Exception e )
{
e.printStackTrace( System.out );
}
} |
|
|
| Back to top |
|
 |
|