Page 1 of 1

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

Posted: Wed Jun 29, 2011 3:10 pm
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

Re: remove image from file

Posted: Wed Jun 29, 2011 4:18 pm
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 ;-)

Re: remove image from file

Posted: Wed Jun 29, 2011 4:40 pm
by cornouws
just for clarity:
tested in one close to bleeding edge LibreOffice and in OOo 321
no difference

Re: remove image from file

Posted: Wed Jun 29, 2011 8:24 pm
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?

Re: remove image from file

Posted: Thu Jun 30, 2011 10:22 am
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.

Re: remove image from file

Posted: Mon Jul 04, 2011 4:57 pm
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 :?

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

Posted: Mon Jul 04, 2011 9:06 pm
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

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

Posted: Mon Jul 04, 2011 9:45 pm
by cornouws
Currently I solve the problem by rewriting my functions, using Dispatch, rather than BitmapTable.insertByName