zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Kubernetes 升级过程记录:从 1.17.0 升级至最新版 1.20.2
    歼10:职业有个基本属性,为了糊口。然后再区分 主动 or 被动,被动者只是为了糊口,那自然是看不起自己的职业
    红胖子:写给人生的九封信,愿你的人生淡定从容,繁华似锦!!!
    Embarcadero MVP(68位,全部都有个人主页)
    Delphi 26 岁
    C# Assembly 与 Reflection
    你精通那么多技术,为何还是做不好一个项目
    Linux 6 个“吓人”的 命令
    Distributed Application runtime
    RocketMQ and kafka
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/11809927.html
Copyright © 2011-2022 走看看