pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "学而时习之" <2907716...@qq.com>
Subject What is different from the input pdf file and output pdf file?
Date Tue, 24 Sep 2013 06:04:57 GMT
package copyfile;


import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.pdfbox.pdfparser.PDFStreamParser;
import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;


public class cpfile {
	static String inputfile = "d:/pdf分析/7.pdf";
	static String outputfile = inputfile.replace(".pdf", "cp.pdf");


	public static void main(String[] args) {
		dealone(inputfile);
	}


	private static void dealone(String f) {
		PDDocument document = null;
		try {
			document = PDDocument.load(f);
			PDDocumentCatalog catalog = document.getDocumentCatalog();
			List<PDPage> pages = catalog.getAllPages();
			for (Object pageObj : catalog.getAllPages()) {
				PDPage page = (PDPage) pageObj;
				PDFStreamParser parser = new PDFStreamParser(page.getContents());
				parser.parse();
				List tokens = parser.getTokens();


				PDStream newContents = new PDStream(document);
				ContentStreamWriter writer = new ContentStreamWriter(
						newContents.createOutputStream());
				writer.writeTokens(tokens);
				newContents.addCompression();
				page.setContents(newContents);
			}
			document.save(outputfile);


		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				document.close();
			} catch (Exception e) {
				e.printStackTrace();
			}


		}


	}
}
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message