Page 1 of 1

[Solved] Select a whole page that begins with a table

Posted: Mon May 13, 2019 5:04 pm
by sam_fr
I need to select a whole page (not only the text) to copy/paste it to a new document.

This sort of code do the job, but fails when the page begins with a table :

Code: Select all

Sub ko()
VisibleCursor = ThisComponent.CurrentController.ViewCursor
      With VisibleCursor
         .jumpToPage(1)
         .jumpToEndOfPage
         oRangePageEnd = .end
         .jumpToStartOfPage
         .gotoRange(oRangePageEnd, true)
      End With
End Sub
The gotoRange statement throw an Exception when the page begins with a table, without any regular text before it :

Code: Select all

Error (<class 'uno.com.sun.star.uno.RuntimeException'>) during conversion
I guess this is related to cursors that can't mix text/table, but I can't cope with that.

I attach such a file to this post (bad.odt).

Any idea to work around this ? The problems occurs at any page when the table is at its beginning, not only the first one.
Thanks

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 5:22 pm
by RoryOF
What happens if you insert a 2pt paragraph (minimum paragraph height) before the table?

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 6:27 pm
by sam_fr
There is no problem anymore if I insert a paragraph before the table.

But I don't create these docs and tables can be at begining of any page. Is there a way to automatically add a paragraph before a table if it is in first position ? I can't figure it out

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 6:39 pm
by RoryOF
Turn on /View /non printing characters (this same command turns off this display). Place cursor before first character in first cell of the table and press Alt Enter. Then place your cursor on the new blank line and change it (/Format /Paragraph) to have 2 pt font (smallest yu can set), no space above or below paragraph (on Indents and Spacing tab) and single line spacing. If you have to do this often, define a paragraph style with these parameters and simply apply that to the line above.

Alt Enter will also insert a line after the table if cursor is last character in the last cell of the table.

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 7:00 pm
by sam_fr
In fact I need to do it with a Basic Macro or Python Uno. I'm writing a script to be executed server side, to split any document sent to it.

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 7:22 pm
by MrProgrammer
sam_fr wrote:I need to select a whole page (not only the text) to copy/paste it to a new document.
I think this "page copy" idea is not well defined. For example, you can have a document containing only a 1 row by 2 column table and put enough text in the first cell so the cell content spans five pages. What would it mean to "copy page 3" to a new document? Page 3 is in the middle of a table. One can perform Insert → Manual Break → Page Break in the middle of the table cell, but this action does not create a new page.

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 10:05 pm
by sam_fr
I need to export each n pages of the document to an HTML page. For example one ODT of 100 pages has to be split in 50 HTML files (each file equals to 2 pages of the odt). For your complete comprehension, they are sent to low cost devices with very poor bandwith. Png images are reworked too. It is not very important if it splits in the middle of a table, or if page lengths are not equal. The content of each page is selected, then paste in a new document (with the XTransferable API), and stored to Url with the HTML filter.

The script is ok but crashes with this problem of tables in first position (and maybe last).

I think I have to use cursors in a different way, but don't figure out how to do it. As all the result pages lengths don't have to be exact the same, we can image to scan the doc and stop after a page ending table, or before a begining one.

But as I can do it with my mouse on my desktop (selecting content of pages with tables), I think there's a way do it with a script, no ?

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 10:20 pm
by RoryOF
sam_fr wrote:I think I have to use cursors in a different way, but don't figure out how to do it. As all the result pages lengths don't have to be exact the same, we can image to scan the doc and stop after a page ending table, or before a begining one.
I think you will have to dive into and read Andrew Pitonyak's book on OO Macro Programmin, available for free download at
http://www.pitonyak.org/oo.php

He deals with the best methods of using OO cursors.

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 10:29 pm
by RoryOF
Do the recipients need to edit the files? If not, you could /File /Export as PDF, and use a macro to make two page PDFs, or drive the conversion from a batch file on the command line.

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 10:36 pm
by sam_fr
Yes I found the AndrewMacro.odt and follow it. But even following some of its examples I can't have it to go.

For example, reading his 7.2 example and chapter 8, I had this idea to move the cursor after the table. The error is still here ! If the table is not at the very beginning, it works ! :-(

Code: Select all

Sub casse()
VisibleCursor = ThisComponent.CurrentController.ViewCursor
VisibleCursor.jumpToPage(1)
      
  do while not (isNull(VisibleCursor.TextTable) or isEmpty(VisibleCursor.TextTable)):
        VisibleCursor.goDown(1, False)
  loop
  
      With VisibleCursor   
         .jumpToEndOfPage
         oRangeFinPage = .end
         .jumpToStartOfPage
         .gotoRange(oRangeFinPage, true)
      End With
      
End Sub
I can see the visiblecursor is really moved after the table, so there's something else I don't know...

Re: Select a whole page that begins with a table

Posted: Mon May 13, 2019 10:51 pm
by sam_fr
Unfortunately I can't use le PDF export filter. It is very easy to split PDF files, but the PDF to HTML export is very bad with the poppler utils, and the pdf2htmlEX soution doesn't create responsive or adaptative HTML that can fits on poor devices (it does quite the same as the PDF.js)

OpenOffice does a very good job at exporting to html (images positions, tables embedding...)

Re: Select a whole page that begins with a table

Posted: Tue May 14, 2019 12:16 am
by JeJe
Try this... if the page starts with a table it inserts a para enabling full selection of the page. It may need adaptation for the added line putting your page layouts out a little and doesn't handle page starts in the middle of a table.

Code: Select all


sub test()
selectpage 2
end sub

Sub Selectpage(pageno)
   dim dispatcher as object,OVC
   doc =thiscomponent
   OVC =doc.currentcontroller.viewcursor
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
   with oVC
      .JumpToPage(pageno,false)
      if Not IsEmpty(.TextTable) then 
          res =.goleft(1,false)
         dispatcher.executeDispatch(Doc.CurrentController.Frame, ".uno:InsertPara", "", 0, Array())
          end if
	     dispatcher.executeDispatch(Doc.CurrentController.Frame, ".uno:GoToEndOfPageSel", "", 0, Array())
   end with
End Sub


Re: Select a whole page that begins with a table

Posted: Tue May 14, 2019 4:35 pm
by sam_fr
Thanks

I will explore this solution.

Maybe you know how to fix LineSpacing with dispatch commands I'm not familiar with ?

This won't work, adding the dispatch command right after the .uno:InsertPara one :

Code: Select all

dim args(1) as new com.sun.star.beans.PropertyValue
args(0).Name = "LineSpacing.Mode"
args(0).Value = 3
args(1).Name = "LineSpacing.Height"
args(1).Value = 1 ' "2pt"
dispatcher.executeDispatch(Doc.CurrentController.Frame, ".uno:LineSpacing", "", 0, args1())
I didn't find a ParaLineSpacing in the documented dispatch commands.

But this works doing it "manually" :

Code: Select all

v = oPar.ParaLineSpacing
'v.Mode = com.sun.star.style.LineSpacing.FIX
v.Mode = 3
v.Height = "2pt"
oPar.ParaLineSpacing = v
Regards

Re: Select a whole page that begins with a table

Posted: Tue May 14, 2019 5:52 pm
by UnklDonald418
When a text table is at the very top of a page, the problem begins with

Code: Select all

VisibleCursor.jumpToStartOfPage
which places the cursor in the first cell of the text table, but oRangePageEnd is outside the text table, thus the error.
Here is a version that tests if the cursor is in a text table then it inserts a blank paragraph before the text table.

Code: Select all

Sub ko()
Dim VisibleCursor  as object
Dim  oRangePageEnd 
 
VisibleCursor = ThisComponent.CurrentController.ViewCursor

VisibleCursor.jumpToPage(1)
VisibleCursor.jumpToStartOfPage
REM test if VisibleCursor is inside a text table
  if isempty(VisibleCursor.TextTable) = FALSE  then
      InsertParBeforeTable()   'VisibleCursor is inside text table so insert a blank paragraph before table
  end if
 VisibleCursor.jumpToEndOfPage
 oRangePageEnd = VisibleCursor.end
 VisibleCursor.jumpToStartOfPage
 VisibleCursor.gotoRange(oRangePageEnd, true)
End Sub

REM "Listing 7.5: Insert a new paragraph before a text table" from "Useful Macro Information" by Andrew Pitonyak
Sub InsertParBeforeTable
Dim oTable
Dim oText
Dim oPar
oTable = ThisComponent.getTextTables().getByIndex(0)
oText = ThisComponent.getText()
oCurs = oText.createTextCursor()
oPar = ThisComponent.createInstance("com.sun.star.text.Paragraph")
oText.insertTextContentBefore ( oPar, oTable )

End Sub
There is a potential problem. If the page has no space at the bottom, then inserting a blank paragraph will cause the last line on the page to be pushed onto a new page, and only that line will be selected.

Re: Select a whole page that begins with a table

Posted: Tue May 14, 2019 9:18 pm
by JeJe
There's a .uno:LineSpacing command:

https://wiki.documentfoundation.org/Dev ... swslots.29

Things are usually better doing things directly - the Dispatch is useful for the times when that doesn't work but the Dispatch call does.

Re: [Solved] Select a whole page that begins with a table

Posted: Thu May 16, 2019 1:32 pm
by sam_fr
OK. Thanks you all.