<文本文档和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转为文本文档的过程。其他类型的转化和细节使用,

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















相关文章
|
2月前
|
XML 缓存 JSON
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
203 0
|
4天前
|
Java Apache Maven
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
|
4天前
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
12 1
|
14天前
|
索引 Python
PDF文件页面提取操作小指南
PDF文件页面提取操作小指南
39 4
|
14天前
|
Python
Python对PDF文件页面的旋转和切割
Python对PDF文件页面的旋转和切割
31 3
|
14天前
|
计算机视觉 Python
Python操作PDF文件
Python操作PDF文件
19 1
|
16天前
|
JSON 数据格式
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
42 2
|
16天前
|
人工智能 计算机视觉 Python
ChatGPT编程省钱、方便小示例——实现PDF转成PNG文件
ChatGPT编程省钱、方便小示例——实现PDF转成PNG文件
25 1
|
17天前
|
存储 安全 网络安全
Python编程--使用PyPDF解析PDF文件中的元数据
Python编程--使用PyPDF解析PDF文件中的元数据
22 1
|
5天前
|
JavaScript 前端开发 容器
Vue生成PDF文件攻略:html2canvas与jspdf联手,中文乱码与自动换行难题攻克
Vue生成PDF文件攻略:html2canvas与jspdf联手,中文乱码与自动换行难题攻克
11 0

热门文章

最新文章