赞
踩
目录
- package document;
-
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
-
- public class Adding_Pages {
-
- public static void main(String args[]) throws IOException {
-
- //创建 PDF 文档对象
- PDDocument document = new PDDocument();
-
- for (int i=0; i<10; i++) {
- //创建空白页
- PDPage blankPage = new PDPage();
-
- //将空白页添加到文档
- document.addPage( blankPage );
- }
-
- //保存文档
- document.save("C:/PdfBox_Examples/my_doc.pdf");
- System.out.println("PDF created");
-
- //关闭文档
- document.close();
-
- }
- }

- import java.io.File;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- public class LoadingExistingDocument {
-
- public static void main(String args[]) throws IOException {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/sample.pdf");
- PDDocument document = PDDocument.load(file);
-
- System.out.println("PDF loaded");
-
- //在文档中添加空白页
- document.addPage(new PDPage());
-
- //保存文档
- document.save("C:/PdfBox_Examples/sample.pdf");
-
- //关闭文档
- document.close();
-
- }
- }

- import java.io.IOException;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDDocumentInformation;
- import org.apache.pdfbox.pdmodel.PDPage;
-
- public class AddingDocumentAttributes {
- public static void main(String args[]) throws IOException {
-
- //创建 PDF 文档对象
- PDDocument document = new PDDocument();
-
- //创建空白页
- PDPage blankPage = new PDPage();
-
- //将空白页添加到文档
- document.addPage( blankPage );
-
- //创建 PDDocumentInformation 对象
- PDDocumentInformation pdd = document.getDocumentInformation();
-
- //设置文档的作者
- pdd.setAuthor("Learnfk");
-
- //设置文档的标题
- pdd.setTitle("Sample document");
-
- //设置文档的创建者
- pdd.setCreator("PDF Examples");
-
- //设置文档的主题
- pdd.setSubject("Example document");
-
- //设置文档的创建日期
- Calendar date = new GregorianCalendar();
- date.set(2015, 11, 5);
- pdd.setCreationDate(date);
- //设置文档的修改日期
- date.set(2016, 6, 5);
- pdd.setModificationDate(date);
-
- //为文档设置关键字
- pdd.setKeywords("sample, first example, my pdf");
-
- //保存文档
- document.save("C:/PdfBox_Examples/doc_attributes.pdf");
-
- System.out.println("Properties added successfully ");
-
- //关闭文档
- document.close();
-
- }
- }

- import java.io.File;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDDocumentInformation;
-
- public class RetrivingDocumentAttributes {
- public static void main(String args[]) throws IOException {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/doc_attributes.pdf")
- PDDocument document = PDDocument.load(file);
- //获取 PDDocumentInformation 对象
- PDDocumentInformation pdd = document.getDocumentInformation();
-
- //检索 PDF 文档的信息
- System.out.println("Author of the document is :"+ pdd.getAuthor());
- System.out.println("Title of the document is :"+ pdd.getTitle());
- System.out.println("Subject of the document is :"+ pdd.getSubject());
-
- System.out.println("Creator of the document is :"+ pdd.getCreator());
- System.out.println("Creation date of the document is :"+ pdd.getCreationDate());
- System.out.println("Modification date of the document is :"+
- pdd.getModificationDate());
- System.out.println("Keywords of the document are :"+ pdd.getKeywords());
-
- //关闭文档
- document.close();
- }
- }

- import java.io.File;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- import org.apache.pdfbox.pdmodel.PDPageContentStream;
- import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
- public class AddingContent {
- public static void main (String args[]) throws IOException {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/my_doc.pdf");
- PDDocument document = PDDocument.load(file);
-
- //检索文档的页面
- PDPage page = document.getPage(1);
- PDPageContentStream contentStream = new PDPageContentStream(document, page);
-
- //开始内容流
- contentStream.beginText();
-
- //将字体设置为内容流
- contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
-
- //设置线的位置
- contentStream.newLineAtOffset(25, 500);
-
- String text = "This is the sample document and we are adding content to it.";
-
- //以字符串形式添加文本
- contentStream.showText(text);
-
- //结束内容流
- contentStream.endText();
-
- System.out.println("Content added");
-
- //关闭内容流
- contentStream.close();
-
- //保存文档
- document.save(new File("C:/PdfBox_Examples/new.pdf"));
-
- //关闭文档
- document.close();
- }
- }

- import java.io.File;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- import org.apache.pdfbox.pdmodel.PDPageContentStream;
- import org.apache.pdfbox.pdmodel.font.PDType1Font;
-
- public class AddMultipleLines {
- public static void main(String args[]) throws IOException {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/my_pdf.pdf");
- PDDocument doc = document.load(file);
-
- //创建PDF文档
- PDPage page = doc.getPage(1);
-
- PDPageContentStream contentStream = new PDPageContentStream(doc, page);
-
- //开始内容流
- contentStream.beginText();
-
- //将字体设置为内容流
- contentStream.setFont( PDType1Font.TIMES_ROMAN, 16 );
-
- //设置Lead
- contentStream.setLeading(14.5f);
-
- //设置Line的位置
- contentStream.newLineAtOffset(25, 725);
-
- String text1 = "This is an example of adding text to a page in the pdf document.
- we can add as many lines";
- String text2 = "as we want like this using the ShowText() method of the
- ContentStream class";
-
- //以字符串的形式添加文本
- contentStream. ShowText(text1);
- contentStream.newLine();
- contentStream. ShowText(text2);
- //结束内容流
- contentStream.endText();
-
- System.out.println("Content added");
-
- //关闭内容流
- contentStream.close();
-
- //保存文档
- doc.save(new File("C:/PdfBox_Examples/new.pdf"));
-
- //关闭文档
- doc.close();
- }
- }

- import java.io.File;
- import java.io.IOException;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.text.PDFTextStripper;
- public class ReadingText {
-
- public static void main(String args[]) throws IOException {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/new.pdf");
- PDDocument document = PDDocument.load(file);
-
- //实例化PDFTextStropper类
- PDFTextStripper pdfStripper = new PDFTextStripper();
-
- //从PDF文档中检索文本
- String text = pdfStripper.getText(document);
- System.out.println(text);
-
- //关闭文件
- document.close();
-
- }
- }

- import java.io.File;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- import org.apache.pdfbox.pdmodel.PDPageContentStream;
- import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
-
- public class InsertingImage {
-
- public static void main(String args[]) throws Exception {
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/sample.pdf");
- PDDocument doc = PDDocument.load(file);
-
- //检索页面
- PDPage page = doc.getPage(0);
-
- //创建PDImagexobject对象
- PDImageXObject pdImage = PDImageXObject.createFromFile("C:/PdfBox_Examples/logo.png",doc);
-
- //创建PDPageContentStream对象
- PDPageContentStream contents = new PDPageContentStream(doc, page);
-
- //在PDF文档中绘制图像
- contents.drawImage(pdImage, 70, 250);
-
- System.out.println("Image inserted");
-
- //关闭PDPageContentStream对象
- contents.close();
-
- //保存文档
- doc.save("C:/PdfBox_Examples/sample.pdf");
-
- //关闭文件
- doc.close();
-
- }
- }

- import java.io.File;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
- import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
- public class EncriptingPDF {
-
- public static void main(String args[]) throws Exception {
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/sample.pdf");
- PDDocument document = PDDocument.load(file);
-
- //创建访问权限对象
- AccessPermission ap = new AccessPermission();
-
- //创建标准化的支持policy对象
- StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
-
- //设置加密密钥的长度
- spp.setEncryptionKeyLength(128);
-
- //设置访问权限
- spp.setPermissions(ap);
-
- //保护文件
- document.protect(spp);
-
- System.out.println("Document encrypted");
-
- //保存文档
- document.save("C:/PdfBox_Examples/sample.pdf");
- //关闭文件
- document.close();
-
- }
- }

- import java.io.File;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
-
- public class AddJavaScript {
-
- public static void main(String args[]) throws Exception {
-
- //加载现有文件
- File file = new File("C:/PdfBox_Examples/new.pdf");
- PDDocument document = PDDocument.load(file);
-
- String javaScript = "app.alert( {cMsg: 'this is an example', nIcon: 3,"
- + " nType: 0, cTitle: 'PDFBox Javascript example'} );";
-
- //创建PDActionjavascript对象
- PDActionJavaScript PDAjavascript = new PDActionJavaScript(javaScript);
-
- //嵌入JavaScript
- document.getDocumentCatalog().setOpenAction(PDAjavascript);
-
- //保存文档
- document.save( new File("C:/PdfBox_Examples/new.pdf") );
- System.out.println("Data added to the given PDF");
-
- //关闭文件
- document.close();
-
- }
- }

- import org.apache.pdfbox.multipdf.Splitter;
- import org.apache.pdfbox.pdmodel.PDDocument;
-
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import java.util.Iterator;
-
- public class SplitPages {
- public static void main(String[] args) throws IOException {
-
- //加载现有的PDF文档
- File file = new File("C:/PdfBox_Examples/sample.pdf");
- PDDocument document = PDDocument.load(file);
-
- //实例化Splitter类
- Splitter splitter = new Splitter();
-
- //分割PDF文档的页面
- List<PDDocument> Pages = splitter.split(document);
-
- //创建迭代器
- Iterator<PDDocument> iterator = Pages.listIterator();
-
- //将每个页面保存为单个文档
- int i = 1;
- while(iterator.hasNext()) {
- PDDocument pd = iterator.next();
- pd.save("C:/PdfBox_Examples/sample"+ i++ +".pdf");
- }
- System.out.println("Multiple PDF's created");
- document.close();
- }
- }

- import org.apache.pdfbox.multipdf.PDFMergerUtility;
- import org.apache.pdfbox.pdmodel.PDDocument;
-
- import java.io.File;
- import java.io.IOException;
-
- public class MergePDFs {
- public static void main(String[] args) throws IOException {
-
- //加载现有的PDF文档
- File file1 = new File("C:/PdfBox_Examples/sample1.pdf");
- PDDocument doc1 = PDDocument.load(file1);
-
- File file2 = new File("C:/PdfBox_Examples/sample2.pdf");
- PDDocument doc2 = PDDocument.load(file2);
-
- //实例化PDFmergerutility类
- PDFMergerUtility PDFmerger = new PDFMergerUtility();
-
- //设置目标文件
- PDFmerger.setDestinationFileName("C:/PdfBox_Examples/merged.pdf");
-
- //添加源文件
- PDFmerger.addSource(file1);
- PDFmerger.addSource(file2);
-
- //合并两份文件
- PDFmerger.mergeDocuments();
-
- System.out.println("Documents merged");
- //关闭文件
- doc1.close();
- doc2.close();
- }
-
- }

- import java.awt.image.BufferedImage;
- import java.io.File;
-
- import javax.imageio.ImageIO;
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.rendering.PDFRenderer;
- public class PdfToImage {
-
- public static void main(String args[]) throws Exception {
-
- //加载现有的PDF文档
- File file = new File("C:/PdfBox_Examples/sample.pdf");
- PDDocument document = PDDocument.load(file);
-
- //实例化PDFRenderer类
- PDFRenderer renderer = new PDFRenderer(document);
-
- //从PDF文档渲染图像
- BufferedImage image = renderer.renderImage(0);
-
- //将图像写入文件
- ImageIO.write(image, "JPEG", new File("C:/PdfBox_Examples/myimage.jpg"));
-
- System.out.println("Image created");
-
- //Closing the document
- document.close();
-
- }
- }

- import java.awt.Color;
- import java.io.File;
-
- import org.apache.pdfbox.pdmodel.PDDocument;
- import org.apache.pdfbox.pdmodel.PDPage;
- import org.apache.pdfbox.pdmodel.PDPageContentStream;
- public class ShowColorBoxes {
-
- public static void main(String args[]) throws Exception {
-
- //加载现有文档
- File file = new File("C:/PdfBox_Examples/BlankPage.pdf");
- PDDocument document = PDDocument.load(file);
-
- //检索PDF文件的页面
- PDPage page = document.getPage(0);
-
- //实例化PDPAGEContentStream类
- PDPageContentStream contentStream = new PDPageContentStream(document, page);
-
- //设置NonStroking颜色
- contentStream.setNonStrokingColor(Color.DARK_GRAY);
-
- //绘制一个矩形
- contentStream.addRect(200, 650, 100, 100);
-
- //绘制一个矩形
- contentStream.fill();
-
- System.out.println("rectangle added");
-
- //关闭contentStream对象
- contentStream.close();
-
- //保存文档
- File file1 = new File("C:/PdfBox_Examples/colorbox.pdf");
- document.save(file1);
-
- //关闭文件
- document.close();
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。