欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

java text类型转换_java语言实现Text格式转换成pdf文件

发布时间:2023/12/31 编程问答 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java text类型转换_java语言实现Text格式转换成pdf文件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

夹包在附件中 下载导入夹包既可

import java.io.BufferedReader;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

/**

* 将从.txt中读到的内容写到pdf中。

* */

public class GeneratePDF {

private final static String READFILEPATH = "E:\\2.txt";  //txt文件

private final static String WRITEFILEPATH = "E:\\2.pdf"; //生成的pdf文件

public static void main(String[] args) throws DocumentException,

IOException {

Document document = new Document(PageSize.A4, 80, 80, 60, 30);

PdfWriter.getInstance(document, new FileOutputStream(WRITEFILEPATH));

document.open();

BaseFont bfChinese = BaseFont.createFont("STSong-Light",

"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

Font FontChinese = new Font(bfChinese, 18, Font.NORMAL);

Paragraph t = new Paragraph("text转换成pdf", FontChinese);

t.setAlignment(Element.ALIGN_CENTER);

t.setLeading(30.0f);

document.add(t);

FontChinese = new Font(bfChinese, 11, Font.NORMAL);

BufferedReader read = null;

try {

read = new BufferedReader(new FileReader(READFILEPATH));

String line = null;

while ((line = read.readLine()) != null) {

t = new Paragraph(line, FontChinese);

t.setAlignment(Element.ALIGN_LEFT);

t.setLeading(20.0f);

document.add(t);

}

} catch (Exception e) {

System.out.println("目标文件不存,或者不可读!");

e.printStackTrace();

} finally {

try {

read.close();

document.close();

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println("============Build Success!===========");

}

}

总结

以上是生活随笔为你收集整理的java text类型转换_java语言实现Text格式转换成pdf文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。