org.apache.poi.xslf.usermodel send image behind the text - java

I am making an application that will create a power point slide show.
I'm managing to put an image on every slide but it overlays the written text.
How to put the text in front of the image?
This is my code.
public void createNewSlide() throws FileNotFoundException, IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlideMaster master = ppt.getSlideMasters().get(0);
XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);
//propiedades do slide
ppt.setPageSize(new Dimension(1280,720));
XSLFSlide slide = ppt.createSlide(layout);
//primeiro slide, começando com texto
XSLFTextShape title = slide.getPlaceholder(0);
title.clearText();
XSLFTextParagraph paragraph = title.addNewTextParagraph();
XSLFTextRun textRun = paragraph.addNewTextRun();
textRun.setText("Simple Text");
textRun.setFontColor(Color.decode("#00ff99"));
textRun.setFontSize(60.);
title.setAnchor(new Rectangle(100,100,500,500));
//adicionando imagem ao ppt
byte[] picData = IOUtils.toByteArray(new FileInputStream("espace.jpg"));
XSLFPictureData pcData = ppt.addPicture(picData, PictureData.PictureType.JPEG);
XSLFPictureShape pictureShape = slide.createPicture(pcData);
pictureShape.setAnchor(new Rectangle(0,0,1280,720));
FileOutputStream out = new FileOutputStream("AprentacaoTeste.pptx");
ppt.write(out);
out.close();
ppt.close();
}
these are my dependencies
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
i'm using netbeans as IDE

Your inserted picture is the last shape in shape tree. So it is in front of all other shapes in shape tree. It would must be the first shape in shape tree, to be behind all other shapes. But it is very hard to change the shape tree' s order if shapes are already added. And the placeholders are already present in layout before other shapes were added.
But I think, what you trying to do is setting a background picture to the slide. This also is only possible using the underlying ooxml-schemas classes until now. But it is much more straight forward than changing the shape tree' s order.
Example:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.util.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Color;
public class CreatePPTXPictureBehindText {
static void setBackgroundPicture(XSLFSlide slide, String picturePath, PictureData.PictureType pictureType) throws Exception {
byte[] picData = IOUtils.toByteArray(new FileInputStream(picturePath));
XSLFPictureData pcData = slide.getSlideShow().addPicture(picData, pictureType);
CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
String idx = slide.addRelation(null, XSLFRelation.IMAGES, pcData).getRelationship().getId();
CTBlip blib = blipFillProperties.addNewBlip();
blib.setEmbed(idx);
}
public static void main(String[] args) throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
//set page size
ppt.setPageSize(new Dimension(1280,720));
//create first slide title layout
XSLFSlideMaster master = ppt.getSlideMasters().get(0);
XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);
XSLFSlide slide = ppt.createSlide(layout);
//set title placeholder's text and anchor
XSLFTextShape title = slide.getPlaceholder(0);
title.clearText();
XSLFTextParagraph paragraph = title.addNewTextParagraph();
XSLFTextRun textRun = paragraph.addNewTextRun();
textRun.setText("Simple Text");
textRun.setFontColor(Color.decode("#00ff99"));
textRun.setFontSize(60.);
title.setAnchor(new Rectangle(100,100,500,500));
//set background picture for slide
setBackgroundPicture(slide, "./espace.jpeg", PictureData.PictureType.JPEG);
FileOutputStream out = new FileOutputStream("./AprentacaoTeste.pptx");
ppt.write(out);
out.close();
ppt.close();
}
}

Related

How to set a different background to the slide by using apache poi?

When I set the background color for the slide, it will overwrite all the background colors. How can I set them separately?
Example:
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide createSlide = ppt.createSlide();
createSlide.getBackground().setFillColor(Color.BLUE);
XSLFSlide createSlide2 = ppt.createSlide();
createSlide2.getBackground().setFillColor(Color.RED);
The background color will all turn red.
XSLFSlide.getBackground gets the background from master sheet in slideMasters if the XSLFSlide has not already a background. And after it is new created the XSLFSlide has not already a background.
So we need at least setting an empty background after creating the slide. Then XSLFSlide.getBackground gets this instead of the background from master sheet.
Example:
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import java.awt.Color;
public class CreatePPTXSheetsDifferentBackground {
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();
if (slide.getXmlObject().getCSld().getBg() == null) slide.getXmlObject().getCSld().addNewBg();
slide.getBackground().setFillColor(Color.BLUE);
slide = slideShow.createSlide();
if (slide.getXmlObject().getCSld().getBg() == null) slide.getXmlObject().getCSld().addNewBg();
slide.getBackground().setFillColor(Color.RED);
FileOutputStream out = new FileOutputStream("CreatePPTXSheetsDifferentBackground.pptx");
slideShow.write(out);
out.close();
}
}

I am using itext to generate pdf. Now , I want to make paragrap align same with table, what should Ido?

Just like the image, I want to make 1 and 3 align same with 2. The table is default alignment.
What should I do?
Here is a simple way of doing it , add both to the same paragraph
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class ItextMain {
public static final String DEST = "simple_table4.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
// file.getParentFile().mkdirs();
new ItextMain().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("1-Not aligned with table"));
document.add(new Chunk());
Paragraph p = new Paragraph();
p.setIndentationLeft(20);// (20);
PdfPTable table = new PdfPTable(4);
for (int aw = 0; aw < 16; aw++) {
table.addCell("hi");
}
table.setHorizontalAlignment(Element.ALIGN_LEFT);
p.add(table);
//document.add(table);
p.add("3- Aligned with table");
document.add(p);
document.close();
}
}
The iText 5 PdfPTable class has a width percentage attribute which holds the width percentage that the table will occupy in the page. By default this value is 80 and the resulting table is horizontally centered on the page, i.e. in particular it is indented.
To prevent this, simply set the percentage value to 100:
PdfPTable table = ...;
table.setWidthPercentage(100);
Alternatively set the horizontal alignment:
table.setHorizontalAlignment(Element.ALIGN_LEFT);

Poi slide formatting

I have created a PPT presentation with apache POI and I ould like to add Title for the PPT with the below code. But it throws compilation error as
The type of the expression must be an array type but it resolved to List
public static void main(String args[]) throws IOException{
//creating presentation
XMLSlideShow ppt = new XMLSlideShow();
//getting the slide master object
XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
//get the desired slide layout
XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);
//creating a slide with title layout
XSLFSlide slide1 = ppt.createSlide(titleLayout);
//selecting the place holder in it
XSLFTextShape title1 = slide1.getPlaceholder(0);
The problem here is that ppt.getSlideMasters() returns List<XSLFSlideMaster> instead of XSLFSlideMaster[] as you're expecting.
So, for the problem you want to solve the following code should be OK:
import org.apache.poi.xslf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class Slideshow {
public static void main(String[] args) throws IOException {
//creating presentation
try (FileOutputStream out = new FileOutputStream("example.ppt");
XMLSlideShow ppt = new XMLSlideShow();) {
//getting the slide master object
XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);
//get the desired slide layout
XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);
//creating a slide with title layout
XSLFSlide slide1 = ppt.createSlide(titleLayout);
//selecting the place holder in it
XSLFTextShape title1 = slide1.getPlaceholder(0);
title1.setText("Text title");
ppt.write(out);
}
}
}
And the result will be:

Does Apache POI API support PPTX2PNG conversion (in java programs) for pptx generated by LibreOffice Impress?

I am trying to convert the pptx to png images using apache-poi in java but it doesn't work with pptx generated by LibreOffice Impress (works fine with others ) and there is no exception thrown.
The resulting images I get are just the background with out the text content of the pptx.
Please see the links for the results.
The resulting image
Screen shot of Actual pptx slide
This is the basic code I got :
package com.preview;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class PptxToPng {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream is = new FileInputStream("~/Downloads/pptx/SamplePPTX.pptx");
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
double zoom = 2;
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Dimension pgsize = ppt.getPageSize();
List<XSLFSlide> slide = ppt.getSlides();
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom),
(int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// Draw first page in the PPTX. First page starts at 0 position
slide.get(0).draw(graphics);
FileOutputStream out = new FileOutputStream("~/Downloads/pptx/converted.png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
ppt.close();
System.out.println("DONE");
}
}
poi-ooxml-3.15.jar comes with a tool called PPTX2PNG. It can be found in the package org.apache.poi.xslf.util
Execute it eg with parameters -scale 1 -format jpg pptx-file.pptx and it should write an image file to the same directory as the origin pptx file.
BTW: despite the name, it can write to png, jpg and gif formats.

Does anyone have an example of Apache POI converting PPTX to PNG

Does anyone know of a good example of converting a PPTX powerpoint presentation to some form of image? PNG/GIF/etc?
I can do it for a PPT but looking for a PPTX conversion example
Thanks
In the meantime it works (... copied it from there):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class PptToPng {
public static void main(String[] args) throws Exception {
FileInputStream is = new FileInputStream("example.pptx");
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Dimension pgsize = ppt.getPageSize();
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
FileOutputStream out = new FileOutputStream("slide-" + (i + 1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
}
}
Answering my own question, I subscribed to the development mailing list and asked this question.
The answer is that this functionailty is currently not supported by apache poi
There's an example class PPTX2PNG now bundled with POI that seems to work with decent results for the PPTX decks I've thrown at it:
http://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java
pptx4j can help you to create SVG in HTML (though there is still work to do to support all shapes); and then you could use one of the tools which create a image from an automated browser window.

Categories