[Solved] API for InsertGraphic?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

[Solved] API for InsertGraphic?

Post by Peter18 »

A friendly hello to anybody,

I want to insert a grafic into a Writer document. I found commands for the dispatcher but no other. In a command list I found it as global command, but no information about parameter and parent objekt.

I would like to use API instead dispatcher. Who can help???

Greetings

Peter
Last edited by Peter18 on Fri Nov 11, 2011 7:51 pm, edited 1 time in total.
OpenOffice 3.3; OpenOffice 4.1.1
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: API for InsertGraphic?

Post by FJCC »

Here is an example of inserting an image after the first paragraph in the document.

Code: Select all

oText = ThisComponent.Text
Curs = oText.createTextCursor
Curs.gotoStart(False)
Curs.gotoNextParagraph(False)
Curs.gotoEndofParagraph(False)

FileName = "C:\Documents and Settings\username\My Documents\Group and projects\folder\zoo.JPG"
FileURL = convertToURL(FileName)

objTextGraphicObject = ThisComponent.createInstance("com.sun.star.text.TextGraphicObject")
Dim objSize as New com.sun.star.awt.Size
objSize.Width = 3530
objSize.Height = 1550
objTextGraphicObject.setSize(objSize)
objTextGraphicObject.GraphicURL = FileURL
oText.insertTextContent(Curs.Start, objTextGraphicObject, false)
End sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

Re: API for InsertGraphic?

Post by Peter18 »

Hallo FJCC,

thank you for your answer. I'l try it. Is it necessary to set the size or is autosizing possiblle?

Greetings

Peter
OpenOffice 3.3; OpenOffice 4.1.1
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: API for InsertGraphic?

Post by FJCC »

As far as I know, you have to set the size. It might be possible to use the setting of some other property, such as the size of the page in the Page Style, to calculate the value of the Size of the image. We would have to know more about what you are trying to do.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

Re: API for InsertGraphic?

Post by Peter18 »

Hallo FJCC,

thank you again.

The problem is, at runtime I don't know the picture size, position or format. So I have to insert a picture somewhere in the text or at the end. If the size of the picture exceeds the page limits it has to be scaled to fit. If the picture fits, it shall be loaded with origin size. I hope this describes my problem with the size of the picture.

Greetings

Peter
OpenOffice 3.3; OpenOffice 4.1.1
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: API for InsertGraphic?

Post by Charlie Young »

Not sure if this will help, unless you do know at least the file type of the picture, but we had a couple of discussions about image sizing a few months ago at viewtopic.php?f=20&t=42794 and viewtopic.php?f=20&t=42802. If it helps any, I eventually got the c++ program to work. It may be compiled to a .dll and then referenced from a Basic program, giving the size in pixels and/or 1/100th mm.
Apache OpenOffice 4.1.1
Windows XP
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

Re: API for InsertGraphic?

Post by Peter18 »

Hallo Charlie Young, hallo FJCC,

thank you for your answers. I tried to insert a grafic without seting the size. It works!

Code: Select all

procedure OOInsertPic( OO: T_OO; Fil: String; Err: T_Error );
var
	Path: String;

begin
  OO.Graphic            := OO.Fil.createInstance('com.sun.star.text.TextGraphicObject');
  Path                  := OO.FCP.getFileURLFromSystemPath('', Fil);
  OO.Graphic.GraphicURL := Path;
  OO.Txt.insertTextContent( OO.Cur, OO.Graphic, False );
end;
But now ther is a new problem. The grafic is centered and the text flow is above and below.

To be compatible with MS-Word (I know it is poisen) I would like to get it left and test flow around.

It should be easy to find out how to do this, but all tools I tried do not work. "Xray" has a runtime error (Parameter), "MRI" starts, but I can not find infos for this problem and the "Object Inspector" is in the frame, but does not start. :( If I could see the tree of methodes and propertys it would propably easy to find the solution. I think I have to start a thred about this, but later.

So I hope you will help me.

Greetings

Peter
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: API for InsertGraphic?

Post by Charlie Young »

Peter18 wrote:Hallo Charlie Young, hallo FJCC,

thank you for your answers. I tried to insert a grafic without seting the size. It works!

Code: Select all

procedure OOInsertPic( OO: T_OO; Fil: String; Err: T_Error );
var
	Path: String;

begin
  OO.Graphic            := OO.Fil.createInstance('com.sun.star.text.TextGraphicObject');
  Path                  := OO.FCP.getFileURLFromSystemPath('', Fil);
  OO.Graphic.GraphicURL := Path;
  OO.Txt.insertTextContent( OO.Cur, OO.Graphic, False );
end;
But now ther is a new problem. The grafic is centered and the text flow is above and below.

To be compatible with MS-Word (I know it is poisen) I would like to get it left and test flow around.

It should be easy to find out how to do this, but all tools I tried do not work. "Xray" has a runtime error (Parameter), "MRI" starts, but I can not find infos for this problem and the "Object Inspector" is in the frame, but does not start. :( If I could see the tree of methodes and propertys it would propably easy to find the solution. I think I have to start a thred about this, but later.

So I hope you will help me.

Greetings

Peter
After using insertTextContent, you should be able to find the inserted object in the document's DrawPage using MRI or Xray. The object should have been aligned based on the ParaAdjust property of OO.Cur (0 = Left, 3 = Center). I have an example in c++ I think is similar to yours that I've been playing with, but I've never used Delphi, and my last foray into Pascal was about 25 years ago, so I can't be too helpful on the language specific items here. I would guess that there is some fairly straightforward way to do setPropertyValue on OO.Cur. If you can find the object in the DrawPage, you should be able to manipulate its TextWrap property as well.
Apache OpenOffice 4.1.1
Windows XP
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

Re: API for InsertGraphic?

Post by Peter18 »

Hello Charlie Young,

thank you for your answer.
Charlie Young wrote:After using insertTextContent, you should be able to find the inserted object in the document's DrawPage using MRI or Xray.
This is the problem both doesn't work. May be I made a mistake, but in the moment I can't look after it.
Peter18 wrote:It should be easy to find out how to do this, but all tools I tried do not work. "Xray" has a runtime error (Parameter), "MRI" starts, but I can not find infos for this problem and the "Object Inspector" is in the frame, but does not start..
I tried this:

Code: Select all

procedure OOInsertPic( OO: T_OO; Fil: String; Err: T_Error );
var
  Path: String;

begin
  OO.Graphic            := OO.Fil.createInstance('com.sun.star.text.TextGraphicObject');
  Path       	        := OO.FCP.getFileURLFromSystemPath('', Fil);
  OO.Graphic.GraphicURL := Path;
  OO.Cur.ParaAdjust     := 1;
  OO.Txt.insertTextContent( OO.Cur, OO.Graphic, False );
end;
But it did not work. The grafic is still centered. How can I find the propertys of the grafic object? Or have you got an example? (I can read C)

Greetings

Peter
OpenOffice 3.3; OpenOffice 4.1.1
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: API for InsertGraphic?

Post by Charlie Young »

Peter18 wrote:Hello Charlie Young,

thank you for your answer.
Charlie Young wrote:After using insertTextContent, you should be able to find the inserted object in the document's DrawPage using MRI or Xray.
This is the problem both doesn't work. May be I made a mistake, but in the moment I can't look after it.
Peter18 wrote:It should be easy to find out how to do this, but all tools I tried do not work. "Xray" has a runtime error (Parameter), "MRI" starts, but I can not find infos for this problem and the "Object Inspector" is in the frame, but does not start..
I tried this:

Code: Select all

procedure OOInsertPic( OO: T_OO; Fil: String; Err: T_Error );
var
  Path: String;

begin
  OO.Graphic            := OO.Fil.createInstance('com.sun.star.text.TextGraphicObject');
  Path       	        := OO.FCP.getFileURLFromSystemPath('', Fil);
  OO.Graphic.GraphicURL := Path;
  OO.Cur.ParaAdjust     := 1;
  OO.Txt.insertTextContent( OO.Cur, OO.Graphic, False );
end;
But it did not work. The grafic is still centered. How can I find the propertys of the grafic object? Or have you got an example? (I can read C)

Greetings

Peter
Where does OO.Cur come from? In my c code it is the Start property of ThisComponent.Text (slipping into basic lingo for the moment). I called it sText, with rTextDoc roughly equivalent to ThisComponent

Code: Select all

 
	Reference< XText > oText = rTextDoc->getText();
	Reference< XTextRange > sText = oText->getStart();
	Reference< XPropertySet > sTextPropertySet(sText, UNO_QUERY);
	sTextPropertySet->setPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("ParaAdjust")),(Any) (sal_Int16) 1); 

That sets ParaAdjust to 1, as in your code. That's Right Alignment, but no matter for illustration purposes (I'm sure you'd notice).

Skipping some gory details for the moment (how to get oGraph, etc.), I do

Code: Select all

	Reference<XTextContent> oGraphContent(oGraph, UNO_QUERY);
	oText->insertTextContent(sText,oGraphContent,sal_False)
and it aligns according to the ParaAdjust property of sText, set above.

One question, I guess, is how does Delphi handle queryInterface? Apparently more neatly than c++, where it must often be done in stages.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: API for InsertGraphic?

Post by Charlie Young »

Before we go any further, I'd better confess to a bit of carelessness. My c++ program uses a com.sun.star.drawing.GraphicObjectShape, while the example from FJCC and Peter18's Delphi program uses com.sun.star.text.TextGraphicObject, and strange alignment problems related to the latter are discussed in one of the threads I linked to above. The GraphicObjectShape seems to offer better control, but if the size is not specified beforehand, one gets a degenerate rectangle inserted, which then needs to be resized.

I'm presently going back to the TextGraphicObject to see if I can learn more about the problems with it.
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: API for InsertGraphic?

Post by Charlie Young »

I've had some luck. The differences between TextGraphicObjects and GraphicObjectShapes is a distinction between Base Frames and Drawing Shapes. To deal with the TextGraphicObject as we wish to do here, we must change its AnchorType, which may be done before insertTextContent (I assume it would work afterward as well). The AnchorType, when the object is created, defaults to com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH. If I change it to com.sun.star.text.TextContentAnchorType.AT_CHARACTER, then the inserted graphic does go according to the ParaAdjust property. I would guess in Delphi, assuming the standard method of specifying OOo enums, you would need

Code: Select all

OO.Graphic.AnchorType  := com.sun.star.text.TextContentAnchorType.AT_CHARACTER;
Since I have it handy, the c++ looks like

Code: Select all

	Reference<XTextContent> oGraphContent(oGraph, UNO_QUERY);
	Reference< XPropertySet > oGraphContentProps(oGraphContent,UNO_QUERY);
	oGraphContentProps->setPropertyValue(OUString::createFromAscii("AnchorType"),(Any) com::sun::star::text::TextContentAnchorType_AS_CHARACTER);
and while I'm at it, oGraph is from the following, where sURL is a char * containing the path and file name of the image.

Code: Select all


	Reference<XMultiServiceFactory> docFactory(xcomponent, UNO_QUERY);	
	Reference<XInterface> GraphicObjectShape = docFactory->createInstance(OUString::createFromAscii("com.sun.star.text.TextGraphicObject"));
	Reference<XShape>  oGraph(GraphicObjectShape, UNO_QUERY);
	Reference< XPropertySet > gShapeProps(oGraph,UNO_QUERY);
	
	Any a;
	OUString aOU = OUString::createFromAscii(sURL);
	
	a <<= aOU;
	
	gShapeProps->setPropertyValue(OUString::createFromAscii("GraphicURL"),a);
	gShapeProps->setPropertyValue(OUString::createFromAscii("VertOrient"),(Any) (sal_Int8) 1);
 Edit: My considerations above are based on working with a blank document before inserting the object. Experimenting with an existing document, I'm finding a bewildering array of possibilities, which I am now exploring. One is to leave the AnchorType AT_PARAGRAPH and use the HoriOrient property of oGraph to adjust the position, which seems to override the ParaAdjust setting. Stay tuned... 
Apache OpenOffice 4.1.1
Windows XP
Peter18
Posts: 102
Joined: Thu May 12, 2011 1:01 pm

Re: API for InsertGraphic?

Post by Peter18 »

Hello Charlie Young,

thank you for your hard work! Sorry I could not look to the board the last days. Your links to "Base Frames and Drawing Shapes" and "HoriOrient property" are good. With

Code: Select all

OO.Graphic.HoriOrient := 0; 
it works.

"OO.Cur" is set to a Bookmark by an other procedure.

Your answers have been very very helpfull, thank you again! :D

Greetings

Peter
OpenOffice 3.3; OpenOffice 4.1.1
Post Reply