Page 1 of 1

Insert an image at the bookmark position

Posted: Mon Oct 19, 2015 5:55 pm
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.

Re: insert an image at the bookmark position

Posted: Mon Oct 19, 2015 6:03 pm
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.

Re: insert an image at the bookmark position

Posted: Mon Oct 19, 2015 6:23 pm
by nassim1987
Thank you, but i don't thinhk that this extension is intended for c++ program.

Re: insert an image at the bookmark position

Posted: Mon Oct 19, 2015 6:26 pm
by RoryOF
Look at the code and follow the logic. Then adapt that to C++.