After every page I got an empty page

Hi there,



I use the following code to create pages for a pdf wihtin a loop.



Section sec1 = pdf.getSections().add();

sec1.setIsLandscape(true);

Image img1 = new Image(sec1);

img1.getImageInfo().setFixWidth(sec1.getPageSetup().getPageHeight());

img1.getImageInfo().setFixHeight(sec1.getPageSetup().getPageWidth());

img1.getImageInfo().setFile(fileNameOfImage);

sec1.getParagraphs().add(img1);



However, after every page I’ve got an empty page within my resulting PDF-File. What’s wrong here?



Regards,



Christian

Hi Christian,

Would you please attach your codes and corresponding files so that we can reproduce the error? Thanks.

Best regards.

Dear,
this is the source-code:

public ChangeableBlobImpl getPdf(Blob blob, String fileName) throws Exception {
ChangeableBlobImpl changeableBlobImpl = null;
try {
//Instantiate a Presentation object that represents a PPT file
ArrayList fileNames = new ArrayList();
Presentation pres = new Presentation(blob.getInputStream());
LOG.info(“PowerPoint-File has got " + pres.getSlides().size() + " slides…”);
if (pres.getSlides().size()>0) {
Pdf pdf = new Pdf();
int pos = 1;
while (pos
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(pos);
pos++;
ByteArrayOutputStream svgByteArrayOutputStream = new ByteArrayOutputStream();
slide.saveToSVG(svgByteArrayOutputStream);
svgByteArrayOutputStream.flush();
LOG.info("The size is of svgByteArayOutputStream is: " + svgByteArrayOutputStream.size());
// Create the transcoder input as a stream
// Create a JPEG transcoder
JPEGTranscoder tStream = new JPEGTranscoder();
// Set the transcoding hints.
tStream.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
ByteArrayInputStream svgByteArrayInputStream = new ByteArrayInputStream(svgByteArrayOutputStream.toByteArray());
TranscoderInput transcoderInputStream = new TranscoderInput(svgByteArrayInputStream);
// Create the transcoder output with a stream
ByteArrayOutputStream jpegByteArrayOutputStream = new ByteArrayOutputStream();
OutputStream ostreamStream = jpegByteArrayOutputStream;
TranscoderOutput transcoderOutputStream = new TranscoderOutput(ostreamStream);
tStream.transcode(transcoderInputStream, transcoderOutputStream);
jpegByteArrayOutputStream.flush();
//Gets a name for the file with a randomized token
String fileNameOfImage = getToken() + “.jpg”;
FileOutputStream fos= new FileOutputStream(fileNameOfImage);
fos.write(jpegByteArrayOutputStream.toByteArray());
fos.flush();
fos.close();
//adds a section
Section sec1 = pdf.getSections().add();
sec1.setIsLandscape(true);
Image img1 = new Image(sec1);
//ToDo: Every time a new section is created, this is wrong
//ToDo: Subtract the border of every page for scaling properly
img1.getImageInfo().setFixWidth(sec1.getPageSetup().getPageHeight());
img1.getImageInfo().setFixHeight(sec1.getPageSetup().getPageWidth());
//img1.getImageInfo().setMemoryData() is currently not working
//This bug is known by Aspose already
img1.getImageInfo().setFile(fileNameOfImage);
sec1.getParagraphs().add(img1);
//Store filename for later deletion
fileNames.add(fileNameOfImage);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
pdf.save(byteArrayOutputStream);
MimeType mimeTypeOut = new MimeType(targetMimeType);
//Conversation has been done - so the zipped file will be given back
changeableBlobImpl =
new ChangeableBlobImpl(byteArrayOutputStream.toByteArray(), mimeTypeOut);
for (String fileNameToDelete : fileNames) {
File fileOfImage = new File(fileNameToDelete);
fileOfImage.delete();
fileOfImage = null;
}
}
} catch (Exception e) {
LOG.info(e);
e.printStackTrace();
}
return changeableBlobImpl;
}

I’ve attached the original Powerpoint-File.
Regards,
Christian

And this is the resulting PDF-File - see attachment!

Dear Christian,

Thanks for attaching your codes. We will check it and give you a reply soon.

Best regards.

Any news regarding this?

We are working on this issue. I hope we can provide a hotfix early next week.

Dear:
I have check your code and Pdf file carefully. There are two reasons why you get extra blank pages.
Firstly, Every Section in Aspose Pdf for java will start a new blank page. So it would be better if you move your code
Section sec1 = pdf.getSections().add();
sec1.setIsLandscape(true);

before the
while (pos statement.
The second is a bit complicated. In your code
img1.getImageInfo().setFixWidth(sec1.getPageSetup().getPageHeight());
img1.getImageInfo().setFixHeight(sec1.getPageSetup().getPageWidth());
you set the size of image to the exactly large of a page. Actually a page has its margin, it would be better to take margin into account. So the code becomes:
img1.getImageInfo().setFixWidth(sec1.getPageSetup().getPageHeight()
- sec1.getPageSetup().getMargin().getLeft()
- sec1.getPageSetup().getMargin().getRight());
img1.getImageInfo().setFixHeight(sec1.getPageSetup().getPageWidth()
- sec1.getPageSetup().getMargin().getBottom()
-sec1.getPageSetup().getMargin().getTop());


If you change your code as above, you will found there is still a extra blank page at the end of your Pdf file. That’s because the last Image height is exactlly the same with the page(excluding margin) which will trigger a new blank page. That situation is subtle, but we can avoid this by changing the height of Image a little( such as minus 0.1f). So the ultimate code becomes:
img1.getImageInfo().setFixWidth(sec1.getPageSetup().getPageHeight()
- sec1.getPageSetup().getMargin().getLeft()
- sec1.getPageSetup().getMargin().getRight());

img1.getImageInfo().setFixHeight(sec1.getPageSetup().getPageWidth()
- sec1.getPageSetup().getMargin().getBottom()
- sec1.getPageSetup().getMargin().getTop()
- 0.1f);
Hopefully, it will work. Thank you for considering our product.
ps: Your code is elegant and very easy to understand.

Thank you very much for your help. It works!