getting error while converting ppt file to PDF in java - java

I'm just beginner in java
I trying to convert PPT file to PDF file using apache poi and Itext library but i'm getting error of NoSuchMethodError
I tried all the version of Apache poi and poi-ooxml but i'm getting same error
please help me to findout Solution for this error
Here is my code :
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.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PPTtoPDF {
public PPTtoPDF() {
}
#SuppressWarnings("resource")
public void convertPPTToPDF(String sourcepath, String destinationPath) throws Exception {
FileInputStream inputStream = new FileInputStream(sourcepath);
double zoom = 2;
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Document pdfDocument = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(destinationPath));
PdfPTable table = new PdfPTable(1);
pdfWriter.open();
pdfDocument.open();
Dimension pgsize = null;
Image slideImage = null;
BufferedImage img = null;
SlideShow ppt = new SlideShow(inputStream);
pgsize = ppt.getPageSize();
Slide slide[] = ppt.getSlides();
System.out.println("Length----> "+slide.length);
pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
pdfWriter.open();
pdfDocument.open();
for (int i = 0; i < slide.length; i++) {
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);
graphics.getPaint();
slideImage = Image.getInstance(img, null);
table.addCell(new PdfPCell(slideImage, true));
System.out.println(table);
}
pdfDocument.add(table);
pdfDocument.close();
pdfWriter.close();
System.out.println("Powerpoint file converted to PDF successfully");
}
public static void main(String[] args) throws Exception
{
PPTtoPDF pp = new PPTtoPDF();
pp.convertPPTToPDF("D:\\tp\\slides.ppt", "D:\\tp\\1.pdf");
}
}
and the error is
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.POIDocument.<init>(Lorg/apache/poi/poifs/filesystem/DirectoryNode;Lorg/apache/poi/poifs/filesystem/POIFSFileSystem;)V
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:134)
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:120)
at org.apache.poi.hslf.HSLFSlideShow.<init>(HSLFSlideShow.java:107)
at org.apache.poi.hslf.usermodel.SlideShow.<init>(SlideShow.java:122)
at convertService.PPTtoPDF.convertPPTToPDF(PPTtoPDF.java:44)
at convertService.PPTtoPDF.main(PPTtoPDF.java:75)

See the Apache POI FAQ entry on this very topic. What has almost certainly happened is that you have added a new copy of POI to your classpath, but an older version was already there (from an earlier need, your framework etc), and Java is now getting confused about which one to use.
You could use tool for that:
mvn dependency
tree JDK 8: jdeps
(https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool)
jdeps is a new command-line tool added since JDK 8 for developers to
use to understand the static dependencies of their applications and
libraries. jdeps is a static analysis tool on the given class files

Related

insert image into excel with Apache-poi

well,I modified my code to eliminate other factors:
package com.shangzhu.drt;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by lixiaoming on 2017/6/26.
*/
public class ImageTest2 {
private static void insertImageWithPOI() throws Exception {
XSSFWorkbook wwb = new XSSFWorkbook();
XSSFSheet ws = wwb.createSheet("sheet0");
BufferedImage image = ImageIO.read(new File("D:/poi.png"));
ByteArrayOutputStream baps = new ByteArrayOutputStream();
ImageIO.write(image,"png",baps);
int pictureIdx = wwb.addPicture(baps.toByteArray(), Workbook.PICTURE_TYPE_PNG);
XSSFDrawing drawing = ws.createDrawingPatriarch();
XSSFCreationHelper helper = wwb.getCreationHelper();
XSSFClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture picture = drawing.createPicture(anchor, pictureIdx);
picture.resize();
File excelFile = new File("D:/POI.xlsx");
OutputStream ops = new FileOutputStream(excelFile);
wwb.write(ops);
}
public static void main(String[] args) {
try {
insertImageWithPOI();
} catch (Exception e) {
e.printStackTrace();
}
}
}
below is the picture("D:/poi.png") in the code:
D:/poi.png
I don't think the source code which is dealing image has problems,But I don't know what I missed
I confirm that there is a problem when default column size is used. XSSFPicture.resize needs calculating the column widths in pixels to get the XSSFClientAnchor Col2 and the Dx2. As long as default column size is used, then this calculation seems to be wrong.
A workaround could be defining explicit column sizes before using XSSFPicture.resize. Then the calculation of the column widths in pixels seems to be correct.
In your code:
...
Picture picture = drawing.createPicture(anchor, pictureIdx);
for (int c=0; c<20; c++) ws.setColumnWidth(c, 11*256);
picture.resize();
...

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.

PDFBox - checkbox is not displayed if set readonly

I am generating PDF using PDFBox, where I need to add a checkbox, which needs to be preset to checked and readonly. But some how it does not work.
Please find below code, which adds the checkbox on PDF:
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
public class MyTest {
public static void main(String arg[]) throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
COSDictionary acroFormDict = new COSDictionary();
acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray()); // Added this line for Tilman's comment
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
document.getDocumentCatalog().setAcroForm(acroForm);
float x = 10f;
float y = page.getMediaBox().getHeight();
float yOffset = 15f;
float yCurrent = y - yOffset;
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(x));
rect.add(new COSFloat(yCurrent));
rect.add(new COSFloat(x+20));
rect.add(new COSFloat(yCurrent-20));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("testChk"));
cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));
PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
// checkbox.setReadonly(true);
// checkbox.setFieldFlags(PDField.FLAG_READ_ONLY);
checkbox.setValue("Yes");
// checkbox.check();
page.getAnnotations().add(checkbox.getWidget());
acroForm.getFields().add(checkbox); // Added this line for Tilman's comment
yCurrent = yCurrent - 20 - yOffset;
File file = new File("C:\\pdf\\CheckBox\\CheckBoxSample1.pdf");
System.out.println("Sample file saved at : " + file.getAbsolutePath());
document.save(file);
document.close();
}
}
Now if you uncomment the line:
checkbox.setReadonly(true);
Or
checkbox.setFieldFlags(PDField.FLAG_READ_ONLY);
The checkbox will no more displayed (or may be there but value is unchecked).
I am using PDFBox 1.8.10
Behavior is similar in Adobe Reader 11 and Foxit.
Also if I generate the PDF without setting the box readonly, in Adobe, I see the checkbox displayed with value set, but when I bring focus on it using tab, it gets disappear. And again on focus out (I click somewhere else other than checkbox), it appears again.
It seems like I am missing a very small thing, but could not find out.
Any help?
Thanks in advance.
You need to give field appearance using PDAppearanceCharacteristicsDictionary class. That will fix above issue.
Please check sample code below:
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;
public class MyCheckBox {
public static void main(String arg[]) throws IOException, COSVisitorException
{
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
document.addPage(page);
PDFont font = PDType1Font.HELVETICA;
PDResources res = new PDResources();
String fontName = res.addFont(font);
String da = "/" + fontName + " 10 Tf 0 0.4 0 rg";
COSDictionary acroFormDict = new COSDictionary();
acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
acroFormDict.setItem(COSName.FIELDS, new COSArray());
acroFormDict.setItem(COSName.DA, new COSString(da));
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
acroForm.setDefaultResources(res);
document.getDocumentCatalog().setAcroForm(acroForm);
float x = 10f;
float y = page.getMediaBox().getHeight();
float yOffset = 15f;
float yCurrent = y - yOffset;
PDGamma colourBlack = new PDGamma();
PDAppearanceCharacteristicsDictionary fieldAppearance =
new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(colourBlack);
COSArray arr = new COSArray();
arr.add(new COSFloat(227/255f));
arr.add(new COSFloat(239/255f));
arr.add(new COSFloat(1f));
fieldAppearance.setBackground(new PDGamma(arr));
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(x));
rect.add(new COSFloat(yCurrent));
rect.add(new COSFloat(x+20));
rect.add(new COSFloat(yCurrent-20));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.TU, new COSString("Test Checkbox with PDFBox"));
cosDict.setItem(COSName.T, new COSString("testChk"));
cosDict.setItem(COSName.DA, new COSString("/F0 10 Tf 0 0.4 0 rg"));
// cosDict.setInt(COSName.FF, 49152); //Radio Button Symbol
cosDict.setInt(COSName.F, 4);
// cosDict.setInt(COSName.FF, 16384);
PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
checkbox.setFieldFlags(PDCheckbox.FLAG_READ_ONLY);
checkbox.setValue("Yes");
// checkbox.setValue("Off");
checkbox.getWidget().setAppearanceCharacteristics(fieldAppearance);
page.getAnnotations().add(checkbox.getWidget());
acroForm.getFields().add(checkbox);
yCurrent = yCurrent - 20 - yOffset;
File file = new File("C:\\pdf\\CheckBoxSample.pdf");
System.out.println("Sample file saved at : " + file.getAbsolutePath());
document.save(file);
document.close();
}
}

Fastest method for blurring an image in java

Edit I'm using Eclipse ADT bundle but when I try importing the library in the suggested answer above it doesn't work.
I'm using a combination of LibGDX and Java (90-99% java) to blur an image. The blur works but it takes nearly a second to take a screenshot, save it, access it, blur it, save it, and re-access it. I can't do much pre-processing because it takes a screenshot of the game to blur. Here is what I'm using currently for blurring: (I'll put drawing and screenshots up if you need to see them, but I think the issue lies in the blurring of an 800x480 png.)
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
public class Blur {
private static BufferedImage mshi;
private BufferedImage databuf;
private static int blurRad = 300;
public static void createBlur(FileHandle file) throws IOException {
mshi = ImageIO.read(file.file());
BufferedImage databuf = new BufferedImage(mshi.getWidth(null),
mshi.getHeight(null),
BufferedImage.TYPE_INT_BGR);
java.awt.Graphics g = databuf.getGraphics();
g.drawImage(mshi, 455, 255, null);
float[] blurKernel = new float[blurRad*blurRad];
for(int i =0; i<blurRad*blurRad; i++) {
blurKernel[i] = 1.0f/256.0f;
}
BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, blurKernel), ConvolveOp.EDGE_ZERO_FILL, null );
mshi = op.filter(mshi, databuf);
g.dispose();
File outputfile = Gdx.files.local("Blur.png").file();
ImageIO.write(mshi, "png", outputfile);
}
}

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