当前位置:   article > 正文

java-poi实现 ppt文件转换PDF文件_java pptx转pdf

java pptx转pdf

借鉴 https://blog.csdn.net/qq_41831842/article/details/115394488

1、maven依赖都使用poi 4.1.2版本:

		<!--  poi -->
	    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>4.1.2</version>
		</dependency>
		
       <!-- itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
      <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.0</version>
        </dependency>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

ppt/pptx转换pdf:

import cn.hutool.core.util.StrUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 类名称:PdfConvertUtil
 * 类描述:转为为PDF工具类
*/
public final class PdfConverUtil {

/**
	 * pptToPdf
	 * @param pptPath PPT文件路径
	 * @param pdfDir 生成的PDF文件路径
	 * @return
	 */
	public static boolean pptToPdf(String pptPath, String pdfDir) {

        if (StrUtil.isEmpty(pptPath)) {
            throw new RuntimeException("word文档路径不能为空");
        }

        if (StrUtil.isEmpty(pdfDir)) {
            throw new RuntimeException("pdf目录不能为空");
        }
        
        
        String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";

        Document document = null;
        HSLFSlideShow hslfSlideShow = null;
        FileOutputStream fileOutputStream = null;
        PdfWriter pdfWriter = null;

        try {
            hslfSlideShow = new HSLFSlideShow(new FileInputStream(pptPath));

            // 获取ppt文件页面
            Dimension dimension = hslfSlideShow.getPageSize();

            fileOutputStream = new FileOutputStream(pdfPath);

            document = new Document();
            
            // pdfWriter实例
            pdfWriter = PdfWriter.getInstance(document, fileOutputStream);

            document.open();

            PdfPTable pdfPTable = new PdfPTable(1);
           
            List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();

            for (int i=0; i < hslfSlideList.size(); i++) {
                HSLFSlide hslfSlide = hslfSlideList.get(i);
                // 设置字体, 解决中文乱码
                for (HSLFShape shape : hslfSlide.getShapes()) {
                    HSLFTextShape textShape = (HSLFTextShape) shape;

                    for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
                        for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
                            textRun.setFontFamily("宋体");
                        }
                    }
                }
                BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);

                Graphics2D graphics2d = bufferedImage.createGraphics();

                graphics2d.setPaint(Color.white);
                graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));

                hslfSlide.draw(graphics2d);

                graphics2d.dispose();

                Image image = Image.getInstance(bufferedImage, null);
                image.scalePercent(50f);

                // 写入单元格
                pdfPTable.addCell(new PdfPCell(image, true));
                document.add(image);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (document != null) {
                    document.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (pdfWriter != null) {
                    pdfWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

	/**
	 *
	 * @Title: pptxToPdf
	 * @param pptPath PPT文件路径
	 * @param pdfDir 生成的PDF文件路径
	 */
	public static boolean pptxToPdf(String pptPath, String pdfDir) {

		if (StrUtil.isEmpty(pptPath)) {
			throw new RuntimeException("word文档路径不能为空");
		}

		if (StrUtil.isEmpty(pdfDir)) {
			throw new RuntimeException("pdf目录不能为空");
		}

		String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";

		Document document = null;

		XMLSlideShow slideShow = null;


		FileOutputStream fileOutputStream = null;

		PdfWriter pdfWriter = null;


		try {

			slideShow = new XMLSlideShow(new FileInputStream(pptPath));

			Dimension dimension = slideShow.getPageSize();

			fileOutputStream = new FileOutputStream(pdfPath);

			document = new Document();

			pdfWriter = PdfWriter.getInstance(document, fileOutputStream);

			document.open();

			PdfPTable pdfPTable = new PdfPTable(1);

			List<XSLFSlide> slideList = slideShow.getSlides();

			for (int i = 0, row = slideList.size(); i < row; i++) {

				XSLFSlide slide = slideList.get(i);

				// 设置字体, 解决中文乱码
				for (XSLFShape shape : slide.getShapes()) {
					XSLFTextShape textShape = (XSLFTextShape) shape;

					for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
						for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
							textRun.setFontFamily("宋体");
						}
					}
				}

				BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);

				Graphics2D graphics2d = bufferedImage.createGraphics();

				graphics2d.setPaint(Color.white);
				graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));

				slide.draw(graphics2d);

				graphics2d.dispose();

				Image image = Image.getInstance(bufferedImage, null);
				image.scalePercent(50f);

				// 写入单元格
				pdfPTable.addCell(new PdfPCell(image, true));
				document.add(image);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (document != null) {
					document.close();
				}
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
				if (pdfWriter != null) {
					pdfWriter.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218

测试

public static void main(String[] args) {
		boolean successful = false;
	   // ppt to pdf
		successful = PdfConvertUtil.pptToPdf("D:\\360_js\\abc1.ppt", "D:\\360_js")
		
		// pptx to pdf
	//	 successful = PdfConvertUtil.pptxToPdf("D:\\360_js\\测321pt.pptx", "D:\\360_js");
		 
	   System.out.println("转换" + (successful ? "成功" : "失败"));
	   }


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

可能会报错:

org.apache.poi.xslf.usermodel.XSLFPictureShape cannot be cast to org.apache.poi.xslf.usermodel.XSLFTextShape

主要都是中文转码的问题,注释掉解决中文乱码的代码就可以执行了

完整的代码,读取目录下所有PPT转化为PDF

import cn.hutool.core.util.StrUtil;

        import java.awt.*;
        import java.awt.image.BufferedImage;

        import org.apache.poi.xslf.usermodel.*;
        import org.apache.poi.hslf.usermodel.*;
        import com.itextpdf.text.Document;
        import com.itextpdf.text.pdf.*;
        import com.itextpdf.text.Image;

import java.io.File;
import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.util.List;

/**
 * 类名称:PdfConvertUtil
 * 类描述:转为为PDF工具类
 */
public final class PdfConverUtil {

    /**
     * pptToPdf
     * pptPath PPT文件路径
     * pdfDir 生成的PDF文件路径
     */
    public static void main(String[] args) {
        boolean successful = false;

        //获取文件路径文件夹下的全部文件列表
        //表示一个文件路径
        File file = new File("D:\\360_js\\abc1.ppt", "D:\\360_js");
        //用数组把文件夹下的文件存起来
        File[] files = file.listFiles();


        //foreach遍历数组
        for (File file2 : files) {
            //打印文件列表:只读取名称使用getName();
//            System.out.println("路径:"+file2.getPath());
//            System.out.println("文件夹/文件名:"+file2.getName());

//            successful = PdfConverUtil.pptToPdf("D:\\360_js\\abc1.ppt", "D:\\360_js");
            successful = PdfConverUtil.pptxToPdf("D:\\360_js\\abc1.ppt", "D:\\360_js");
        }
//          ppt to pdf
//        successful = pptToPdf("D:\\360_js\\abc1.ppt", "D:\\360_js");

        // pptx to pdf
        //	 successful = PdfConvertUtil.pptxToPdf("D:\\360_js\\测321pt.pptx", "D:\\360_js");

        System.out.println("转换" + (successful ? "成功" : "失败"));
    }

    public static boolean pptToPdf(String pptPath, String pdfDir) {

        if (StrUtil.isEmpty(pptPath)) {
            throw new RuntimeException("word文档路径不能为空");
        }

        if (StrUtil.isEmpty(pdfDir)) {
            throw new RuntimeException("pdf目录不能为空");
        }


        String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";

        Document document = null;
        HSLFSlideShow hslfSlideShow = null;
        FileOutputStream fileOutputStream = null;
        PdfWriter pdfWriter = null;

        try {
            hslfSlideShow = new HSLFSlideShow(new FileInputStream(pptPath));

            // 获取ppt文件页面
            Dimension dimension = hslfSlideShow.getPageSize();

            fileOutputStream = new FileOutputStream(pdfPath);

            document = new Document();

            // pdfWriter实例
            pdfWriter = PdfWriter.getInstance(document, fileOutputStream);

            document.open();

            PdfPTable pdfPTable = new PdfPTable(1);

            List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();

            for (int i=0; i < hslfSlideList.size(); i++) {
                HSLFSlide hslfSlide = hslfSlideList.get(i);
//                // 设置字体, 解决中文乱码
//                for (HSLFShape shape : hslfSlide.getShapes()) {
//                    HSLFTextShape textShape = (HSLFTextShape) shape;
//
//                    for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
//                        for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
//                            textRun.setFontFamily("宋体");
//                        }
//                    }
//                }
                BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);

                Graphics2D graphics2d = bufferedImage.createGraphics();

                graphics2d.setPaint(Color.white);
                graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));

                hslfSlide.draw(graphics2d);

                graphics2d.dispose();

                Image image = Image.getInstance(bufferedImage, null);
                image.scalePercent(50f);

                // 写入单元格
                pdfPTable.addCell(new PdfPCell(image, true));
                document.add(image);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (document != null) {
                    document.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (pdfWriter != null) {
                    pdfWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     *
     * @Title: pptxToPdf
     * @param pptPath PPT文件路径
     * @param pdfDir 生成的PDF文件路径
     */
    public static boolean pptxToPdf(String pptPath, String pdfDir) {

        if (StrUtil.isEmpty(pptPath)) {
            throw new RuntimeException("word文档路径不能为空");
        }

        if (StrUtil.isEmpty(pdfDir)) {
            throw new RuntimeException("pdf目录不能为空");
        }

        String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";

        Document document = null;

        XMLSlideShow slideShow = null;


        FileOutputStream fileOutputStream = null;

        PdfWriter pdfWriter = null;


        try {

            slideShow = new XMLSlideShow(new FileInputStream(pptPath));

            Dimension dimension = slideShow.getPageSize();

            fileOutputStream = new FileOutputStream(pdfPath);

            document = new Document();

            pdfWriter = PdfWriter.getInstance(document, fileOutputStream);

            document.open();

            PdfPTable pdfPTable = new PdfPTable(1);

            List<XSLFSlide> slideList = slideShow.getSlides();

            for (int i = 0, row = slideList.size(); i < row; i++) {

                XSLFSlide slide = slideList.get(i);

//                // 设置字体, 解决中文乱码
//                for (XSLFShape shape : slide.getShapes()) {
//                    XSLFTextShape textShape = (XSLFTextShape) shape;
//
//                    for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
//                        for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
//                            textRun.setFontFamily("宋体");
//                        }
//                    }
//                }

                BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);

                Graphics2D graphics2d = bufferedImage.createGraphics();

                graphics2d.setPaint(Color.white);
                graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));

                slide.draw(graphics2d);

                graphics2d.dispose();

                Image image = Image.getInstance(bufferedImage, null);
                image.scalePercent(50f);

                // 写入单元格
                pdfPTable.addCell(new PdfPCell(image, true));
                document.add(image);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (document != null) {
                    document.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                if (pdfWriter != null) {
                    pdfWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/878413
推荐阅读
相关标签
  

闽ICP备14008679号