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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 java.awt.Dimension;
  21. import java.awt.Graphics2D;
  22. import java.awt.RenderingHints;
  23. import java.awt.image.BufferedImage;
  24. import java.io.File;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Set;
  28. import java.util.TreeSet;
  29. import javax.imageio.ImageIO;
  30. import org.apache.poi.sl.draw.DrawFactory;
  31. import org.apache.poi.sl.usermodel.Slide;
  32. import org.apache.poi.sl.usermodel.SlideShow;
  33. import org.apache.poi.sl.usermodel.SlideShowFactory;
  34. /**
  35. * An utility to convert slides of a .pptx slide show to a PNG image
  36. *
  37. * @author Yegor Kozlov
  38. */
  39. public class PPTX2PNG {
  40. static void usage(String error){
  41. String msg =
  42. "Usage: PPTX2PNG [options] <ppt or pptx file>\n" +
  43. (error == null ? "" : ("Error: "+error+"\n")) +
  44. "Options:\n" +
  45. " -scale <float> scale factor\n" +
  46. " -slide <integer> 1-based index of a slide to render\n" +
  47. " -format <type> png,gif,jpg (,null for testing)" +
  48. " -outdir <dir> output directory, defaults to origin of the ppt/pptx file" +
  49. " -quiet do not write to console (for normal processing)";
  50. System.out.println(msg);
  51. // no System.exit here, as we also run in junit tests!
  52. }
  53. public static void main(String[] args) throws Exception {
  54. if (args.length == 0) {
  55. usage(null);
  56. return;
  57. }
  58. String slidenumStr = "-1";
  59. float scale = 1;
  60. File file = null;
  61. String format = "png";
  62. File outdir = null;
  63. boolean quiet = false;
  64. for (int i = 0; i < args.length; i++) {
  65. if (args[i].startsWith("-")) {
  66. if ("-scale".equals(args[i])) {
  67. scale = Float.parseFloat(args[++i]); // lgtm[java/index-out-of-bounds]
  68. } else if ("-slide".equals(args[i])) {
  69. slidenumStr = args[++i]; // lgtm[java/index-out-of-bounds]
  70. } else if ("-format".equals(args[i])) {
  71. format = args[++i]; // lgtm[java/index-out-of-bounds]
  72. } else if ("-outdir".equals(args[i])) {
  73. outdir = new File(args[++i]); // lgtm[java/index-out-of-bounds]
  74. } else if ("-quiet".equals(args[i])) {
  75. quiet = true;
  76. }
  77. } else {
  78. file = new File(args[i]);
  79. }
  80. }
  81. if (file == null || !file.exists()) {
  82. usage("File not specified or it doesn't exist");
  83. return;
  84. }
  85. if (format == null || !format.matches("^(png|gif|jpg|null)$")) {
  86. usage("Invalid format given");
  87. return;
  88. }
  89. if (outdir == null) {
  90. outdir = file.getParentFile();
  91. }
  92. if (!"null".equals(format) && (outdir == null || !outdir.exists() || !outdir.isDirectory())) {
  93. usage("Output directory doesn't exist");
  94. return;
  95. }
  96. if (scale < 0) {
  97. usage("Invalid scale given");
  98. return;
  99. }
  100. if (!quiet) {
  101. System.out.println("Processing " + file);
  102. }
  103. try (SlideShow<?, ?> ss = SlideShowFactory.create(file, null, true)) {
  104. List<? extends Slide<?, ?>> slides = ss.getSlides();
  105. Set<Integer> slidenum = slideIndexes(slides.size(), slidenumStr);
  106. if (slidenum.isEmpty()) {
  107. usage("slidenum must be either -1 (for all) or within range: [1.." + slides.size() + "] for " + file);
  108. return;
  109. }
  110. Dimension pgsize = ss.getPageSize();
  111. int width = (int) (pgsize.width * scale);
  112. int height = (int) (pgsize.height * scale);
  113. for (Integer slideNo : slidenum) {
  114. Slide<?, ?> slide = slides.get(slideNo);
  115. String title = slide.getTitle();
  116. if (!quiet) {
  117. System.out.println("Rendering slide " + slideNo + (title == null ? "" : ": " + title));
  118. }
  119. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  120. Graphics2D graphics = img.createGraphics();
  121. // default rendering options
  122. graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  123. graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  124. graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  125. graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  126. graphics.scale(scale, scale);
  127. // draw stuff
  128. slide.draw(graphics);
  129. // save the result
  130. if (!"null".equals(format)) {
  131. String outname = file.getName().replaceFirst(".pptx?", "");
  132. outname = String.format(Locale.ROOT, "%1$s-%2$04d.%3$s", outname, slideNo, format);
  133. File outfile = new File(outdir, outname);
  134. ImageIO.write(img, format, outfile);
  135. }
  136. graphics.dispose();
  137. img.flush();
  138. }
  139. }
  140. if (!quiet) {
  141. System.out.println("Done");
  142. }
  143. }
  144. private static Set<Integer> slideIndexes(final int slideCount, String range) {
  145. Set<Integer> slideIdx = new TreeSet<>();
  146. if ("-1".equals(range)) {
  147. for (int i=0; i<slideCount; i++) {
  148. slideIdx.add(i);
  149. }
  150. } else {
  151. for (String subrange : range.split(",")) {
  152. String idx[] = subrange.split("-");
  153. switch (idx.length) {
  154. default:
  155. case 0: break;
  156. case 1: {
  157. int subidx = Integer.parseInt(idx[0]);
  158. if (subrange.contains("-")) {
  159. int startIdx = subrange.startsWith("-") ? 0 : subidx;
  160. int endIdx = subrange.endsWith("-") ? slideCount : Math.min(subidx,slideCount);
  161. for (int i=Math.max(startIdx,1); i<endIdx; i++) {
  162. slideIdx.add(i-1);
  163. }
  164. } else {
  165. slideIdx.add(Math.max(subidx,1)-1);
  166. }
  167. break;
  168. }
  169. case 2: {
  170. int startIdx = Math.min(Integer.parseInt(idx[0]), slideCount);
  171. int endIdx = Math.min(Integer.parseInt(idx[1]), slideCount);
  172. for (int i=Math.max(startIdx,1); i<endIdx; i++) {
  173. slideIdx.add(i-1);
  174. }
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. return slideIdx;
  181. }
  182. }