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

insertDocumentFromURL without pagebreak ???

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


Joined: 20 Jun 2005
Posts: 11

PostPosted: Wed Dec 07, 2005 2:33 am    Post subject: insertDocumentFromURL without pagebreak ??? Reply with quote

Hello,

i use the following code to include a MS Word File into a exists OpenOffice Writerfile, but before the included information openoffices do a pagebreak. has some a idea to tell openoffices to disables the pagebreak ???? Or must a set spezial Properties ????

Thanks
VortexAudio2001


Code:

Sub DateiOeffnen

dim args1(1) as new com.sun.star.beans.PropertyValue
dim url as String

args1(0).Name = "FilterName"
args1(0).Value = "<Alle Formate>"

url=converttourl("C:\test.doc")
oDoc = ThisComponent

dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

oFrame = oDoc.CurrentController.Frame
oText = oDoc.getText()

oCur = oText.createTextCursorByRange(oDoc.getCurrentController().getViewCursor().getEnd())
oCur.insertDocumentFromURL(url, Array())

end sub
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Wed Dec 07, 2005 9:59 am    Post subject: Reply with quote

Worst case, insert the document and then go back and remove the page break. Just remember the location where the document was inserted and then go look for the page break there.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
vortexaudio2001
General User
General User


Joined: 20 Jun 2005
Posts: 11

PostPosted: Thu Dec 08, 2005 3:57 am    Post subject: Reply with quote

i have the positions, but i can“t do delete the page break. Also to replace the page break won“t work.

Code:

Sub Cleanup(oDoc, oRem)
oRem.setSearchString("\n&")
oRem.setReplaceString("")
oDoc.replaceAll(oRem)
End Sub


Has someone another idea ??? or a little code example ???

Thanks VortexAudio2001[/quote]
Back to top
View user's profile Send private message
maxime.labelle
General User
General User


Joined: 30 Mar 2005
Posts: 30

PostPosted: Thu Dec 08, 2005 6:31 am    Post subject: Reply with quote

I have had the same problem.

Note that a paragraph break is sometimes inserted, sometimes not. For instance, if you insert a document starting with a TextTable, then a paragraph break will be inserted. If you insert a document starting with a picture, no paragraph breaks will be inserted!

The solution I used, because I could not succeed in recording the position of a TextCursor, was to :

1. insert a paragraph break at the position of the text view cursor
2. insert a bookmark wit a well known name at this position
3. perform the insertion of the other document
4. do the cleanup

The cleanup is the tricky part : because you cannot move the TextCursor inside a (potential) TextTable, you have to use the TextViewCursor. So

4a. position the TextViewCursor at the bookmark anchor
4b. remove the extra paragraph break using the dispatcher ".uno:Delete"
4c. re-position the TextViewCursor at the end (by using TextCursor.gotoEnd() and TextViewCursor.gotoRange(TextCursor)

Hope this helps ! But what a mess...

Maxime.
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Thu Dec 08, 2005 1:29 pm    Post subject: Reply with quote

Quote:
i have the positions, but i can“t do delete the page break. Also to replace the page break won“t work.


I assume that this means that you do not know how to delete a page break. I have examples of this in my free macro document and in my book.
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
vortexaudio2001
General User
General User


Joined: 20 Jun 2005
Posts: 11

PostPosted: Thu Dec 08, 2005 1:55 pm    Post subject: Reply with quote

Hi,

i chance my code, but i can“t get the position of the textcursor at the bookmark. Can you post your code soo i can look at it.

Thankx
VortexAudio2001

Code:


Sub DateiOeffnen

dim args1(1) as new com.sun.star.beans.PropertyValue
dim url as String

args1(0).Name = "FilterName"
args1(0).Value = "<Alle Formate>"

url=converttourl("C:\test.doc")
oDoc = ThisComponent

dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

oFrame = oDoc.CurrentController.Frame
oText = oDoc.getText()

oCur = oText.createTextCursorByRange(oDoc.getCurrentController().getViewCursor().getStart())

oText.insertControlCharacter(oCur,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,FALSE)
rem oText.insertString(oCur,"tr909",FALSE)

oBookmark = oDoc.createInstance("com.sun.star.text.Bookmark")
oBookmark.Name = "TTT"
oDoc.Text.insertTextContent(oCur,oBookmark, True)
oCur.insertDocumentFromURL(url, Array())

myBookmark = oDoc.getBookmarks().getByName("TTT")
myAnchor = myBookmark.getAnchor()
myCursor = myAnchor.getText().createTextCursorByRange(myAnchor)

dispatcher.executeDispatch(oFrame, ".uno:Delete", "", 0, Array())
end sub


Back to top
View user's profile Send private message
maxime.labelle
General User
General User


Joined: 30 Mar 2005
Posts: 30

PostPosted: Fri Dec 09, 2005 12:24 am    Post subject: Reply with quote

Hi,

The following code is used to insert a document that starts with a text table.
Without this code, there would be an extra paragraph break inserted before the text table.

Code:

Option Explicit

Public Sub Main
   PreProcessInsertFile
   PerformInsertFile ConvertToURL("c:\temp\texttable.odt")
   PostProcessInsertFile
End Sub

Private Sub PreProcessInsertFile

   Dim oTextViewCursor As Object
   Set oTextViewCursor = ThisComponent.CurrentController.ViewCursor
   
   Dim oText As Object
   Set oText = ThisComponent.Text
   
   Dim oTextCursor As Object
   Set oTextCursor = oText.CreateTextCursorByRange(oTextViewCursor.Start)
   oTextCursor.GotoRange oTextViewCursor.End, False

   ' insert an extra paragraph breaks
   
   oText.InsertControlCharacter oTextCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False
   
   ' insert a well-known bookmark
   
   Dim oBookmark As Object
   Set oBookmark = ThisComponent.CreateInstance("com.sun.star.text.Bookmark")
   oBookmark.Name = "PreInsertBookmark"
   
   oTextCursor.GoLeft 1, False
   oText.InsertTextContent oTextCursor, oBookmark, False

End Sub

Private Sub PerformInsertFile(ByVal sURL As String)

   Dim oTextViewCursor As Object
   Set oTextViewCursor = ThisComponent.CurrentController.ViewCursor

   Dim oDocumentInsertable As Object
   Set oDocumentInsertable = ThisComponent.Text.CreateTextCursorByRange(oTextViewCursor)
   
   ' the argument to InsertDocumentFromURL *must* be an empty sequence
   
   oDocumentInsertable.InsertDocumentFromURL sURL, Array()

End Sub

Private Sub PostProcessInsertFile

   ' obtain the well-known bookmark
   
   Dim oBookmark As Object
   Set oBookmark = ThisComponent.Bookmarks.getByName("PreInsertBookmark")
   
   ' find the position of the bookmark and remove the extra
   ' paragraph break. since we do not know how to move a
   ' TextCursor in a TextTable, we need to manipulate the
   ' TextViewCursor

   Dim oTextViewCursor As Object
   Set oTextViewCursor = ThisComponent.CurrentController.ViewCursor
   oTextViewCursor.GotoRange oBookmark.Anchor, False

   ' it is not possible to span a range across a selection that only consists of
   ' a sequence of paragraph breaks, so we need to simulate the Delete key
   ' using the dispatcher

   Dim oFrame As Object
   Set oFrame = ThisComponent.CurrentController.Frame

   Dim oDispatcher As Object
   Set oDispatcher = CreateUnoService("com.sun.star.frame.DispatchHelper")
   
   oDispatcher.ExecuteDispatch oFrame, ".uno:Delete", "", 0, Array()

   ' we need to reposition the ViewCursor to the end of the document
   ' otherwise, the subsequent insertions will occur right in the middle !

   Dim oTextCursor As Object
   Set oTextCursor = ThisComponent.Text.CreateTextCursor()
   oTextCursor.GotoEnd False

   oTextViewCursor.GotoRange oTextCursor, False

End Sub
Back to top
View user's profile Send private message
vortexaudio2001
General User
General User


Joined: 20 Jun 2005
Posts: 11

PostPosted: Fri Dec 09, 2005 4:09 am    Post subject: Reply with quote

Great it Works.

Thanks for your help Maxime !!!

VortexAudio2001
Back to top
View user's profile Send private message
n0mer
General User
General User


Joined: 20 Mar 2006
Posts: 46

PostPosted: Mon Sep 11, 2006 8:17 am    Post subject: Reply with quote

maxime.labelle wrote:
Hope this helps ! But what a mess...

Some idea.
Actually page break is no more than paragraph property.
When i do insert document, get pagebreak and manually edit this, all i should do is to modify first paragraph (uncheck "Text Flow"->"Breaks"->"Insert" checkbox).
After performing insertion we _already_ have a cursor with the whole inserted text.
Why not use it to navigate to the first paragraph and swith "Breaks" off?
Back to top
View user's profile Send private message Visit poster's website
claudiobosticco
Newbie
Newbie


Joined: 23 Jan 2007
Posts: 1
Location: Turin, Italy

PostPosted: Wed Jan 24, 2007 3:20 am    Post subject: Reply with quote

Hello, I'm new to OOo world, has someone a code snippet to remove page breakx in Java?

TIA
Back to top
View user's profile Send private message
Dodo
Guest





PostPosted: Tue Feb 19, 2008 9:00 am    Post subject: Reply with quote

Hey guys. Smile

I tried to convert those two code snippets of macros to java, cause that's the language I am programming in.

I used a bookmark in my writer template and the method does not throw any exceptions.

The funny thing is, it just does not work, that means: The page break remains untouched. Smile

Code:

private void embedSubDoc(String fileName, String bookmarkName,    boolean removePageBreak) throws BflexxException {

  try {

    XBookmarksSupplier bookmarksSupplier = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class, iTextDocument);
    XTextRange iTextRange = apiGetBookmarkTextRange(bookmarksSupplier, bookmarkName);      
    XText textContent = iTextRange.getText();

    // Create a text cursor from the iTextRange.
    XTextCursor iTextCursor = textContent.createTextCursorByRange(iTextRange);

    XDocumentInsertable iDocumentInsertable = (XDocumentInsertable) UnoRuntime.queryInterface(XDocumentInsertable.class, iTextCursor);

    XMultiServiceFactory iMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, iTextDocument);
    // inserting and retrieving a bookmark
    Object bookmark = iMSF.createInstance("com.sun.star.text.Bookmark");
    // name the bookmark
    XNamed xNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, bookmark);
    xNamed.setName("iDeleteBookmark");
    // get XTextContent interface
    XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, bookmark);
    // insert bookmark at the end of the document
    // instead of mxDocText.getEnd you could use a text cursor's
    // XTextRange interface or any XTextRange
    iTextDocument.getText().insertTextContent(iTextRange, xTextContent, false);

    // That was the bookmark we later need to delete the page break.

    // Insert the document "fileName" with the iDocumentInsertable.
    iDocumentInsertable.insertDocumentFromURL(fileName, null);

    // After the insert: remove the page break
    XController xController = iTextDocument.getCurrentController();
    XFrame xFrame = xController.getFrame();
    XDispatchProvider xDispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, xFrame);

    Object dispatchHelper = iMultiServiceFactory.createInstance("com.sun.star.frame.DispatchHelper");
    XDispatchHelper helper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, dispatchHelper);
         
    XBookmarksSupplier bookmarksSupplier2 = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class, iTextDocument);

    com.sun.star.util.XRefreshable iRefreshable = (com.sun.star.util.XRefreshable) UnoRuntime.queryInterface(com.sun.star.util.XRefreshable.class, bookmarksSupplier);
    iRefreshable.refresh(); // Do I HAVE to do that?

    Object iDeleteBookmark = bookmarksSupplier2.getBookmarks().getByName("iDeleteBookmark");
    XTextContent iBookmarkContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, iDeleteBookmark);
    XTextRange iBookmarkRange = iBookmarkContent.getAnchor();
    XTextRange iRange = iBookmarkRange.getText().createTextCursorByRange(iBookmarkRange);

    helper.executeDispatch(xDispatchProvider, ".uno:Delete", "", 0, new PropertyValue[] {});


I am working on that method since this morning, perhaps I just did not see the last step. You know, sometimes you get blind, looking at the same code all the time. Smile

Hope, someone outthere see what I did not.

Have a nice evening (or morning or what so ever *g*).
Greetz,
Dodo
Back to top
rafaelcds
Newbie
Newbie


Joined: 23 May 2012
Posts: 1

PostPosted: Wed May 23, 2012 10:45 am    Post subject: Reply with quote

The method insertDocumentFromURL does not create a page break for extensions .odt and .rtf
So, my solution to insert/paste .odt and .rtf documents using the XTransferable clipboard interface and remove the page break generated by the LibreOffice is:

Code:
   public void inserirDocumento(OOoBean bean, boolean removerQuebraPagina) {
      try {
         WriterDocument writerOrigem = new WriterDocument(bean);
         XTextRange inicio = writerOrigem.getText().getStart();
         XTextRange fim = writerOrigem.getText().getEnd();

         inserirParagrafo();
         XTransferable transferable = writerOrigem.getConteudoPorRange(inicio, fim);
         setTransferable(transferable);

         if (removerQuebraPagina) {
            // !Importante
            // SoluĆ§Ć£o para remover a quebra de pĆ”gina inserida pelo LibreOffice:
            // 1) Guardar a posiĆ§Ć£o do fim do texto e mover o cursor para o inĆ­cio da pĆ”gina criada
            XTextViewCursor textViewCursor = getTextViewCursor();
            XTextRange posicaoFimTexto = textViewCursor.getStart();
            XPageCursor pageCursor = getPageCursor(textViewCursor);
            pageCursor.jumpToStartOfPage();

            // 2) Selecionar um caractere Ć  esquerda e removĆŖ-lo, pois este representa a quebra de pĆ”gina
            textViewCursor.goLeft((short) 1, true);
            textViewCursor.setString("");

            // 3) Mover o cursor de volta para o fim da seĆ§Ć£o
            textViewCursor.gotoRange(posicaoFimTexto, false);
         }
      } catch (Exception e) {
         throw new RuntimeException(MSG_ERRO_OPEN_OFFICE, e);
      }
   }

The full class containing the method dependencies can be found at: http://pastebin.com/rasyYbRZ
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