Hi,

On Mon, Aug 05, 2013 at 07:21:28PM +0300, K.Misha wrote:
> I have a problem with inserting image into calc document!
> 
> I'm doing it this way:
> rColProps->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("GraphicURL")),
>  makeAny(sstring));

If you want the image embedded inside the document, instead of linked,
do not use the "GraphicURL" property, use "Graphic". This property
requires an instance of com.sun.star.graphic.XGraphic. You can get one
using the service com.sun.star.graphic.GraphicProvider

See attached code.


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.
#
#**************************************************************

# On *nix can be overridden with
# make PRJ=$OO_SDK_HOME
PRJ=../../../..
SETTINGS=$(PRJ)/settings

include $(SETTINGS)/settings.mk
include $(SETTINGS)/std.mk
include $(SETTINGS)/dk.mk

# Define non-platform/compiler specific settings
APP_NAME=InsertImage
MAINTARGET=$(APP_NAME)_Example

#this should be in odk/settings/std.mk
OUT_DEPLOY=$(OUT)/deploy

OUT_APP_INC=$(OUT_INC)/$(APP_NAME)
OUT_APP_GEN=$(OUT_MISC)/$(APP_NAME)
OUT_APP_OBJ=$(OUT_OBJ)/$(APP_NAME)
OUT_APP_LIB=$(SHAREDLIB_OUT)/$(APP_NAME)
OUT_APP_BIN=$(OUT_BIN)/$(APP_NAME)
OUT_APP_DEPLOY=$(OUT_DEPLOY)/$(APP_NAME)

CXXFILES = main.cxx

OBJFILES = $(patsubst %.cxx,$(OUT_APP_OBJ)/%.$(OBJ_EXT),$(CXXFILES))


ifneq "$(DEBUG)" ""
        CC_DEFINES+=-DOSL_DEBUG_LEVEL=3
endif

# Targets
.PHONY: ALL
ALL : \
        $(MAINTARGET)

include $(SETTINGS)/stdtarget.mk

$(OUT_APP_OBJ)/%.$(OBJ_EXT) : %.cxx $(SDKTYPEFLAG)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        $(CC) $(CC_FLAGS) $(STL_INCLUDES) $(CC_INCLUDES) -I$(OUT_APP_INC) 
$(CC_DEFINES) $(CC_OUTPUT_SWITCH)$(subst /,$(PS),$@) $<

$(OUT_APP_BIN)/_$(APP_NAME)$(EXE_EXT) : $(OBJFILES)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        -$(MKDIR) $(subst /,$(PS),$(OUT_APP_GEN))
ifeq "$(OS)" "WIN"
        $(LINK) $(EXE_LINK_FLAGS) /OUT:$@ /MAP:$(OUT_APP_GEN)/$(basename 
$(@F)).map \
                $(OBJFILES) $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) 
$(SALLIB) $(STLPORTLIB)
else
        $(LINK) $(EXE_LINK_FLAGS) $(LINK_LIBS) -o $@ $(OBJFILES) \
                $(CPPUHELPERLIB) $(CPPULIB) $(SALHELPERLIB) $(SALLIB) 
$(STLPORTLIB) $(STDC++LIB) $(CPPUHELPERDYLIB) $(CPPUDYLIB) $(SALHELPERDYLIB) 
$(SALDYLIB)
ifeq "$(OS)" "MACOSX"
        $(INSTALL_NAME_URELIBS_BIN)  $@
endif
endif

$(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT) : $(OUT_APP_BIN)/_$(APP_NAME)$(EXE_EXT)
        -$(MKDIR) $(subst /,$(PS),$(@D))
        $(COPY) $(subst /,$(PS),$(BIN_DIR)/unoapploader$(EXE_EXT)) $(subst 
/,$(PS),$@)
# workaround for touch problem under Windows with full qualified paths
        make -t $@

$(MAINTARGET) : $(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT)
        @echo 
--------------------------------------------------------------------------------
        @echo Please use the following command to execute the example!
        @echo -
        @echo $(MAKE) $(APP_NAME).run
        @echo 
--------------------------------------------------------------------------------


$(APP_NAME).run: $(OUT_APP_BIN)/$(APP_NAME)$(EXE_EXT)
        cd $(subst /,$(PS),$(OUT_APP_BIN)) && $(basename $@)

.PHONY: clean
clean :
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_INC))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_GEN))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_OBJ))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_LIB))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_BIN))
        -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_DEPLOY))
/**************************************************************
 *
 * 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/sheet/XSpreadsheet.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.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';
    }
}

int main( void )
{
    try
    {
        css::uno::Reference< css::uno::XComponentContext > xContext( 
cppu::bootstrap() );

        css::uno::Reference< css::frame::XComponentLoader  > xLoader(
            xContext->getServiceManager()->createInstanceWithContext(
                OUString( RTL_CONSTASCII_USTRINGPARAM(
                              "com.sun.star.frame.Desktop" ) ), xContext ),
            css::uno::UNO_QUERY_THROW );

        css::uno::Reference < css::sheet::XSpreadsheetDocument > xDoc(
            xLoader->loadComponentFromURL(
                OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/scalc" 
) ),
                OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ),
                css::frame::FrameSearchFlag::ALL,
                css::uno::Sequence < css::beans::PropertyValue >() ),
            css::uno::UNO_QUERY_THROW );

        //----------------------------------------------------------------------
        // get the draw page

        css::uno::Reference< css::sheet::XSpreadsheets > xSpreadsheets(
            xDoc->getSheets() );
        OSL_ENSURE( xSpreadsheets.is(), "No XSpreadsheets!" );

        if ( !xSpreadsheets->hasElements() )
        {
            xSpreadsheets->insertNewByName(
                OUString( RTL_CONSTASCII_USTRINGPARAM( "Sheet 1" ) ), 0 );
        }

        css::uno::Reference< css::container::XIndexAccess > xIndexAccess(
            xSpreadsheets, css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::sheet::XSpreadsheet > xSheet(
            xIndexAccess->getByIndex( 0 ), css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::drawing::XDrawPageSupplier > 
xDrawPageSupplier(
            xSheet, css::uno::UNO_QUERY_THROW );

        css::uno::Reference< css::drawing::XDrawPage > xDrawPage(
            xDrawPageSupplier->getDrawPage(), css::uno::UNO_SET_THROW );

        //----------------------------------------------------------------------
        // create the shape

        css::uno::Reference< css::lang::XMultiServiceFactory > xDocFactory(
            xDoc, css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::drawing::XShape > xShape(
            xDocFactory->createInstance(
                OUString( RTL_CONSTASCII_USTRINGPARAM(
                              "com.sun.star.drawing.GraphicObjectShape" ) ) ),
            css::uno::UNO_QUERY_THROW );
        css::uno::Reference< css::beans::XPropertySet > xShapeProps(
            xShape, css::uno::UNO_QUERY_THROW );

        //----------------------------------------------------------------------
        // get the graphic

        // we use the URL of an image inside the images.zip that comes with AOO
        // but it can be any URL
        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;

        xShape->setPosition( css::awt::Point( 1000, 2000 ) );
        xShape->setSize( aSize100thMM );

        xShapeProps->setPropertyValue(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ),
            css::uno::makeAny( xGraphic ) );

        // add the shape
        xDrawPage->add( xShape );

    }
    catch ( const css::uno::Exception &e )
    {
        std::cerr << "Caught an exception: "
                  << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 
).getStr()
                  << '\n';
    }

    return 0;
}

Attachment: pgpBTWDkvIc2p.pgp
Description: PGP signature

Reply via email to