Export PowerPoint slides into java.awt.Graphics2D - java

i started a test to convert a ppt document to jpeg or png image. i use java to test.
follow the instruction on the apache web: http://poi.apache.org/slideshow/how-to-shapes.html#Render, and the code:
FileInputStream is = new FileInputStream("slideshow.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
//save the output
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
}
in the ppt doc, i just type the "hello world", than i run the java program, and the png image generated successfully. but i open the image with ACDsee software to view the image, but the "helloworld" didn't appear in the png image. what is the matter? Can anyone here give me some advice? you can also test by yourself to look at the result, pls let me know if you have got the same result.

It appears that you aren't doing anything at all with your BufferedImage. You are simply filling the image with white and saving it to a file.

Related

Save a BufferedImage to BMP/PNG/JPG and then open it back

I have made a project like Paint from windows and now I want to make the save/open buttons. I have found out how to save the bufferedImage, but the problem is how do I open it back in the correct location and be able to draw on it again?
To read in an image, use ImageIO.
File myPath = new FIle("path to image");
BUfferedImage img = ImageIO.read(myPath);
Also what you could (should) do is load the image into your user space so that you don't edit the original image:
public static BufferedImage userSpace(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = newImage.createGraphics();
graphics.drawRenderedImage(image, null);
graphics.dispose();
return newImage;
}

Saving com.itextpdf.text.Image as a image file

Is there a way to save the com.itextpdf.text.Image file as a jpg file on the file system ?
Barcode39 code39 = new Barcode39();
code39.setCode(barcode);
code39.setStartStopText(false);
image39 = code39.createImageWithBarcode(cb, null, null);
image39.scaleAbsolute(width, height);
image39.setAbsolutePosition(top, left);
cb.addImage(image39);
I am creating a bar code image and adding it to a pdf. At the same time i want the image to be saved on the file system. Any help is appreciated.
OR,
Is it possible to retrieve the barcode from the pdf( both the barcode as well as the numbers under it) as an image file and save it to the file system using itext ?
Just convert the Barcode39 itext image into an AWT image using createAwtImage:
java.awt.Image awtImage = code39.createAwtImage(Color.BLACK, Color.WHITE);
Then convert it to a BufferedImage and store it:
BufferedImage bImage= new BufferedImage(awtImage.getWidth(), awtImage.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bImage.createGraphics();
g.drawImage(awtImage, 0, 0, null);
g.dispose();
ImageIO.write(bImage, "jpg", new File("code39.jpg"));
You can use this code:
BarcodeQRCode qrcode = new BarcodeQRCode("testo testo testo", 1, 1, null);
Image image = qrcode.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getWidth(null), BufferedImage.TYPE_4BYTE_ABGR);
buffImg.getGraphics().drawImage(image, 0, 0, null);
buffImg.getGraphics().dispose();
File file = new File("tmp.png");
ImageIO.write(buffImg, "png", file);
I hope it helps you.
Enrico

Java GUI to PDF using PDFBox

whats the best way to embedded a java GUI component such as JPanel to pdf using PDFBox?
Im currently using this code:
PDDocument doc = null;
PDPage page = null;
PDXObjectImage ximage = null;
try {
doc = new PDDocument();
page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
BufferedImage image = new BufferedImage((int)PDPage.PAGE_SIZE_A4.getWidth()*2,(int)PDPage.PAGE_SIZE_A4.getHeight()*2, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
panel.setBounds(0, 0,(int)PDPage.PAGE_SIZE_A4.getWidth()*2,(int)PDPage.PAGE_SIZE_A4.getHeight()*2);
panel.doLayout();
panel.validate();
panel.paint(g);
g.dispose();
ximage = new PDPixelMap(doc, image);
content.drawXObject(ximage, 0 , 0 ,(int)PDPage.PAGE_SIZE_A4.getWidth(),(int)PDPage.PAGE_SIZE_A4.getHeight() );
content.close();
if (path==null || path.equals(""))
doc.print();
else
doc.save(path);
doc.close();
} catch(Exception ie) {
ie.printStackTrace();
}
but two problems that i got with it is 1. sometimes i am getting a blank pdf for some reason, and 2. when it works the PDF looks pixelated.
is there a better way?
PS: i am not allowed to use iText in my project
Because you add the (vector) panel graphics to the PDF page through a PDFPixelMap, the panel graphics will always be pixelated (converted to a raster).
If you want to prevent rasterization, you would have to use the vector drawing commands such as drawLine, stroke, drawText, etc. instead.

Is it possible to create image programmatically on Java, Android?

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}

Chinese characters converted to squares when using APACHE POI to convert PPT to Image

I got a problem when I try to use Apache POI project to convert my PPT to Images. My code as follows:
FileInputStream is = new FileInputStream("test.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
//save the output
FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png");
javax.imageio.ImageIO.write(img, "png", out);
out.close();
It works fine except that all Chinese words are converted to some squares. Then how can I fix this?
This seems to be a bug with apache POI. I have added it in bugzilla
https://issues.apache.org/bugzilla/show_bug.cgi?id=54880
The problem is not on the POI side, but in the JVM font setting.
You need to set the font to one in the list of JVM fonts (/usr/lib/jvm/jdk1.8.0_20/jre/lib/fonts or similar), such as simsun.ttc.
XSLFTextShape[] phs = slide[i].getPlaceholders();
for (XSLFTextShape ts : phs) {
java.util.List<XSLFTextParagraph> tpl = ts.getTextParagraphs();
for(XSLFTextParagraph tp: tpl) {
java.util.List<XSLFTextRun> trs = tp.getTextRuns();
for(XSLFTextRun tr: trs) {
logger.info(tr.getFontFamily());
tr.setFontFamily("SimSun");
}
}
}
The issue is usage of FileOuputStream which will always write data to the file in default system encoding which is most probably ISO-8859_1 for Windows. Chinese characters are not supported by this encoding. You need to create a stream where you can write using UTF-8 encoding which needs creation of reader. I was looking at the API but did not find any methods taking reader as an argument. But check if ImageOutputStream can help you.

Categories

Resources