java doc转pdf_java 完美解决 ppt/pptx 转pdf 源码

需要的pom文件

52a2fc780cd2d2da731e56bc4f45ca41.png
508ec93dd626ee7246f1826c235b6365.png
3ce838c437a19e8f27076525868d3c9e.png
2197682ea2dbe525e11aefc11ce5db20.png

第一个先来介绍pptx转pdf

public class PptxToPDFConverter extends Converter{ // private static final String fontPath =PptxToPDFConverter.class.getClassLoader().getResource("").getPath()+"/simsun.ttc"; public PptxToPDFConverter(InputStream inStream, OutputStream outStream, boolean showMessages, boolean closeStreamsWhenComplete) { super(inStream, outStream, showMessages, closeStreamsWhenComplete); } private XSLFSlide[] slides; @Override public void convert() throws Exception { loading(); Dimension pgsize = processSlides(); processing(); double zoom = 2; // magnify it by 2 as typical slides are low res AffineTransform at = new AffineTransform(); at.setToScale(zoom, zoom); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outStream); document.open(); if(slides!=null){ for (int j = 0; j < getNumSlides(); j++) { for (XSLFShape shape : slides[j].getShapes()) { if (shape instanceof XSLFTextShape) { XSLFTextShape txtshape = (XSLFTextShape) shape; // System.out.println("txtshape" + (i+1) + ":" + txtshape.getShapeName()); //System.out.println("text:" +txtshape.getText()); for (XSLFTextParagraph textPara : txtshape.getTextParagraphs()) { List textRunList = textPara.getTextRuns(); for (XSLFTextRun textRun : textRunList) { textRun.setFontFamily("simsun.ttc"); } } } } } } for (int i = 0; i < getNumSlides(); i++) { BufferedImage bufImg = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB); Graphics2D graphics = bufImg.createGraphics(); graphics.setTransform(at); //clear the drawing area graphics.setPaint(getSlideBGColor(i)); graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); try{ drawOntoThisGraphic(i, graphics); } catch(Exception e){ //Just ignore, draw what I have } Image image = Image.getInstance(bufImg, null); document.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight())); document.newPage(); image.setAbsolutePosition(0, 0); document.add(image); } //Seems like I must close document if not output stream is not complete document.close(); //Not sure what repercussions are there for closing a writer but just do it. writer.close(); finished(); } protected Dimension processSlides() throws IOException{ InputStream iStream = inStream; XMLSlideShow ppt = new XMLSlideShow(iStream); Dimension dimension = ppt.getPageSize(); slides = ppt.getSlides(); return dimension; } protected int getNumSlides(){ return slides.length; } protected void drawOntoThisGraphic(int index, Graphics2D graphics){ slides[index].draw(graphics); } protected Color getSlideBGColor(int index){ return slides[index].getBackground().getFillColor(); }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值