Java实现Txt转PDF文件

TxT转PDF可以直接使用IText就可以了,IText在pdf领域可以说暂时是最好的方案了。通过直接读取txt文件,然后生成pdf,再添加文本就可以了。

代码如下:

复制代码
public class Txt2PDF {
    private static final String FONT = "C:\\Windows\\Fonts\\simhei.ttf";
    public static void text2pdf(String text, String pdf) throws DocumentException, IOException {
        Document document = new Document();
        OutputStream os = new FileOutputStream(new File(pdf));
        PdfWriter.getInstance(document, os);
        document.open();
        //方法一:使用Windows系统字体(TrueType)
        BaseFont baseFont = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(text)), "GBK");
        BufferedReader bufferedReader = new BufferedReader(isr);
     String str = "";
        while ((str = bufferedReader.readLine()) != null) {
            document.add(new Paragraph(str, font));
        }
        document.close();
    }
    public static void main(String[] args) throws Exception {
        String PDFTIMEDIR = "F:/pdf/";
        String text = PDFTIMEDIR + "1.txt";
        String pdf = PDFTIMEDIR + "1.txt.pdf";
        text2pdf(text, pdf);
    }
}
复制代码

simhei.ttf字体可从网上进行下载

项目中使用:

复制代码
package com.ksource.pwlp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import com.ksource.core.fastdfs.FastDfsUtil;
import com.ksource.core.fastdfs.FileResponseData;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 文本文件转pdf并上传到fastDFS
 * 并返回相应的参数
 * @author dxy
 *
 */
public class TxtToPdf {
    
    public FileResponseData txtToPdf(HttpServletRequest request, InputStream is, String pdf) throws Exception {
        String fontPath = request.getSession().getServletContext().getRealPath("/assets/vendor/font-awesome/fonts/simhei.ttf");
        Document document = new Document();
        OutputStream os = new FileOutputStream(new File(pdf));
        PdfWriter.getInstance(document, os);
        document.open();
        BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont);
        InputStreamReader isr = new InputStreamReader(is, "GBK");
        BufferedReader bufferedReader = new BufferedReader(isr);
        String str = "";
        while ((str = bufferedReader.readLine()) != null) {
            document.add(new Paragraph(str, font));
        }
        document.close();
        FileResponseData fileResponseData = FastDfsUtil.uploadToServerByPath(pdf);
        File file = new File(pdf);
        if(file.exists()){
            file.delete();
        }
        return fileResponseData;
    }
}
复制代码
posted @   岁月淡忘了谁  阅读(6651)  评论(4编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示