Insert an image at the bookmark position

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
nassim1987
Posts: 9
Joined: Mon Oct 19, 2015 5:15 pm

Insert an image at the bookmark position

Post by nassim1987 »

Hello,

I develop a c++ project tha mange a OpenOffice writer documents. I want to insert an image in the bookmark position. For this I used the following program:

Code: Select all

void insertImage(std::string bookmarkName,std::string fileName)
{
	Any bookmark;
	Reference< XText > xText = this->xTextDocument->getText();
	Reference< XBookmarksSupplier > xBookmarksSupplier (this->rDocument, UNO_QUERY);
	Reference< XNameAccess > rNameAccess = xBookmarksSupplier->getBookmarks();
	bookmark = rNameAccess->getByName(OUString::createFromAscii((char*)bookmarkName.c_str()));

	Reference< XTextContent >xTextContentBookmark (bookmark,UNO_QUERY);
	Reference< XTextRange >  xTextRange = xTextContentBookmark->getAnchor();
	
	// Generate a File URL
	OUString GraphicURL;
	osl::FileBase::getFileURLFromSystemPath(OUString::createFromAscii((char*)fileName.c_str()),GraphicURL);

	Reference<XMultiServiceFactory> rMSF (xTextDocument,UNO_QUERY);
	Reference <XPropertySet> rGraphic ( rMSF->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.GraphicObject" ))), UNO_QUERY );

	rGraphic->setPropertyValue(OUString::createFromAscii("GraphicURL"),makeAny(GraphicURL));
	rGraphic->setPropertyValue( OUString::createFromAscii("Width"), Any((sal_Int32 ) 1500 ) );
       rGraphic->setPropertyValue( OUString::createFromAscii("Height"), Any((sal_Int32 ) 1500 ) );

	Reference <XTextContent> xTextContent (rGraphic,UNO_QUERY);
       xText->insertTextContent(xTextRange, xTextContent,false);

}

This function put the image in the center of the line although my bookmark is in the begining of the line. In some case of my project, i have to place three image in the same line. I need for this three bookmarks and I want to put each image at the position of the appropriate bookmark. The problem all the images are placed in the center and superposed one each other.

I want to know please il there is a solution for this problem or if there is something to add to the function

Thank you.
OpenOffice 4.1.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: insert an image at the bookmark position

Post by RoryOF »

There is an existing extension to put markers at the bookmark positions.
Visible Bookmarks

I do not know if this will run on OO 4.1.1, but if you are working with macros you should be able to sort that out and to note the mechanism used to insert the marker at the correct location. Substituting the image(s) you require for the markers should be trivial.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
nassim1987
Posts: 9
Joined: Mon Oct 19, 2015 5:15 pm

Re: insert an image at the bookmark position

Post by nassim1987 »

Thank you, but i don't thinhk that this extension is intended for c++ program.
OpenOffice 4.1.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: insert an image at the bookmark position

Post by RoryOF »

Look at the code and follow the logic. Then adapt that to C++.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply