[Solved - partly] Removed image still in the file container

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

[Solved - partly] Removed image still in the file container

Post by cornouws »

Hi,

Placing (plus embedding) images in Writer with API works OK.
Also removing them from the Drawpage is no prob. (various macro's to do that).

However, the image remains in the file container. So the file size is not reduced after removing some large images. And that was the intention ;-)

I thought I'dd once seen how to actually remove graphic from the file, but cannot find where/how.
Anyone has an example or knows which service/.. is involved to do this?

Thanks
Last edited by cornouws on Thu Jul 07, 2011 9:43 pm, edited 3 times in total.
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: remove image from file

Post by cornouws »

Hmm, there seems to be smthing strange with images that I insert with code.
When I remove those with a function, the images are not removed from the file (physical)
But when I add images by hand, and remove them with a function, these indeed are actually removed from the file.

Adding image is done basically as follows:

Code: Select all

oBitmaps = ThisComponent.createInstance( "com.sun.star.drawing.BitmapTable" )
	sURL = ConvertToURL(sInternalName)
	oBitmaps.insertByName(sInternalName, sURL)
	sGraphicUrl = oBitmaps.getByName(sInternalName)

 	oGraph = oDoc.createInstance("com.sun.star.text.TextGraphicObject")
	oGraph.GraphicURL = sGraphicUrl
	' and set a lot more properties
	oText.insertTextContent(oCursor, oGraph, True)
Function to remove:

Code: Select all

	oCon = ThisComponent.getCurrentController()
	oDrawPage = ThisComponent.GetDrawpage
	oFrame = ThisComponent.CurrentController.Frame
	oDisp = createUnoService("com.sun.star.frame.DispatchHelper")
	If oDrawPage.Count > 0 then
		For n = oDrawPage.count -1 to 0 step -1
			oCon.select(oDrawPage.getByindex(n))
			oDisp.executeDispatch(oFrame, ".uno:Delete", "", 0, Array()) 
		Next
	End If
So obviously I'm puzzled ;-)
Last edited by cornouws on Wed Jun 29, 2011 4:42 pm, edited 1 time in total.
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: remove image from file

Post by cornouws »

just for clarity:
tested in one close to bleeding edge LibreOffice and in OOo 321
no difference
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: remove image from file

Post by rudolfo »

Hm, did convert embedded images into link images some time ago with a rather rude method: I unzipped the embedded images from the .odt zip archive and modified the link attributes in the content.xml file to point to the files outside of the archive in the file system. Initially I did manually remove the .jpeg and .png files in the image subdirectory in the archive. But I think since OpenOffice 3.1 this was no longer necessary. But the automatic removal in writer required me to open the manually modified .odt zip archive again in writer (which I usually did anyway to see if my modifications were without typos and the image files were really found on the filesystem and displayed). Then with the next save in Writer the image files were removed from the .odt file.

So maybe it helps if you add an additional cycle to your macro code where you re-open and save the file?
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: remove image from file

Post by cornouws »

Hi Rudolfo,
Thanks for the reply.
rudolfo wrote:So maybe it helps if you add an additional cycle to your macro code where you re-open and save the file?
Not yet tried that - I would expect that doing the same manually have the desired affect too, and it hansn't :-\

Will have a look in the zipped file to see what the difference is between manually added graphics and graphics added with the macro.
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: remove image from file

Post by cornouws »

cornouws wrote:Will have a look in the zipped file to see what the difference is between manually added graphics and graphics added with the macro.
Hmm, there is no difference.

I also tried some other possibilities, e.g. when placing the graphic using the cursor in a table, at a bookmark ..
Still, there is basically no difference in how the graphic is mentioned in the xml file, nor how it is place in the Pictures folder (of course) ...
So I'm really puzzled, why a graphic that I add by code, will not be removed from the file, and a graphic in the same document that is added by hand, will be removed :?
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: Removed image with macro .. still in the file container

Post by cornouws »

So found the difference.
When embedding the image with use of
oBitmaps = ThisComponent.createInstance( "com.sun.star.drawing.BitmapTable" )
oBitmaps.insertByName(sInternalName, sURL)

the next is added to the manifext.xml:
<manifest:file-entry manifest:media-type="application/binary" manifest:full-path="layout-cache"/>

Embedding an image with a dispatchHelper, does not have that effect.
In the latter situation, removing an image with code indeed removes it from the file container

So now
- manually changing the manifext.xml and
- also removing layout-cache from the file container
- and then removing an image with code ...
Nope, still the image is in the file :-\

So, maybe I've found the direction for the solution, but by all means not yet the solution
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: Removed image with macro .. still in the file container

Post by cornouws »

Currently I solve the problem by rewriting my functions, using Dispatch, rather than BitmapTable.insertByName
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
Post Reply