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

How to delete all empty pages in a Writer document

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
Cybb20
Super User
Super User


Joined: 02 Mar 2004
Posts: 1569
Location: Frankfurt, Germany

PostPosted: Sun May 15, 2005 6:35 am    Post subject: How to delete all empty pages in a Writer document Reply with quote

Code:

Sub Delete_Empty_Pages( )
 'deletes all empty pages in a writer document recursively, meaning we start from the last page,
 'working our way up to the first page.

 odoc = thisComponent
 'if you have problems with thisComponent for the new OOo 2.0 builds,
 'use Stardesktop.getCurrentComponent() instead

 view = odoc.getCurrentController()
 cursor = view.getViewCursor()
 cursor.setVisible(False)
 
 cursor.jumpToLastPage()
 DealWithPage(cursor)

End Sub

Function DealWithPage(cur)
 'recursive function, watch out for the recursion limit which used to be around 500, but
 'should have been increased by the time.
 
 Dim charcount as Integer, currentpage as Integer
 Dim bEmptyPage as Boolean

 charcount = 0
 bEmptyPage = True
 currentpage = cur.getPage()
 
 cur.jumpToEndOfPage()
 
 Do While cur.getPage() = currentpage
    If cur.goLeft(1, True) Then
       charcount = charcount + 1
        s = cur.getString()
        If Not IsWhitespace(s) Then
           bEmptyPage = False
           Exit Do
        End If
     Else
        'first page must be empty! Exit loop and handle later.
        charcount = charcount + 1
        Exit Do
     End If            
 Loop
 
 If bEmptyPage Then
    DeletePage(cur, charcount, (currentpage = 1))
 Else
    cur.jumpToPreviousPage()   
 End If   
 
 If currentpage = 1 Then Exit Function 'careful, only exit condition!
 
 DealWithPage(cur)

End Function

Function IsWhitespace(s) as Boolean
 s = Left(s, 1)
 If len(s) = 0 Then
    IsWhitespace = True
   Exit Function
 Else   
    If Asc(s) <= 32 Then    
      IsWhitespace = True
      Exit Function
   End If   
 End If

'if it fell through, it is not whitespace!
IsWhiteSpace = False      
End Function

Function DeletePage(cur, i as Integer, bFirstPage as Boolean)
 'msgbox "charcount = " & i & " for page = " & cur.getPage()-1
 If bFirstPage Then
    cur.jumpToStartOfPage()
 Else
    cur.jumpToEndOfPage()
 End If
 
 cur.goRight(i, True)
 cur.setString("")
    
End Function


Let me know if you have any problems using it or if you think something must be corrected or some parts should be improved.

UPDATE:
As of today, this macro only works for formatted text only, meaning it does not work for tables and other objects.

Christian
_________________
- Knowledge is Power -
Back to top
View user's profile Send private message Send e-mail
sandyraghav
General User
General User


Joined: 16 Jul 2007
Posts: 13

PostPosted: Thu Aug 02, 2007 8:56 pm    Post subject: Re:How to delete all empty pages in a Writer document Reply with quote

Hi Christian,
I have a task to delete some pages from my writer document and thought this is the solution but this code is just to remove the contents of the page and not to remove the page itself from the writer document. What else we need to do in case we want to delete the page itself? Any idea?
Thanks
_________________
Impossible is nothing!!!
Back to top
View user's profile Send private message
merryelf
Newbie
Newbie


Joined: 06 Oct 2008
Posts: 4

PostPosted: Thu Oct 09, 2008 7:54 pm    Post subject: Reply with quote

you may get some useful information from here:
http://markmail.org/message/dd3ecinbzzg5x462#query:XPageCursor+page:1+mid:way2ogtp32yqfpwj+state:results

Hope it is helpful.
_________________
You never know what you can do till you try.
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 Code Snippets 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