我目前正在使用Apache POI 3.16拍摄PPTX幻灯片并将其转换为一组.png缩略图。我通过网络上发现的一些预先制定的解决方案,将我的PPTX建模为PNG功能。
我的问题主要是在生成的PNG中渲染文本大约6-8pt。此外,复杂的形状(如六边形)在渲染时会非常歪斜。任何人都可以建议任何修复?我最关心的是字体大小要大得多。
这是我的代码,为了便于阅读而发表评论:
public List<String> generatePNGThumbnails(String inputFilePath) {
// List of the thumbnail paths that will be returned.
ArrayList<String> thumbnailFilePaths = new ArrayList<>();
// Open the file.
File inputFile = new File(inputFilePath);
try {
// Open the PPTX as an XMLSlideShow.
XMLSlideShow slideShow = new XMLSlideShow(OPCPackage.open(inputFile));
// Output size for rendered images.
Dimension pageSize = slideShow.getPageSize();
int width = (int)(pageSize.width * TRANSFORMATION_SCALE);
int height = (int)(pageSize.height * TRANSFORMATION_SCALE);
// Get a list of the slides in the PPTX.
List<XSLFSlide> slides = slideShow.getSlides();
for (int i = 0; i < slides.size(); i++) {
XSLFSlide slide = slides.get(i);
// Create a new image object and prepare it for rendering.
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
// Set graphics rendering options and draw the PPTX slide to the image.
graphics.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
graphics.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY
);
graphics.setRenderingHint(
RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_QUALITY
);
graphics.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC
);
graphics.setRenderingHint(
RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON
);
graphics.setRenderingHint(
RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY
);
graphics.setColor(Color.white);
graphics.clearRect(0, 0, width, height);
graphics.scale(TRANSFORMATION_SCALE, TRANSFORMATION_SCALE);
// Draw the slide to the 2DGraphics.
slide.draw(graphics);
// Save the newly created image to the local disk.
String slideFileName = "slide" + i + ".pptx";
FileOutputStream fileOutputStream = new FileOutputStream(slideFileName);
ImageIO.write(img, "png", fileOutputStream);
// Add the output file path to the list of paths to be returned.
thumbnailFilePaths.add(slideFileName);
// Clean up before proceeding to the next slide.
graphics.dispose();
img.flush();
fileOutputStream.close();
}
} catch (InvalidFormatException | IOException e) {
// ...
}
}