首页 > 其他分享 >多格式转pdf——txt转pdf

多格式转pdf——txt转pdf

时间:2023-10-25 10:46:26浏览次数:29  
标签:null String fos 格式 fontPath new pdf txt document

 1     public static String txtToPdfConverter(String txtFilePath, String pdfFilePath, String fontPath) throws Exception {
 2         // 创建Document对象
 3         Document document = new Document();
 4         // 创建PdfWriter对象将文档写入文件
 5         PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
 6         // 打开文档
 7         document.open();
 8         // 设置字体,用于支持中文显示
 9         fontPath = fontPath + ",0";
10 //        System.out.println(fontPath);
11         BaseFont font = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
12         Font chineseFont = new Font(font, 12);
13         // 创建BufferedReader对象来读取文件
14         BufferedReader reader = new BufferedReader(new FileReader(urlToFile(new URL(txtFilePath))));
15         String line;
16         while ((line = reader.readLine()) != null) {
17             Paragraph paragraph = new Paragraph(line, chineseFont);
18             // 添加文本内容到文档
19             document.add(paragraph);
20         }
21         reader.close();
22         document.close();
23         //本地上传oss
24         return pdfFilePath;
25     }

 

 1     public static File urlToFile(URL url) {
 2         InputStream is = null;
 3         File file = null;
 4         FileOutputStream fos = null;
 5         try {
 6             file = File.createTempFile("tmp", null);
 7             URLConnection urlConn = null;
 8             urlConn = url.openConnection();
 9             is = urlConn.getInputStream();
10             fos = new FileOutputStream(file);
11             byte[] buffer = new byte[4096];
12             int length;
13             while ((length = is.read(buffer)) > 0) {
14                 fos.write(buffer, 0, length);
15             }
16             return file;
17         } catch (IOException e) {
18             return null;
19         } finally {
20             if (is != null) {
21                 try {
22                     is.close();
23                 } catch (IOException e) {
24                 }
25             }
26             if (fos != null) {
27                 try {
28                     fos.close();
29                 } catch (IOException e) {
30                 }
31             }
32         }
33     }

 

标签:null,String,fos,格式,fontPath,new,pdf,txt,document
From: https://www.cnblogs.com/Rover20230226/p/17786561.html

相关文章

  • 多格式转pdf——docx 转 pdf
    1publicstaticStringdocxToPdfConverter(StringdocxFilePath,StringpdfFilePath,StringfontPath)throwsException{2FileInputStreamfile=newFileInputStream(docxFilePath);3XWPFDocumentdocxDocument=newXWPFDocument(file)......
  • 多格式转pdf——md 转 pdf
    publicstaticStringmdToPdfConverter(StringmdFilePath,StringpdfFilePath,StringfontPath)throwsException{//创建Document对象Documentdocument=newDocument();//创建PdfWriter对象将文档写入文件PdfWriter.getInsta......
  • 多格式转pdf——doc 转 pdf
    publicstaticStringdocToPdfConverter(StringdocFilePath,StringpdfFilePath,StringfontPath)throwsException{FileInputStreamfile=newFileInputStream(docFilePath);HWPFDocumentdocDocument=newHWPFDocument(file);//......
  • 【畅联】H264视频流格式解析
      ......
  • 【专题】2022年中国跨境电商行业研究报告PDF合集分享(附原数据表)
    报告链接:http://tecdat.cn/?p=32044近年来,我国的跨境电子商务发展迅速,在过去五年中,其贸易额增长率达到了16.2%,已经成为稳定对外贸易的一支重要力量。阅读原文,获取专题报告合集全文,解锁文末52份跨境电商行业相关报告。一方面,随着跨境电子商务的发展,跨境电子商务的监管政策得到了......
  • 【专题】2023年B2B内容营销行业基准、预算及趋势报告PDF合集分享(附原数据表)
    报告链接:http://tecdat.cn/?p=32837在国内,B2B内容营销人才十分稀缺,尤其是当内容营销人才从媒体型向营销型转变时,内容营销的价值得以量化,进一步加强了内容营销人才对自身价值的认识。点击阅读原文,获取专题报告全文,解锁文末38份营销行业相关报告。优秀的内容人才,尤其是那些能够制......
  • 【专题】2022年中国制造业数字化转型研究报告PDF合集分享(附原数据表)
    报告链接:http://tecdat.cn/?p=32145本文中所说的制造业数字化转型,指的是在制造企业的设计、生产、管理、销售及服务的每一个环节中,将新一代信息技术应用到制造企业的设计、生产、管理、销售及服务的每一个环节中,并可以以每一个环节中产生的数据为基础,展开控制、监测、检测、预测......
  • Linux的命令基本格式
    因为对服务器来讲,图形界面会占用更多的系统资源,而且会安装更多的服务、开放更多的端口,这对服务器的稳定性和安全性都有负面影响。其实,服务器是一个连显示器都没有的家伙,要图形界面干十么?说到这里,有很多人会很崩溃。笔者就经常听到抱怨Linux是落后于时代的老古董,就像笔者的白头发......
  • 《富爸爸穷爸爸》PDF电子书分享
    为了学好理财,本人最近在整理一些理财书籍,穷爸爸富爸爸系列在理财书籍中常年位居第一,是必不可少的理财书籍以下是《富爸爸穷爸爸》电子书PDF百度云下载链接,有需要的可以下载下载地址:链接:https://pan.baidu.com/s/12-27-W-Gyz9Dl2EtJeLiYw提取码:p4dp复制这段内容后打开百度网......
  • WebGL编程指南pdf电子版
    WebGL编程指南pdf电子版作者:【美】KouichiMatsuda,RodgerLea(松田浩一,罗杰•李)出版年:2014-6ISBN:9787121229428连接提取码:m9lw对WebGL基础介绍详细,翻译的很好。着色器,点线三角形绘制,变换矩阵,MVP矩阵,光照和层次模型。入门WebGL很好的参考,读完可以轻松实现一个机器人行......