| View previous topic :: View next topic |
| Author |
Message |
boypula Newbie

Joined: 22 Mar 2006 Posts: 4
|
Posted: Thu Mar 30, 2006 7:50 pm Post subject: Inserting images using C# |
|
|
| How do you insert an image into a text document using C#? Thanks. |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3531 Location: Hamburg, Germany
|
Posted: Sat Apr 01, 2006 6:23 am Post subject: Re: Inserting images using C# |
|
|
| boypula wrote: | | How do you insert an image into a text document using C#? |
All I can offer is a java example: | Code: | private void insertImage(String imageName, XComponent document, XTextRange bookmark) {
try {
String imageUrl;
Image image;
File imageFile = new File(imageName);
try {
imageUrl = ("file:///" + imageFile.getCanonicalPath()).replace('\\', '/');
image = ImageIO.read(imageFile);
} catch (IOException e) {
image = null;
// Handle exception
}
short hOrientation = HoriOrientation.LEFT; // Choose among HoriOrientation elements
short vOrientation = VertOrientation.TOP; // Choose among VertOrientation elements
int imageWidth;
int imageHeight;
int enlargement = 10; // Depends on the size of your image and the desired size in the document
if (image != null) {
imageWidth = (image.getWidth(null) * enlargement);
imageHeight = (image.getHeight(null) * enlargement);
image.flush();
} else {
imageWidth = 100;
imageHeight = 100;
System.out.println("Something is wrong with the image.");
}
TextContentAnchorType oTCAT = null; // Choose among TextContentAnchorType elements
WrapTextMode oWTM = null; // Choose among WrapTextMode elements
// Create graphic object
XMultiServiceFactory xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
Object graphicObject = xWriterFactory.createInstance("com.sun.star.text.GraphicObject");
// Set properties
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, graphicObject);
xPropSet.setPropertyValue("GraphicURL", imageUrl);
xPropSet.setPropertyValue("HoriOrient", new Short(hOrientation));
xPropSet.setPropertyValue("VertOrient", new Short(vOrientation));
xPropSet.setPropertyValue("Width", new Integer(imageWidth));
xPropSet.setPropertyValue("Height", new Integer(imageHeight));
xPropSet.setPropertyValue("AnchorType", oTCAT);
xPropSet.setPropertyValue("TextWrap", oWTM);
// Insert graphic object
XTextContent xGraphicObjectTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, graphicObject);
bookmark.getText().insertTextContent(bookmark, xGraphicObjectTextContent, false);
} catch (com.sun.star.uno.Exception e) {
// Handle exception
} catch (com.sun.star.uno.RuntimeException e) {
// Handle exception
}
} |
Hope it helps anyway.
With kind regards
hol.sten
Last edited by hol.sten on Wed Nov 01, 2006 11:40 am; edited 1 time in total |
|
| Back to top |
|
 |
wangxianlg Power User

Joined: 10 Aug 2004 Posts: 67
|
Posted: Tue Jul 11, 2006 11:49 am Post subject: embedding the images into the file -- v 2.0 |
|
|
Hi there,
I'm just wondering if you how to embedding the images into the file using java?
Thanks
Alan |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3531 Location: Hamburg, Germany
|
Posted: Thu Jul 13, 2006 10:59 am Post subject: Re: embedding the images into the file -- v 2.0 |
|
|
| wangxianlg wrote: | | I'm just wondering if you how to embedding the images into the file using java? |
I'm using the above posted code.
With kind regards
hol.sten |
|
| Back to top |
|
 |
aloiziops Power User

Joined: 07 Jul 2006 Posts: 52
|
Posted: Wed Nov 01, 2006 5:52 am Post subject: |
|
|
| I am tryibg to use the code posted above. But I get some error, what the type of graphicObject and xGOProps fiels. They aren't declared into the code. |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3531 Location: Hamburg, Germany
|
Posted: Wed Nov 01, 2006 11:39 am Post subject: |
|
|
| aloiziops wrote: | | I am tryibg to use the code posted above. But I get some error |
Ups!
| aloiziops wrote: | | what the type of graphicObject |
Declare it with
| Code: | | Object graphicObject; |
| aloiziops wrote: | | and xGOProps fiels. |
Change both occurences of xGOProps to xPropSet.
| aloiziops wrote: | | They aren't declared into the code. |
I fixed my code in my posting above. Thanks for mentioning.
Does it now work for you?
Regards
hol.sten |
|
| Back to top |
|
 |
aloiziops Power User

Joined: 07 Jul 2006 Posts: 52
|
Posted: Fri Nov 03, 2006 8:44 am Post subject: |
|
|
Hi hol.sten
That's ok. It works.
thanks. |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3531 Location: Hamburg, Germany
|
Posted: Fri Nov 03, 2006 10:35 am Post subject: |
|
|
| aloiziops wrote: | | That's ok. It works. |
Thanks again for mentioning.
Regards
hol.sten |
|
| Back to top |
|
 |
Juan400 Newbie

Joined: 04 Jul 2012 Posts: 2
|
Posted: Fri Jul 20, 2012 11:27 am Post subject: Re: Inserting images using C# |
|
|
| boypula wrote: | | How do you insert an image into a text document using C#? Thanks. |
Can you sharee with me the sample in C#?, if you have it |
|
| Back to top |
|
 |
|