Setting the license properly

Hi,

I used the code provided in the docs.
It properly finds the license file, but watermarks are still added to each slide.

I tried:

  • Keeping around a static License license variable in the class in which all Aspose.Slides functions are called.
  • Instantiating, and setting, a License object before each time I call an Aspose.Slides function.
In all my tries, I found no success and no thrown exceptions.

What could be wrong?
Thank you

Hi Daniel,

Please try this code in one of your functions for testing purpose:

try {

FileInputStream fin=new FileInputStream("d:\\Aspose.Total.Java.lic");

License lic=new License();

lic.setLicense(fin);

Presentation pres=new Presentation();

pres.write("d:\\PRES.ppt");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (AsposeLicenseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

If you still find the watermark on the output slides, it means you are using license of some other product or invalid license. You can seek help about license information from Aspose.Purchase forum here.

Using your simple test, I was able to properly register the license to produce an empty slide.



Back to my application,what is needed for it to properly register the license to work?

Here’s the approach I use:

static License license;

public static Collection getSlides(
Presentation asposePres,
SlideLibPresentation pres,
Locale locale,
SlideLibFactory slideFactory,
FileFactory fileFactory) throws Exception {

registerRuntimeLicenseIfNeeded();

final Collection slides=new ArrayList(asposePres.getSlides().size());
for (int i=1; i <= asposePres.getSlides().size(); i++) {
slides.add(
slideFactory.createSlide(
pres,
new SlideBundle(cloneSlideToNewPresentation(asposePres, i), new Long(i)),
locale,
fileFactory));
}
return slides;
}

private static void registerRuntimeLicenseIfNeeded() throws Exception {
if (license == null) {
final InputStream licenseStream=PowerpointUtils.class.getResourceAsStream(“Aspose.Slides.lic”);
try {
License license=new License();
license.setLicense(licenseStream);
PowerpointUtils.license=license;
}
catch (Exception e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
throw e;
} finally {
IOUtils.closeQuietly(licenseStream);
}
}
}

Please note that no exception is thrown; the code successfully locates the license file and load it. But the resulting presentation files are still watermarked.

I also tried calling my registerRuntimeLicenseIfNeeded() function everywhere an aspose library function is called, to no avail.

I also tried keeping local variables of the loaded license in each one of my own functions that use an aspose library function.

Hi Daniel,

I don't know what is PowerpointUtils.license in your application. However, i tested an approach that works and may be useful to you. Here is the approach:

static License lic;

public static void setPPTProps()

{

setLicProps();

Presentation pres=new Presentation();

Slide sld= pres.addTitleSlide();

TextHolder thld=(TextHolder)sld.getPlaceholders().get(0);

TextHolder thld1=(TextHolder)sld.getPlaceholders().get(1);

thld.setText("Click to add title");

thld1.setText("Click to add subtitlle");

pres.write("d:\\ppt\\titlePRES6.ppt");

}

public static void setLicProps()

{

FileInputStream fin;

try {

fin = new FileInputStream("d:\\Aspose.Total.Java.lic");

lic=new License();

lic.setLicense(fin);

} catch (AsposeLicenseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

Hi Muhammad,

Thanks for your answer. Basically, all the code I showed you was part of the PowerpointUtils class; therefore, the approach you showed me is exactly the same.

I’m left puzzled as to why this doesn’t work in my application.

Hello,

You should call setLicense() method before opening presentation file. In your code Presentation object is already created.

Yes and no. The Presentation given to getSlides(…) is created from another static method of the same class (PowerpointUtils) that owns the getSlides(…) static method. And in each one of those static methods that create a new Presentation, I start them by calling registerRuntimeLicenseIfNeeded() before they do their work.

Well, I suppose I should try setting the license before I call any one of PowerpointUtils’ methods.

You don’t need to set license every time. It can be done only once for example when your application starts. Also it’s not necessary to create static License object.

In case PowerpointUtil methods work with Aspose.Slides then you definitely should call setLicense() before you use it.

…to no avail.

Although no exception is thrown, could there be a problem with my licenseStream?

Do you mean that something like the following, at the start of my application, should enable the whole application to use the license even if it’s been garbage collected since then?

//start app…
setupAspose();


where setupAspose() is:

private void setupAspose() {
License lic=new License()
lic.setLicense(…)
}

and then, in the same thread but in a very different context:

Presentation pres=new Presentation()


Yes, that’s right, only one setupAspose() call should be enough. Sure, you can call setLicense() many time if you like but that is not necessary and takes time. For the real code example you can check our Northwind demo.

About your previous question. In case of any errors in the license file setLicense() method will throw exception.

Hi,

I thought of simplifying my code at the maximum so that it may help identify the issue.

Here’s the simplified code that lives for now in a Struts 1.3 Action’s perform method:

if(slideBundlesToBeFilled.size() == 0) {
try {
final InputStream licenseStream=SaveSlideLibPresentationAction.class.getResourceAsStream(“Aspose.Slides.lic”);
try {
License license=new License();
license.setLicense(licenseStream);

Presentation asposePres = new Presentation(new ByteArrayInputStream(presentationFile.getContent()));

logger.info(“Aspose.Slides started extracting slides from a presentation.”);

for (int i=1; i <= asposePres.getSlides().size(); i++) {
final Presentation to = new Presentation();

final Slide srcSlide = asposePres.getSlideByPosition(i);
final int newSlidePosition = to.getSlides().getLastSlidePosition() + 1;

asposePres.cloneSlide(srcSlide, newSlidePosition, to, new TreeMap());
slideBundlesToBeFilled.add(new SlideBundle(to, new Long(i)));
}
logger.info(“Aspose.Slides finished extracting slides from a presentation.”);
}
catch (Exception e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
throw e;
} finally {
IOUtils.closeQuietly(licenseStream);
}
} catch (Exception e) {
logger.error(“A very unexpected exception arose: can’t convert something we already successfully converted in the past!”, e);
return false;
}
}
return true;

And here’s SlideBundle:

public class SlideBundle {

public static final int THUMBNAIL_WIDTH=146;
public static final int ZOOM_WIDTH=640;
public static final int THUMBNAIL_HEIGHT=110;
public static final int ZOOM_HEIGHT=480;

private static final String IMAGE_FORMAT_NAME=“jpeg”;
private static final Dimension THUMBNAIL_DIMENSION=new Dimension(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
private static final Dimension ZOOM_DIMENSION=new Dimension( ZOOM_WIDTH, ZOOM_HEIGHT);

private final ByteArrayOutputStream slide;
private final ByteArrayOutputStream thumbnail;
private final ByteArrayOutputStream zoom;
private final String notes;
private final Long position;

public SlideBundle(Presentation containsOneSlide, Long positionInParentPresentation) throws IOException, PptWriteException {
position= positionInParentPresentation;

final ByteArrayOutputStream out2 =new ByteArrayOutputStream();
containsOneSlide.write(out2);
slide= out2;

final Slide slide = containsOneSlide.getSlideByPosition(containsOneSlide.getSlides().getLastSlidePosition());
final ByteArrayOutputStream out=new ByteArrayOutputStream();
ImageIO.write(slide.getThumbnail(THUMBNAIL_DIMENSION), IMAGE_FORMAT_NAME, out);
thumbnail= out;

final ByteArrayOutputStream out1 =new ByteArrayOutputStream();
ImageIO.write(slide.getThumbnail(ZOOM_DIMENSION), IMAGE_FORMAT_NAME, out1);
zoom= out1;

notes= “”;
}

public ByteArrayOutputStream getSlide() {
return slide;
}
public ByteArrayOutputStream getThumbnail() {
return thumbnail;
}
public ByteArrayOutputStream getZoom() {
return zoom;
}
public Long getPosition() {
return position;
}
}

When this code runs, everything works fine, except that watermarks are still there when I read the thumbnail images from their SlideBundle instance, and this, even though I specified the license which we’ve already tested that it works fine using your even simpler test.

Here’s what appears in the logs:

INFO [classnameAction] - Aspose.Slides started extracting slides from a presentation.
INFO [classnameAction] - Aspose.Slides finished extracting slides from a presentation.

And that’s all; so the code ran and completed its work; moreover, no exceptions were thrown.

Do you have an idea why this happens? What is the requirement about the setting up the license that my code fails to satisfy?

Hi Daniel,

As Mr. Alexey already explained, you only need to call the license file prior to do anything with PPT / PPTX. What i could understand, you are calling slideBundlesToBeFilled.size() prior to set the license.

Hi Muhammad,

This slideBundlesToBeFilled.size() call is a size() call on an ArrayList. So I wouldn't think it's related to PPT. It's simply a verification of: if it's not been done before, let's do what's in the "if" now.

So right now I believe we have properly established that:
  • My license file is valid.
  • My license file is properly read and registered in the runtime by setLicense(), without any exception being thrown.
  • My license is setup before I do any PPT work in this block of code.
  • My conversion code works with proper results and without exceptions
    • BUT watermarks are still there.
So here's where I suspect the problem might be:
  • The resulting SlideBundle instances are used much later after their instantiation, and in other threads, to access the thumbnails; and there's some notion of lazy production of thumbnails because my instances store these thumbnails not as byte arrays, but as output streams, and thus, when I call getThumbnail(), I'm in another thread that doesn't know of the lazily-produced license, and thus watermarks are added!?
  • Or, I shouldn't close the license stream that is provided to setLicense()?
Do you know of any such kind of things I should know about?

Sorry that this takes long to solve, and thanks for your time

Hello Daniel,

You can close license stream immediately after calling setLicense() method.

I’m also sorry that setting license take so long time. The situation with watermark is strange enough.

1. Try to create thumbnail in the first thread (where setLicense() called) and check if it has watermark. If watermark still there then check what presentationFile.getContent() method returns. May be source presentation already has watermark?

2. Try to add setLicense() also at the beginning of SlideBundle method and check result.

Thanks for your answer,

I have tried both 1 and 2, with no success; and presentationFile.getContent() returns the original powerpoint file we uploaded, and has no watermarks.

Here’s how looks the test code now:

private static boolean regenerateSlidesIfNeeded(File presentationFile, ArrayList slideBundlesToBeFilled) {

final InputStream licenseStream = SaveSlideLibPresentationAction.class.getResourceAsStream(“Aspose.Slides.lic”);
final License license = new License();
try {
license.setLicense(licenseStream);
} catch (AsposeLicenseException e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
} finally {
IOUtils.closeQuietly(licenseStream);
}

if(slideBundlesToBeFilled.size() == 0) {
try {
logger.info(“Aspose.Slides started extracting slides from a presentation.”);

Presentation asposePres = new Presentation(new ByteArrayInputStream(presentationFile.getContent()));
for (int i=1; i <= asposePres.getSlides().size(); i++) {
final Presentation to = new Presentation();

final Slide srcSlide = asposePres.getSlideByPosition(i);
final int newSlidePosition = to.getSlides().getLastSlidePosition() + 1;

asposePres.cloneSlide(srcSlide, newSlidePosition, to, new TreeMap());

final ByteArrayOutputStream slidePresStream =new ByteArrayOutputStream();
to.write(slidePresStream);

final Slide slide = to.getSlideByPosition(to.getSlides().getLastSlidePosition());

final ByteArrayOutputStream thumbnailStream=new ByteArrayOutputStream();
ImageIO.write(slide.getThumbnail(SlideBundle.THUMBNAIL_DIMENSION), SlideBundle.IMAGE_FORMAT_NAME, thumbnailStream);

final ByteArrayOutputStream zoomStream =new ByteArrayOutputStream();
ImageIO.write(slide.getThumbnail(SlideBundle.ZOOM_DIMENSION), SlideBundle.IMAGE_FORMAT_NAME, zoomStream);

slideBundlesToBeFilled.add(new SlideBundle(slidePresStream, thumbnailStream, zoomStream, “”, new Long(i)));
}
logger.info(“Aspose.Slides finished extracting slides from a presentation.”);
} catch (Exception e) {
logger.error(“A very unexpected exception arose: can’t convert something we already successfully converted in the past!”, e);
return false;
}
}
return true;
}

So SlideBundle is now completely devoid of any PPT logic, but I still left lots of setLicense() calls in it:

public class SlideBundle {

public static final int THUMBNAIL_WIDTH=146;
public static final int ZOOM_WIDTH=640;
public static final int THUMBNAIL_HEIGHT=110;
public static final int ZOOM_HEIGHT=480;

public static final String IMAGE_FORMAT_NAME=“jpeg”;
public static final Dimension THUMBNAIL_DIMENSION=new Dimension(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
public static final Dimension ZOOM_DIMENSION=new Dimension( ZOOM_WIDTH, ZOOM_HEIGHT);

private final ByteArrayOutputStream slide;
private final ByteArrayOutputStream thumbnail;
private final ByteArrayOutputStream zoom;
private final String notes;
private final Long position;

private static final Logger logger = Logger.getLogger(SlideBundle.class);

public SlideBundle(
ByteArrayOutputStream slide,
ByteArrayOutputStream thumbnail,
ByteArrayOutputStream zoom,
String notes,
Long positionInParentPresentation) throws IOException, PptWriteException {
final InputStream licenseStream = SlideBundle.class.getResourceAsStream(“Aspose.Slides.lic”);
final License license = new License();
try {
license.setLicense(licenseStream);
} catch (AsposeLicenseException e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
} finally {
IOUtils.closeQuietly(licenseStream);
}

this.slide = slide;
this.thumbnail = thumbnail;
this.zoom = zoom;
this.notes = notes;
this.position = positionInParentPresentation;
}

public ByteArrayOutputStream getSlide() {
final InputStream licenseStream = SlideBundle.class.getResourceAsStream(“Aspose.Slides.lic”);
final License license = new License();
try {
license.setLicense(licenseStream);
} catch (AsposeLicenseException e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
} finally {
IOUtils.closeQuietly(licenseStream);
}

return slide;
}
public ByteArrayOutputStream getThumbnail() {
final InputStream licenseStream = SlideBundle.class.getResourceAsStream(“Aspose.Slides.lic”);
final License license = new License();
try {
license.setLicense(licenseStream);
} catch (AsposeLicenseException e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
} finally {
IOUtils.closeQuietly(licenseStream);
}

return thumbnail;
}
public ByteArrayOutputStream getZoom() {
final InputStream licenseStream = SlideBundle.class.getResourceAsStream(“Aspose.Slides.lic”);
final License license = new License();
try {
license.setLicense(licenseStream);
} catch (AsposeLicenseException e) {
logger.error(“Aspose.Slide’s license could not be loaded or registered.”, e);
} finally {
IOUtils.closeQuietly(licenseStream);
}

return zoom;
}
public String getNotes() {
return notes;
}
public Long getPosition() {
return position;
}
}

Ok, let’s start from the beginning. At first, please try our latest version of Aspose.Slides for Java (attached). In case it won’t help please attach here in the forum example of created thumbnail with watermark and created one-slide ppt file also with watermark (created with latest version). Also please write some details about your configuration.

Files attached as requested.

All the PPT-related code is, for now, in a Struts 1.3’s Action class that’s performed when a user uploads a new presentation file, or wishes to re-generate, from the original, unaltered-and-whole presentation file, the individual slides, thumbnails and zooms.

As you have seen, it’s the re-generation step that I use to test Aspose’s license setup, as it takes me less time to click through the admin interface to ask for re-generation than it would take to create a new record. Nonetheless, since it’s a complete re-generation from the original ppt file, it’s a perfectly valid test.

The regenerateSlidesIfNeeded static method I have shared in the previous post is called in this Action’s perform method. The app also uses Spring injection for some fields in this Action class, but the said fields are not static and therefore not accessible nor used in the static method regenerateSlidesIfNeeded.

SlideBundle instances are later converted to a domain-model object that is persisted to a database using Hibernate. Thus, it’s at this precise point in time that the ByteArrayOutputStream instances for the individual slide presentation file, the thumbnail and the zoomed image are realized (read) so that they may be used by Hibernate as byte[] arrays for persistence purposes.

Finally, they are later read from the persistence store into ByteArrayInputStream instances that are used to stream the images to the end user’s browser, which displays them using the correct MIME types and such. As for the individual slide presentations, they may be used as sources of a dynamically built presentation of many such slides, and downloaded by the user.

All of this functionality works very fine, and your library is very usable, except for the watermarks issue we’re trying to sort out now.

I hope it helps.

There is one more possible reason. setLicense() doesn’t throw exception if license file stream is null. If your code SaveSlideLibPresentationAction.class.getResourceAsStream(“Aspose.Slides.lic”) can’t find the license then null will be returned but exception won’t be thrown and watermark will appear. Could you please check if license stream is not null;

Alexey, you found it! Cheers!

I see this behavior is properly documented in your API web site.

In the future, please provide an api-docs.jar as an in-IDE aid to development purposes. Your License class is so easy to use that I never thought to take the laborious task of firing up a browser to Aspose’s web-based API docs. (But I read all I prose and code examples in Reference web site.)

Thanks everyone for your time and help.