Hi, On Thu, Sep 19, 2013 at 09:33:34PM +0300, K.Misha wrote: > Hello! > > Can you send me some examples of using images in writer?
An image in Writer is a com.sun.star.text.TextGraphicObject, see http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/TextGraphicObject.html http://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Graphic_Objects - create a graphic object at the document factory, call createInstance passing the service name - you can insert a linked or an embedded graphic, in both cases you need to know the actual size of the image before setting the graphic object size. Use the GraphicProvider service to retrieve that information, then set the size with the "Size" property - linked images are inserted setting the "GraphicURL" property, embedded images are inserted setting the "Graphic" property; the graphic object is a text content, you have to use insertTextContent to insert it. The red warning box at http://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Graphic_Objects is outdated: Embedding of graphics *is* supported by using the "Graphic" property instead of "GraphicURL" - there are many other properties controlling the graphic object, they are documented in http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/TextGraphicObject.html http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/BaseFrameProperties.html Attached is a variation of http://markmail.org/message/cl356hyng5utkuif Regards -- Ariel Constenla-Haile La Plata, Argentina
/************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * *************************************************************/ #include <cppuhelper/bootstrap.hxx> #include <com/sun/star/awt/Size.hpp> #include <com/sun/star/awt/XToolkit.hpp> #include <com/sun/star/awt/XUnitConversion.hpp> #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/container/XIndexAccess.hpp> #include <com/sun/star/drawing/XDrawPageSupplier.hpp> #include <com/sun/star/drawing/XDrawPagesSupplier.hpp> #include <com/sun/star/frame/FrameSearchFlag.hpp> #include <com/sun/star/frame/XComponentLoader.hpp> #include <com/sun/star/graphic/GraphicType.hpp> #include <com/sun/star/graphic/XGraphicProvider.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/text/XTextCursor.hpp> #include <com/sun/star/text/XText.hpp> #include <com/sun/star/text/XTextDocument.hpp> #include <com/sun/star/text/XTextContent.hpp> #include <com/sun/star/text/ControlCharacter.hpp> #include <com/sun/star/uno/XComponentContext.hpp> #include <com/sun/star/util/MeasureUnit.hpp> #include <iostream> namespace css = ::com::sun::star; using rtl::OUString; inline bool operator!( const css::awt::Size &aSize ) { return aSize.Width == 0 && aSize.Height == 0; } static css::uno::Reference< css::graphic::XGraphic > GetGraphicFromURL( const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &rURL ) { css::uno::Reference< css::graphic::XGraphic > xGraphic; try { css::uno::Reference< css::graphic::XGraphicProvider > xGraphicProvider( rxContext->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ), rxContext ), css::uno::UNO_QUERY_THROW ); css::uno::Sequence< css::beans::PropertyValue > aMediaProps( 1 ); aMediaProps[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) ); aMediaProps[0].Value <<= rURL; xGraphic.set( xGraphicProvider->queryGraphic( aMediaProps ), css::uno::UNO_SET_THROW ); } catch ( const css::uno::Exception &e ) { std::cerr << "Caught an exception: " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << '\n'; } return xGraphic; } static void ConvertPixelTo100thMM( css::awt::Size &rSize100thMM, const css::awt::Size &aSizePixel, const css::uno::Reference< css::uno::XComponentContext > &rxContext ) { try { css::uno::Reference< css::awt::XToolkit > xToolkit( rxContext->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), rxContext ), css::uno::UNO_QUERY_THROW ); css::uno::Reference< css::awt::XUnitConversion > xUnitConversion( xToolkit->createScreenCompatibleDevice( 5, 5 ), css::uno::UNO_QUERY_THROW ); rSize100thMM = xUnitConversion->convertSizeToLogic( aSizePixel, com::sun::star::util::MeasureUnit::MM_100TH ); } catch ( const css::uno::Exception &e ) { std::cerr << "Caught an exception: " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << '\n'; } } template < typename T > css::uno::Reference< T > LoadComponent( const css::uno::Reference< css::uno::XComponentContext > &xContext, css::uno::Reference< css::frame::XComponentLoader > &xLoader, const rtl::OUString &rURL, const css::uno::Sequence < css::beans::PropertyValue > &rMediaDescriptor = css::uno::Sequence < css::beans::PropertyValue >(), const rtl::OUString &rFrame = OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), sal_Int32 nFrameSearchFlag = css::frame::FrameSearchFlag::ALL ) throw ( css::uno::Exception ) { css::uno::Reference < T > xDoc; try { if ( !xLoader.is() ) { xLoader.set( xContext->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), xContext ), css::uno::UNO_QUERY_THROW ); } xDoc.set( xLoader->loadComponentFromURL( rURL, rFrame, nFrameSearchFlag, rMediaDescriptor ), css::uno::UNO_QUERY_THROW ); } catch ( const css::uno::Exception &e ) { std::cerr << "Caught an exception: " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << '\n'; throw e; } return xDoc; } int main( void ) { try { css::uno::Reference< css::uno::XComponentContext > xContext( cppu::bootstrap() ); css::uno::Reference< css::frame::XComponentLoader > xLoader; css::uno::Reference < css::text::XTextDocument > xDoc( LoadComponent< css::text::XTextDocument >( xContext, xLoader, OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/swriter" ) ) ) ); css::uno::Reference< css::lang::XMultiServiceFactory > xDocFactory( xDoc, css::uno::UNO_QUERY_THROW ); css::uno::Reference< css::text::XTextContent > xGraphicObject( xDocFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextGraphicObject" ) ) ), css::uno::UNO_QUERY_THROW ); css::uno::Reference< css::beans::XPropertySet > xGraphicObjectProps( xGraphicObject, css::uno::UNO_QUERY_THROW ); //---------------------------------------------------------------------- // get the graphic const OUString sURL( RTL_CONSTASCII_USTRINGPARAM( "http://svn.apache.org/viewvc/openoffice/trunk/main/default_images/introabout/logo.png?revision=1388877&view=co" ) ); css::uno::Reference< css::graphic::XGraphic > xGraphic( GetGraphicFromURL( xContext, sURL ) ); if ( xGraphic->getType() == css::graphic::GraphicType::EMPTY ) return 1; // get the size of the shape css::uno::Reference< css::beans::XPropertySet > xGraphicProps( xGraphic, css::uno::UNO_QUERY_THROW ); css::awt::Size aSize100thMM; xGraphicProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Size100thMM" ) ) ) >>= aSize100thMM; if ( !aSize100thMM ) { css::awt::Size aSizePixel; xGraphicProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "SizePixel" ) ) ) >>= aSizePixel; ConvertPixelTo100thMM( aSize100thMM, aSizePixel, xContext ); } if ( !aSize100thMM ) return 1; //---------------------------------------------------------------------- xGraphicObjectProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) ), css::uno::makeAny( aSize100thMM ) ); xGraphicObjectProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ), css::uno::makeAny( xGraphic ) ); css::uno::Reference < css::text::XText > xText( xDoc->getText() ); xText->insertTextContent( xText->getStart(), xGraphicObject, sal_False ); } catch ( const css::uno::Exception &e ) { std::cerr << "Caught an exception: " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << '\n'; } return 0; }
pgpODMPwctxYj.pgp
Description: PGP signature