You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PPTX2PNG.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.util;
  20. import org.apache.poi.openxml4j.opc.OPCPackage;
  21. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  22. import org.apache.poi.xslf.usermodel.XSLFSlide;
  23. import javax.imageio.ImageIO;
  24. import java.awt.Color;
  25. import java.awt.Dimension;
  26. import java.awt.Graphics2D;
  27. import java.awt.RenderingHints;
  28. import java.awt.image.BufferedImage;
  29. import java.io.FileOutputStream;
  30. /**
  31. * An utulity to convert slides of a .pptx slide show to a PNG image
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public class PPTX2PNG {
  36. static void usage(){
  37. System.out.println("Usage: PPTX2PNG [options] <pptx file>");
  38. System.out.println("Options:");
  39. System.out.println(" -scale <float> scale factor");
  40. System.out.println(" -slide <integer> 1-based index of a slide to render");
  41. }
  42. public static void main(String[] args) throws Exception {
  43. if (args.length == 0) {
  44. usage();
  45. return;
  46. }
  47. int slidenum = -1;
  48. float scale = 1;
  49. String file = null;
  50. for (int i = 0; i < args.length; i++) {
  51. if (args[i].startsWith("-")) {
  52. if ("-scale".equals(args[i])) {
  53. scale = Float.parseFloat(args[++i]);
  54. } else if ("-slide".equals(args[i])) {
  55. slidenum = Integer.parseInt(args[++i]);
  56. }
  57. } else {
  58. file = args[i];
  59. }
  60. }
  61. if(file == null){
  62. usage();
  63. return;
  64. }
  65. System.out.println("Processing " + file);
  66. XMLSlideShow ppt = new XMLSlideShow(OPCPackage.open(file));
  67. Dimension pgsize = ppt.getPageSize();
  68. int width = (int) (pgsize.width * scale);
  69. int height = (int) (pgsize.height * scale);
  70. XSLFSlide[] slide = ppt.getSlides();
  71. for (int i = 0; i < slide.length; i++) {
  72. if (slidenum != -1 && slidenum != (i + 1)) continue;
  73. String title = slide[i].getTitle();
  74. System.out.println("Rendering slide " + (i + 1) + (title == null ? "" : ": " + title));
  75. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  76. Graphics2D graphics = img.createGraphics();
  77. // default rendering options
  78. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  79. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  80. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  81. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  82. graphics.setColor(Color.white);
  83. graphics.clearRect(0, 0, width, height);
  84. graphics.scale(scale, scale);
  85. // draw stuff
  86. slide[i].draw(graphics);
  87. // save the result
  88. int sep = file.lastIndexOf(".");
  89. String fname = file.substring(0, sep == -1 ? file.length() : sep) + "-" + (i + 1) +".png";
  90. FileOutputStream out = new FileOutputStream(fname);
  91. ImageIO.write(img, "png", out);
  92. out.close();
  93. }
  94. System.out.println("Done");
  95. }
  96. }