You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Andreas Beeker <an...@gmx.de> on 2013/10/23 01:30:34 UTC
How to save high-level objects to a OPCPackage?
Hi,
I'm currently working on that FontEmbedder discussed recently, but I'm puzzled on how to use
XMLSlideShow with OPCPackage and read-write-access.
The following is similar in what is described @ http://stackoverflow.com/questions/16108142
So, I'm opening an existing .pptx, adding a few fonts files and add a few entries to XMLSlideShow.getCTPresentation().
When I close the OPCPackage, the fonts are added, but the CTPresentation hasn't been changed.
Regarding Nicks comment, one would have to save the changes in the high-level objects, but
save(OutputStream/File) is probably not the right way, when you are working with a writable OPCPackage.
Of course I had a look at the examples, e.g. org.apache.poi.openxml4j.opc.TestPackage,
but I don't want to save it to a different file, but to the original one ...
Looking at the API, the revert() method is public, but commit() is protected - how come?
When commit() is made public and called by the user code, the whole lot is saved ... well, nearly ...
Libre Office left out the package properties, on my original test file, so I needed to create them on the fly.
So in the end, I've got something like this - please comment on how to do it right:
public static void main(String[] args) throws Exception {
OPCPackage opc = OPCPackage.open("textruns.pptx", PackageAccess.READ_WRITE);
XMLSlideShow pptx = new XMLSlideShow(opc);
// manipulate pptx and add new parts (fonts) to the package
FontEmbedder fe = new FontEmbedder(pptx);
fe.analyzeDocument();
fe.embedFonts();
pptx.commit();
// bugfix for Libre Office, which omits sometimes the package properties and the
// next call to close throws an exception otherwise
opc.getPackageProperties();
opc.close();
}
Thank you,
Andi.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: How to save high-level objects to a OPCPackage?
Posted by Andreas Beeker <an...@gmx.de>.
Sorry Yegor, but I don't get it ... as there's no no-arg write() method, you probably mean
POIXMLDocument.write(FileOuputStream), i.e. as in "save-as", which works,
but I thought OPCPackage handling is designed differently ... especially when one has
to call revert() on a read-only package.
The FontEmbedder uses the standard package function on the pptx instance,
so no custom zip stuff and the original opc instance == pptx.getPackage().
How about moving the write()-code to a new method (POIXMLDocument.onSave()),
which has to be called before the (read-write) OPCPackage is closed? ... something like this:
********** 8< 8< 8< *************
public final void write(OutputStream stream) throws IOException {
onSave();
getPackage().save(stream);
}
public void onSave() throws IOException {
//force all children to commit their changes into the underlying OOXML Package
Set<PackagePart> context = new HashSet<PackagePart>();
onSave(context);
context.clear();
//save extended and custom properties
getProperties().commit();
}
********** 8< 8< 8< *************
Andi.
On 26.10.2013 12:59, Yegor Kozlov wrote:
> Why can't use use pptx.getPackage() inside FontEmbedder? in this case the
> call of pptx.write() will commit all your changes automatically.
>
> Yegor
>
>
>
>
> On Wed, Oct 23, 2013 at 3:30 AM, Andreas Beeker <an...@gmx.de>wrote:
>
>> Hi,
>>
>> I'm currently working on that FontEmbedder discussed recently, but I'm
>> puzzled on how to use
>> XMLSlideShow with OPCPackage and read-write-access.
>>
>> The following is similar in what is described @ http://stackoverflow.com/*
>> *questions/16108142 <http://stackoverflow.com/questions/16108142>
>>
>> So, I'm opening an existing .pptx, adding a few fonts files and add a few
>> entries to XMLSlideShow.**getCTPresentation().
>> When I close the OPCPackage, the fonts are added, but the CTPresentation
>> hasn't been changed.
>>
>> Regarding Nicks comment, one would have to save the changes in the
>> high-level objects, but
>> save(OutputStream/File) is probably not the right way, when you are
>> working with a writable OPCPackage.
>> Of course I had a look at the examples, e.g. org.apache.poi.openxml4j.opc.
>> **TestPackage,
>> but I don't want to save it to a different file, but to the original one
>> ...
>>
>> Looking at the API, the revert() method is public, but commit() is
>> protected - how come?
>>
>> When commit() is made public and called by the user code, the whole lot is
>> saved ... well, nearly ...
>> Libre Office left out the package properties, on my original test file, so
>> I needed to create them on the fly.
>>
>> So in the end, I've got something like this - please comment on how to do
>> it right:
>>
>> public static void main(String[] args) throws Exception {
>> OPCPackage opc = OPCPackage.open("textruns.**pptx",
>> PackageAccess.READ_WRITE);
>> XMLSlideShow pptx = new XMLSlideShow(opc);
>>
>> // manipulate pptx and add new parts (fonts) to the package
>> FontEmbedder fe = new FontEmbedder(pptx);
>> fe.analyzeDocument();
>> fe.embedFonts();
>>
>> pptx.commit();
>> // bugfix for Libre Office, which omits sometimes the package
>> properties and the
>> // next call to close throws an exception otherwise
>> opc.getPackageProperties();
>> opc.close();
>> }
>>
>> Thank you,
>> Andi.
>>
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: How to save high-level objects to a OPCPackage?
Posted by Yegor Kozlov <ye...@dinom.ru>.
Why can't use use pptx.getPackage() inside FontEmbedder? in this case the
call of pptx.write() will commit all your changes automatically.
Yegor
On Wed, Oct 23, 2013 at 3:30 AM, Andreas Beeker <an...@gmx.de>wrote:
> Hi,
>
> I'm currently working on that FontEmbedder discussed recently, but I'm
> puzzled on how to use
> XMLSlideShow with OPCPackage and read-write-access.
>
> The following is similar in what is described @ http://stackoverflow.com/*
> *questions/16108142 <http://stackoverflow.com/questions/16108142>
>
> So, I'm opening an existing .pptx, adding a few fonts files and add a few
> entries to XMLSlideShow.**getCTPresentation().
> When I close the OPCPackage, the fonts are added, but the CTPresentation
> hasn't been changed.
>
> Regarding Nicks comment, one would have to save the changes in the
> high-level objects, but
> save(OutputStream/File) is probably not the right way, when you are
> working with a writable OPCPackage.
> Of course I had a look at the examples, e.g. org.apache.poi.openxml4j.opc.
> **TestPackage,
> but I don't want to save it to a different file, but to the original one
> ...
>
> Looking at the API, the revert() method is public, but commit() is
> protected - how come?
>
> When commit() is made public and called by the user code, the whole lot is
> saved ... well, nearly ...
> Libre Office left out the package properties, on my original test file, so
> I needed to create them on the fly.
>
> So in the end, I've got something like this - please comment on how to do
> it right:
>
> public static void main(String[] args) throws Exception {
> OPCPackage opc = OPCPackage.open("textruns.**pptx",
> PackageAccess.READ_WRITE);
> XMLSlideShow pptx = new XMLSlideShow(opc);
>
> // manipulate pptx and add new parts (fonts) to the package
> FontEmbedder fe = new FontEmbedder(pptx);
> fe.analyzeDocument();
> fe.embedFonts();
>
> pptx.commit();
> // bugfix for Libre Office, which omits sometimes the package
> properties and the
> // next call to close throws an exception otherwise
> opc.getPackageProperties();
> opc.close();
> }
>
> Thank you,
> Andi.
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.**org<us...@poi.apache.org>
> For additional commands, e-mail: user-help@poi.apache.org
>
>