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:
Related
I'm trying to call GoogleCloudVision to get the image labels into an array. I don't know what to do for the ObjectLabel method which seems to be the problem. The program is meant to take an image file and run it through googles cloud vision and get an array of labels for the image, then based on the array determine what is the image.
this is the error I'm getting.
java.nio.file.NoSuchFileException: C:\Users\DELL Laptop\Downloads\Exam3Starter\src
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:533)
at java.base/java.nio.file.Files.newDirectoryStream(Files.java:544)
at GoogleCloudVision.findJsonCredentialsFile(GoogleCloudVision.java:51)
at GoogleCloudVision.detectImageLabels(GoogleCloudVision.java:80)
at SeeFood.labelImage(SeeFood.java:57)
at SeeFood.main(SeeFood.java:83)
Code:
public static void labelImage(String filename) throws IOException, InterruptedException {
// TODO: replace with your implementation
String userFile = filename;
JFrame img = new JFrame();
ImageIcon icon = new ImageIcon(userFile);
JLabel label = new JLabel(icon);
img.add(label);
img.pack();
img.setVisible(true);
ArrayList<ObjectLabel> labels = GoogleCloudVision.detectImageLabels(userFile);
JOptionPane.showMessageDialog(null, labels);
} // end labelImage
public class ObjectLabel {
String Label = "";
float confidence = 2.0F;
public ObjectLabel(String description, float score) {
}
}
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();
}
}
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 want to export data to a PDF. As a test I first want to print just a TextView. This is my code:
public static void exportPdf (Context ctx) {
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1240, 1754, 1).create();
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
LinearLayout content = new LinearLayout(ctx);
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
content.measure(measureWidth, measuredHeight);
content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
TextView tv = new TextView(content.getContext());
tv.setText("Test");
content.addView(tv);
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// write the document content
...
}
The result is an empty page. What am I doing wrong?
Edit: It is not my goal to write a text on the pdf's canvas. I want to display a Layout. The TextView is just provisory.
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);