Java读写pdf文件

Java读写pdf文件

在OA业务开发中,经常需要java后台读取或生成pdf文件,
itextpdf是一个常用的java操作pdf的开源库

物料准备:
1.引入itextpdf依赖
2.定义Text2PdfUtil工具类

引入itextpdf相关的依赖

 <dependency>
             <groupId>com.itextpdfgroupId>
             <artifactId>itext-asianartifactId>
             <version>5.2.0version>
         dependency>
 
         <dependency>
            <groupId>com.itextpdfgroupId>
            <artifactId>itextpdfartifactId>
            <version>5.5.13version>
        dependency>

定义工具类Text2PdfUtil.java

package cn.ath.knowwikibackend.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import java.io.*;
import java.nio.charset.Charset;

/**
 * 需要引入的依赖
 *
 * 
 *             com.itextpdf
 *             itext-asian
 *             5.2.0
 *         
 *
 *         
 *             com.itextpdf
 *             itextpdf
 *             5.5.13
 *         
 *
 */
public class Text2PdfUtil {

    /**
     * txt文本文件  转pdf文件
     * @param text   F:/data/te616.txt
     * @param pdf  F:/data/aet618.pdf
     * @throws DocumentException
     * @throws IOException
     */
    public static void text2pdf(String text,String pdf) throws DocumentException, IOException {
        Document doc = new Document();
        OutputStream os = new FileOutputStream(new File(pdf));
        PdfWriter.getInstance(doc, os);
        doc.open();
        //指定 使用内置的中文字体
        BaseFont baseFont =
                BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        Font font = new Font(baseFont,12,Font.NORMAL);
        //指定输出编码为UTF-8
        InputStreamReader isr = new InputStreamReader(
                new FileInputStream(new File(text)), Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
        String str = "";
        while((str = br.readLine()) != null){
            doc.add(new Paragraph(str,font));
        }
        isr.close();
        br.close();
        doc.close();
    }


    /**
     * 读取pdf文件的内容
     * @param filename  F:/data/aet618.pdf
     * @return  String
     */
    public static String readPDF(String filename){
        StringBuilder result = new StringBuilder();
        try {
            PdfReader reader = new PdfReader(filename);
            int countPage = reader.getNumberOfPages();
            for(int i=1;i<=countPage;i++){
                result.append(PdfTextExtractor.getTextFromPage(reader, i));
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

}


测试

@Test
    void contextLoads() throws DocumentException, IOException {
        String txtPath = "E:/test617.txt";
        String pdfPath = "E:/test617.pdf";
        Text2PdfUtil.text2pdf(txtPath,pdfPath);
        System.out.println(Text2PdfUtil.readPDF(pdfPath));
    }

Java读写pdf文件_第1张图片
Java读写pdf文件_第2张图片

你可能感兴趣的:(JavaSE笔记,JavaWeb笔记,java,pdf,开发语言,itextpdf,itext-asian)