<文本文档和pdf文件互相转化>入门练习

简介: 首先会需要两个jar包文件 itext-5.5.5和text-asian.jar,可以在我上传的资源处下载。 将这两个jar包添加到项目中。 然后编写简单的测试Demo /** * */package com.skd.util;import java.io.BufferedReader;import java.io.File;import java.io.Fil

首先会需要两个jar包文件 itext-5.5.5和text-asian.jar,可以在我上传的资源处下载。

将这两个jar包添加到项目中。

然后编写简单的测试Demo


/**
 * 
 */
package com.skd.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

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.PdfWriter;

/**
 * @author JING
 * @date 2015年4月28日
 * @time  下午9:11:14
 * @fileName Office2PDF.java
 * @function 
 */
public class Office2PDF {
	static final String PDF_SUFFIX=".PDF";
	static final String TXT_SUFFIX=".txt";
	static final String JAVA_SUFFIX=".java";
	static final String DOCX_SUFFIX=".docx";
	static final String DOC_SUFFIX=".doc";

	/**
	 * @param args
	 * @throws Exception 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, Exception {
		//这两个路径可以指定为自己的文本文件路径,不带扩展名
		String filePath="D:\\test_data\\开发";
		String pdfPath=filePath+PDF_SUFFIX;
		String txtPath=filePath+JAVA_SUFFIX;
		//文本文件转为pdf文件
		//txt2Pdf(txtPath, pdfPath);
		//获取pdf文件中的内容并保存在同名的文本文件中
		//getPdf(pdfPath);
		System.out.println("结束");
	}
	
	/**
	 * 获取pdf文件中的内容并保存在同名的文本文件中
	 * @param pdfPath
	 * @throws IOException
	 */
	public static void getPdf(String pdfPath) throws IOException {
		//是否排序
		boolean sort=false;
		//pdf文件名
		String fileName=pdfPath;
		//读取文件的内容
		String pdfContent=null;
		//编码方式
		String encoding="UTF-8";
		//开始提取页
		int startPage=1;
		//结束提取页
		int endPage=Integer.MAX_VALUE;
		//文件输入流
		Writer writer=null;
		PDDocument doc=null;
		doc=PDDocument.load(fileName);
		
		if(fileName.length() > 4){
            //以原来pdf名称来命名新产生的txt文件
            File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");
            fileName = outputFile.getPath();
        }
		//文件输出流,写入到filename中
		writer=new OutputStreamWriter(new FileOutputStream(fileName), encoding);
		PDFTextStripper pdfTextStripper = new PDFTextStripper();
		pdfTextStripper.setSortByPosition(sort);
		pdfTextStripper.setStartPage(startPage);
		pdfTextStripper.setEndPage(endPage);
		//调用PDFTextStripper的writeText
		pdfTextStripper.writeText(doc,writer );
		writer.close();
		 if(writer != null){
         }
		 doc.close();
         if(doc != null){
         }
	}
	
	/**
	 * 文本文件转为pdf文件
	 * @param txtPath 
	 * @param pdfPath
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void txt2Pdf(String txtPath,String pdfPath) throws IOException, DocumentException {
		Document document = new Document();
		//系统字体的路径C:\Windows\Fonts\SIMKAI.TTF 楷体常规 放置在项目src下的res下。不设置字体不显示中文
		//BaseFont bfChinese = BaseFont.createFont("res/SIMKAI.TTF",BaseFont.IDENTITY_H,   BaseFont.NOT_EMBEDDED);  //"fonts/UniGB-UCS2-H"   
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",   BaseFont.EMBEDDED);   
	   Font FontChinese = new Font(bfChinese, 12,Font.NORMAL);  
		try {
			BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(txtPath)));
			OutputStream os=new FileOutputStream(pdfPath);
			PdfWriter.getInstance(document, os);
			document.open();
			String cache=null;
			while((cache=reader.readLine())!=null){
				document.add(new Paragraph(cache, FontChinese));
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			document.close();
		}	
	}
}



上面代码中的两个方法就是文本文档转为pdf,然后pdf转为文本文档的过程。其他类型的转化和细节使用,

待以后继续测试使用后再来分享经验















相关文章
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
62 0
|
1月前
|
存储 缓存 Python
如何使用Python抓取PDF文件并自动下载到本地
如何使用Python抓取PDF文件并自动下载到本地
31 0
|
3月前
|
Java API Apache
使用 Apache PDFBox 操作PDF文件
Apache PDFBox库是一个开源的Java工具,专门用于处理PDF文档。它允许用户创建全新的PDF文件,编辑现有的PDF文档,以及从PDF文件中提取内容。此外,Apache PDFBox还提供了一些命令行实用工具。
100 6
|
3月前
|
存储
Vue3 实现 PDF 文件在线预览功能
Vue3 实现 PDF 文件在线预览功能
292 0
|
2月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
62 1
|
1月前
|
JSON JavaScript 前端开发
vue项目使用Print.js插件实现PDF文件打印
vue项目使用Print.js插件实现PDF文件打印
33 0
|
1月前
|
Shell Python
Python生成PDF文件
Python生成PDF文件
22 0
|
1月前
|
前端开发
前端实现生成pdf文件并下载
前端实现生成pdf文件并下载
35 1
|
2月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
97 0
|
2月前
|
数据安全/隐私保护 Python Windows
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)
44 0