[Solved] [C++] Insert image, footer/header, footnote and TOC
Posted: Sat Jul 19, 2014 5:52 pm
Hi,
My C++ program include many function copy from web (reference are given).
With this program I can write text, frame and table in an office document. I don't know how to insert an image. In The method used to insert image I have got an exception when code reach xBitmap->getByName. May be somebody can give me some idea to find my mistake.
Method is:
My source code is :
My C++ program include many function copy from web (reference are given).
With this program I can write text, frame and table in an office document. I don't know how to insert an image. In The method used to insert image I have got an exception when code reach xBitmap->getByName. May be somebody can give me some idea to find my mistake.
Method is:
Code: Select all
void AOO_LO::InsererImage(char *nomFichier)
{
//
// Biblio : https://www.mail-archive.com/prog@fr.openoffice.org/msg03538.html
//
char aFileName[200]; // nom de l'image avec chemin complet
char aStr[1024];
OUString sGraphicUrl, anOUString;
// Création d'un objet graphique
Reference <XPropertySet> xGraphic ( aoo_loMSF->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.GraphicObject" ))), UNO_QUERY );
strcpy(aFileName, nomFichier);
sprintf (aStr, "file://%s", aFileName); // chemin + nom de l'image à insérer
sGraphicUrl = OUString (aStr, (sal_Int32)strlen(aStr),RTL_TEXTENCODING_ISO_8859_1 );
// Créer une image dans le document pour ne plus avoir de lien avec le fichier
Reference <XNameContainer> xBitmap(aoo_loMSF->createInstance(OUString(
RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.BitmapTable" ))), UNO_QUERY );
sprintf (aStr, "ID017"); // création d'une bitmap dans le document avec un nom au choix
xBitmap->insertByName(createStr(aStr), makeAny(sGraphicUrl));
// remplissage des pixels de la bitmap avec ceux de l'image à intégrer et intégration dans le doc
try {
// récupération de l'URL de l'image intégrée dans le document (URL créée automatiquement par OO)
xBitmap->getByName(createStr(aStr)) >>= anOUString; // EXCEPTION DECLENCHEE
xGraphic->setPropertyValue(createStr("GraphicURL"),
makeAny(anOUString));
}
catch (Exception e)
{
int i=0;
}
}
Code: Select all
#include <stdio.h>
#include <wchar.h>
#include <sal/main.h>
#include <cppuhelper/bootstrap.hxx>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/process.h>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/drawing/XShapes.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XTextFrame.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XTextGraphicObjectsSupplier.hpp>
#include <com/sun/star/awt/XStyleSettingsSupplier.hpp>
#include <com/sun/star/table/XTableRows.hpp> // Pour les tableaux
#include <com/sun/star/container/XIndexAccess.hpp> // Pour les tableaux
#include <com/sun/star/container/XNameContainer.hpp> // Pour les tableaux
#include <string.h>
#include <com\sun\star\io\xinputstream.hpp>
#include <com/sun/star/text/ControlCharacter.hpp>
#include <com/sun/star/text/TextContentAnchorType.hpp>
using namespace rtl;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::registry;
using namespace com::sun::star::drawing;
using namespace cppu;
using namespace com::sun::star::awt;
using namespace com::sun::star::graphic;
using namespace com::sun::star::text;
using namespace com::sun::star::table;
using namespace com::sun::star::container;
using namespace com::sun::star::io;
using namespace com::sun::star::awt;
// handy cause OUStrings are very often used in the API
#define createStr(x) (OUString::createFromAscii(x))
class AOO_LO {
private :
void Init(){
xComponentContext=NULL;rServiceManager=NULL;xComponentLoader=NULL;
composantWriter=NULL;xTextDoc=NULL;xCurseurTexte=NULL;};
public :
AOO_LO(void);
~AOO_LO(void){ composantWriter->dispose();};
void Connection();
void LancerWriter();
void AjouterTexte(char * ,int pos=-1);
void ChangerStyle(char *styleName);
void SauverDocument(char *nom,char *filtre);
void InsererCadre (char *texte, int largeur,int hauteur);
void InsererImage(char *);
// Manipulationde tableau
Reference <XTextTable> InsererTableau(int nbLig,int nbCol);
void ChangerCouleurLigne(Reference <XTextTable> xTable,int indLig,int rgb);
void ChangerCouleurTableau(Reference <XTextTable> xTable,int rgb);
void ChangerCouleurTexteCellule(Reference <XTextTable> ,char *ref,int rgb);
void ChangerCouleurFondCellule(Reference <XTextTable> ,char *ref,int rgb);
void DefFormuleCellule(Reference <XTextTable> ,char *ref,char *formule);
void DefValeurCellule(Reference <XTextTable> ,char *ref,char *valeur);
void DefValeurCellule(Reference <XTextTable> ,char *ref,int);
Reference< XComponentContext > xComponentContext;
Reference< XMultiComponentFactory > rServiceManager;
Reference < XComponentLoader > xComponentLoader;
Reference<XMultiServiceFactory> aoo_loMSF;
// Pour writer
Reference< XComponent >composantWriter;
Reference < XTextDocument > xTextDoc;
Reference< XText > contenuTexte;
Reference< XTextCursor> xCurseurTexte;
};
AOO_LO::AOO_LO()
{
Init();
Connection();
}
void AOO_LO::Connection()
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer
// Biblio : https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=53902
//
xComponentContext = bootstrap();
// gets the service manager from the office
rServiceManager = xComponentContext->getServiceManager();
// Creates an instance of a component which supports the services specified by the factory
Reference < XComponentLoader > componentLoader(rServiceManager->createInstanceWithContext(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop") ),
xComponentContext ), UNO_QUERY );
xComponentLoader = componentLoader;
}
void AOO_LO::LancerWriter()
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer
//
Sequence<PropertyValue> loadProps(1);
loadProps[0].Name = OUString::createFromAscii("Hidden");
loadProps[0].Value = Any(true);
Reference< XComponent > xComponent = xComponentLoader->loadComponentFromURL(
createStr("private:factory/swriter"), OUString(RTL_CONSTASCII_USTRINGPARAM("_blank")),
0, loadProps );
composantWriter =xComponent;
Reference < XTextDocument > xTD (composantWriter,UNO_QUERY);
xTextDoc = xTD;
Reference< XText > xText = xTextDoc->getText();
contenuTexte = xText;
Reference< XTextCursor> xTextCursor = xText->createTextCursor();
xCurseurTexte = xTextCursor;
Reference<XMultiServiceFactory> oDocMSF (xTextDoc,UNO_QUERY);
aoo_loMSF=oDocMSF;
}
void AOO_LO::AjouterTexte(char *texte,int pos)
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer
//
if (pos>=0)
xCurseurTexte->goRight(pos,0); // added to move the cursor
contenuTexte->insertString(xCurseurTexte,createStr(texte),false); // save file
}
void AOO_LO::ChangerStyle(char *styleName)
{
//
// Biblio https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=68055&p=303360&hilit=style#p303360
//
Reference< XPropertySet> xCursorProps( xCurseurTexte,UNO_QUERY);
// Reference < XTextDocument > xTextDocument (xComponent,UNO_QUERY);
xCursorProps->setPropertyValue("ParaStyleName", ::Any(createStr( styleName)));
}
void AOO_LO::SauverDocument(char *nom,char *filtre)
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer
//
Reference<XStorable> rStore(composantWriter, UNO_QUERY);
Sequence<PropertyValue> storeProps(1);
storeProps[0].Name = createStr("FilterName");
storeProps[0].Value = Any(createStr(filtre));
rStore->storeAsURL(createStr(nom), Sequence < ::com::sun::star::beans::PropertyValue >());
}
void AOO_LO::InsererCadre (char *texte, int largeur,int hauteur)
{
//
// Biblio : https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Text_Frames
//
try {
Reference <XTextFrame> xFrame (aoo_loMSF->createInstance(createStr("com.sun.star.text.TextFrame")),UNO_QUERY);
Reference < XShape> xShape (xFrame,UNO_QUERY);
::com::sun::star::awt::Point *Pos = new ( ::com::sun::star::awt::Point );
::com::sun::star::awt::Size *Size = new ( ::com::sun::star::awt::Size );
Size->Width = largeur;
Size->Height = hauteur;
xShape->setSize(*Size);
Reference<XPropertySet> xFrameProps (xFrame,UNO_QUERY);
xCurseurTexte->gotoEnd( false ); // Insert a new paragraph
contenuTexte->insertControlCharacter ( xCurseurTexte,com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK, false ); // Then insert the new frame
contenuTexte->insertTextContent(xCurseurTexte, xFrame, false);
// Access the XText interface of the text contained within the frame
Reference<XText> xFrameText = xFrame->getText(); // Create a TextCursor over the
Reference<XTextCursor> xFrameCursor = xFrameText->createTextCursor(); // Insert some text into the frame
xFrameText->insertString( xFrameCursor, createStr(texte), false );
contenuTexte->insertControlCharacter ( xCurseurTexte,com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK, false );
}
catch (Exception e)
{
int i=0;
}
}
void AOO_LO::InsererImage(char *nomFichier)
{
//
// Biblio : https://www.mail-archive.com/prog@fr.openoffice.org/msg03538.html
//
char aFileName[200]; // nom de l'image avec chemin complet
char aStr[1024];
OUString sGraphicUrl, anOUString;
// Création d'un objet graphique
Reference <XPropertySet> xGraphic ( aoo_loMSF->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.GraphicObject" ))), UNO_QUERY );
strcpy(aFileName, nomFichier);
sprintf (aStr, "file://%s", aFileName); // chemin + nom de l'image à insérer
sGraphicUrl = OUString (aStr, (sal_Int32)strlen(aStr),RTL_TEXTENCODING_ISO_8859_1 );
// Créer une image dans le document pour ne plus avoir de lien avec le fichier
Reference <XNameContainer> xBitmap(aoo_loMSF->createInstance(OUString(
RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.BitmapTable" ))), UNO_QUERY );
sprintf (aStr, "ID017"); // création d'une bitmap dans le document avec un nom au choix
xBitmap->insertByName(createStr(aStr), makeAny(sGraphicUrl));
// remplissage des pixels de la bitmap avec ceux de l'image à intégrer et intégration dans le doc
try {
// récupération de l'URL de l'image intégrée dans le document (URL créée automatiquement par OO)
xBitmap->getByName(createStr(aStr)) >>= anOUString; // EXCEPTION DECLENCHEE
xGraphic->setPropertyValue(createStr("GraphicURL"),
makeAny(anOUString));
}
catch (Exception e)
{
int i=0;
}
}
Reference <XTextTable> AOO_LO::InsererTableau(int nbLig,int nbCol)
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer Tableaux dans un document Writer
//
Reference <XTextTable> xTable (aoo_loMSF->createInstance(createStr("com.sun.star.text.TextTable")),UNO_QUERY);
if (!xTable.is())
{
printf("Erreur creation XTextTable interface !\n");
return NULL;
}
xTable->initialize(nbLig, nbCol);
Reference <XTextRange> xTextRange = contenuTexte->getEnd();
Reference <XTextContent> contenuTableau (xTable,UNO_QUERY);
contenuTexte->insertTextContent(xTextRange, contenuTableau,(unsigned char) 0);
return xTable;
}
void AOO_LO::ChangerCouleurTableau(Reference <XTextTable> xTable,int rgb)
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer Tableaux dans un document Writer
//
Reference<XPropertySet> xTableProps (xTable,UNO_QUERY);
Any prop;
prop <<= (sal_Bool)false;
xTableProps->setPropertyValue(OUString::createFromAscii("BackTransparent"),prop);
prop <<= (long)rgb;
xTableProps->setPropertyValue(OUString::createFromAscii("BackColor"),prop);
}
void AOO_LO::ChangerCouleurLigne(Reference <XTextTable> xTable,int indLig,int rgb)
{
//
// Biblio : https://wiki.openoffice.org/wiki/FR/Documentation/OpenOffice_Writer Tableaux dans un document Writer
//
Any prop;
Reference<XTableRows> xTableRows = xTable->getRows();
Reference<XIndexAccess> theRows (xTableRows,UNO_QUERY);
Reference<XPropertySet> xRowProps (theRows->getByIndex((short)indLig),UNO_QUERY);
prop <<= (sal_Bool)false;
xRowProps->setPropertyValue(OUString::createFromAscii("BackTransparent"),prop);
prop <<= (long)rgb;
xRowProps->setPropertyValue(OUString::createFromAscii("BackColor"),prop);
}
void AOO_LO::ChangerCouleurTexteCellule(Reference <XTextTable> tableau,char *ref,int rgb)
{
Reference<XCell> xCell = tableau->getCellByName(createStr(ref));
Reference<XText> xText = Reference<XText>(xCell,UNO_QUERY);
Reference <XTextCursor> xTextCursor = xText->createTextCursor();
Reference<XPropertySet> oCPS(xTextCursor,UNO_QUERY);
Any prop;
prop <<= (long)rgb; // color red
oCPS->setPropertyValue(createStr("CharColor"),prop);
}
void AOO_LO::ChangerCouleurFondCellule(Reference <XTextTable> tableau,char *ref,int rgb)
{
Reference<XCell> xCell = tableau->getCellByName(createStr(ref));
Reference<XText> xText = Reference<XText>(xCell,UNO_QUERY);
Reference <XTextCursor> xTextCursor = xText->createTextCursor();
Reference<XPropertySet> oCPS(xTextCursor,UNO_QUERY);
Any prop;
prop <<= (long)rgb; // color red
// BUG oCPS->setPropertyValue(createStr("BackGroundColor"),prop);
}
void AOO_LO::DefFormuleCellule(Reference <XTextTable> tableau,char *ref,char *formule)
{
Reference<XCell> xCell = tableau->getCellByName(createStr(ref));
Reference<XText> xText = Reference<XText>(xCell,UNO_QUERY);
xCell->setFormula(createStr(formule));
}
void AOO_LO::DefValeurCellule(Reference <XTextTable> tableau,char *ref,char *texte)
{
Reference<XCell> xCell = tableau->getCellByName(createStr(ref));
Reference<XText> xText = Reference<XText>(xCell,UNO_QUERY);
Reference <XTextCursor> xTextCursor = xText->createTextCursor();
xTextCursor->setString(createStr(texte));
}
void AOO_LO::DefValeurCellule(Reference <XTextTable> tableau,char *ref,int v)
{
Reference<XCell> xCell = tableau->getCellByName(createStr(ref));
Reference<XText> xText = Reference<XText>(xCell,UNO_QUERY);
xCell->setValue(v);
}
//============================================================================
int main(int argc, char **argv)
{
sal_detail_initialize( argc, argv);
AOO_LO docLO;
docLO.LancerWriter();
docLO.AjouterTexte("mon texte",0);
docLO.ChangerStyle("Heading 1");
docLO.AjouterTexte( "Professional Experience\n\r");
docLO.ChangerStyle("Heading 2");
docLO.AjouterTexte("Title\r");
docLO.ChangerStyle("Heading 3");
docLO.AjouterTexte("Test Point 1\r");
docLO.ChangerStyle("Heading 3");
docLO.AjouterTexte("Test Point 2\r");
docLO.ChangerStyle("Heading 3");
docLO.AjouterTexte("Test Point 3\r");
docLO.InsererCadre("The first line in the newly created text frame.\nThe second line in the new text frame.",7000,2000);
/** This snippet shows the necessary steps to set a property at the
current position of a given text cursor mxDocCursor
*/
// insertion d'un tableau, d'une image
Reference <XTextTable> tableau=docLO.InsererTableau(5,6);
docLO.ChangerCouleurTableau(tableau,0x00FF00);
docLO.ChangerCouleurLigne(tableau,0,0xFFFF00);
docLO.ChangerCouleurLigne(tableau,2,0xFFFF00);
docLO.DefValeurCellule(tableau,"A1","Titre du tableau");
docLO.DefValeurCellule(tableau,"A2",17);
docLO.DefValeurCellule(tableau,"A3",14);
docLO.DefFormuleCellule(tableau,"A4","=<A2>+<A3>");
docLO.ChangerCouleurTexteCellule(tableau,"B1",0xFF0000);
docLO.ChangerCouleurFondCellule(tableau,"B1",0x707070);
docLO.DefValeurCellule(tableau,"B1","Titre 2");
docLO.DefValeurCellule(tableau,"B2",20);
docLO.DefValeurCellule(tableau,"B3",20);
docLO.DefFormuleCellule(tableau,"B4","=<B2>*<B3>");
docLO.InsererImage("F:/lo_sdk/Koala.jpg");
docLO.SauverDocument("file:///F:/lo_sdk/test.odt","MS PowerPoint 97");
return 0;
}