OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Writer: convert horizontal rules to page break.

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Thu Mar 30, 2006 7:35 am    Post subject: Writer: convert horizontal rules to page break. Reply with quote

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
View user's profile Send private message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Fri Mar 31, 2006 1:45 am    Post subject: Reply with quote

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
View user's profile Send private message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Fri Mar 31, 2006 5:05 am    Post subject: Reply with quote

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
View user's profile Send private message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Fri Mar 31, 2006 5:35 am    Post subject: Reply with quote

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
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 8979
Location: Lexinton, Kentucky, USA

PostPosted: Fri Mar 31, 2006 6:51 am    Post subject: Reply with quote

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
View user's profile Send private message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Fri Mar 31, 2006 7:05 am    Post subject: Reply with quote

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
View user's profile Send private message
Roswell_
General User
General User


Joined: 30 Mar 2006
Posts: 7

PostPosted: Fri Mar 31, 2006 7:33 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group