使用ITEXT操作PDF文件
生活随笔
收集整理的這篇文章主要介紹了
使用ITEXT操作PDF文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
使用ITEXT操作PDF文件
pom.xml
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.0.6</version> </dependency>demo.java?
package com.spinach.home.pdf;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;import com.itextpdf.text.Anchor; import com.itextpdf.text.Annotation; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.List; import com.itextpdf.text.ListItem; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfAction; import com.itextpdf.text.pdf.PdfAnnotation; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfName; import com.itextpdf.text.pdf.PdfOutline; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfString; import com.itextpdf.text.pdf.PdfTransition; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.DottedLineSeparator; import com.itextpdf.text.pdf.draw.LineSeparator; import com.itextpdf.text.pdf.draw.VerticalPositionMark;/*** @ClassName: PDFInit* @Description: TODO PDF 操作* @author: V7* @date: 2016年5月6日 下午4:41:18*/ public class PDFInit {/*** @Title: main* @Description: TODO 測(cè)試 PDF 入口* @param args* @return: void*/public static final String FILE_DIR = "";public static Document document;public static void main(String[] args) throws Exception {// TODO Auto-generated method stub// createPDF();// 生成一個(gè) PDF 文件// decoratePDF();// 設(shè)置 PDF 的頁面大小 和 背景顏色// createPDFAddPassWord();// 設(shè)置 PDF 的密碼// createPDFAddNewPages();// 添加頁// addWaterMark();//為 PDF 文件添加圖片水印,文字水印,背景圖ce();//addContent();//插入Chunk, Phrase, Paragraph, List// addExtraContent();//插入Anchor, Image, Chapter, Section// draw();//畫圖// setAlignment();//設(shè)置段落// deletePage();//刪除 page// insertPage();// 插入 page/* * splitPDF();//分割 page mergePDF();// 合并 PDF 文件 */// sortpage();// 排序pageaddOutline();// 目錄// setHeaderFooter();// 頁眉,頁腳// addColumnText();// 文字左右文字// setSlideshow();// 文檔視圖// zipPDF();// 壓縮PDF到Zip// setAnnotation();// 注釋}/*** @Title: createPDF* @Description: TODO 創(chuàng)建一個(gè) PDF 文件,并添加文本* @throws FileNotFoundException* @throws DocumentException* @return: void*/public static void createPDF() throws FileNotFoundException, DocumentException {// 第一步:實(shí)例化 documentdocument = new Document();// 第二步:生成文件PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDF.pdf"));// 第三步:打開 documentdocument.open();// 第四步:添加文本document.add(new Paragraph("Hello World"));// 第五步:關(guān)閉 documentdocument.close();}/*** @Title: decoratePDF* @Description: TODO 創(chuàng)建一個(gè) PDF 文件,修改文件的屬性* @throws FileNotFoundException* @throws DocumentException* @return: void*/public static void decoratePDF() throws FileNotFoundException, DocumentException {// 頁面大小Rectangle rect = new Rectangle(PageSize.B6.rotate());// 頁面背景色rect.setBackgroundColor(BaseColor.ORANGE);document = new Document(rect);PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createdecoratePDF.pdf"));// PDF版本(默認(rèn)1.4)writer.setPdfVersion(PdfWriter.VERSION_1_7);// 文檔屬性document.addTitle("Title@sample");document.addAuthor("Author@rensanning");document.addSubject("Subject@iText sample");document.addKeywords("Keywords@iText");document.addCreator("Creator@iText");// 頁邊空白document.setMargins(10, 20, 80, 10);// 打開document.open();document.add(new Paragraph("Hello World"));// 關(guān)閉document.close();}/*** @Title: createPDFAddPassWord* @Description: TODO 創(chuàng)建一個(gè) PDF 文件,并為該文件設(shè)置密碼* @throws FileNotFoundException* @throws DocumentException* @return: void*/public static void createPDFAddPassWord() throws FileNotFoundException, DocumentException {// 頁面大小Rectangle rect = new Rectangle(PageSize.B5.rotate());// 頁面背景色rect.setBackgroundColor(BaseColor.GREEN);document = new Document(rect);PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFAddPassWord.pdf"));// 設(shè)置密碼為:"World"writer.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.ALLOW_SCREENREADERS, PdfWriter.STANDARD_ENCRYPTION_128);// opendocument.open();document.add(new Paragraph("Hello World"));// closedocument.close();}/*** @Title: createPDFAddNewPages* @Description: TODO 創(chuàng)建一個(gè) PDF 文件,并添加新的頁* @throws FileNotFoundException* @throws DocumentException* @return: void*/public static void createPDFAddNewPages() throws FileNotFoundException, DocumentException {document = new Document();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "createPDFAddNewPages.pdf"));// opendocument.open();document.add(new Paragraph("First page"));// 添加版本號(hào)..document.add(new Paragraph(Document.getProduct()));// 添加一頁document.newPage();document.add(new Paragraph("two page"));writer.setPageEmpty(true);// 添加一頁document.newPage();document.add(new Paragraph("three page"));document.close();}/*** @Title: addWaterMark* @Description: TODO 為 PDF 文件添加水印,背景圖* @return: void* @throws IOException* @throws DocumentException* @throws DocumentException*/public static void addWaterMark() throws IOException, DocumentException {FileOutputStream out = new FileOutputStream(FILE_DIR + "addWaterMark.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();document.add(new Paragraph("First page"));document.newPage();document.add(new Paragraph("New page"));document.newPage();document.add(new Paragraph("Third page"));document.close();// 圖片水印PdfReader reader = new PdfReader(FILE_DIR + "addWaterMark.pdf");PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR + "addWaterMark2.pdf"));Image img = Image.getInstance("resource/watermark.jpg");img.setAbsolutePosition(200, 400);PdfContentByte under = stamp.getUnderContent(1);under.addImage(img);// 文字水印PdfContentByte over = stamp.getOverContent(2);// 加載字庫來完成對(duì)字體的創(chuàng)建BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);over.beginText();// 設(shè)置顏色 默認(rèn)為藍(lán)色over.setColorFill(BaseColor.RED);// 設(shè)置字體字號(hào)over.setFontAndSize(bf, 50);// 設(shè)置起始位置over.setTextMatrix(30, 30);// 開始寫入水印 左-下-傾斜度over.showTextAligned(Element.ALIGN_LEFT, "KaiYuTechnology", 245, 400, 30);over.endText();// 背景圖Image img2 = Image.getInstance("resource/test.jpg");img2.setAbsolutePosition(0, 0);PdfContentByte under2 = stamp.getUnderContent(3);under2.addImage(img2);stamp.close();reader.close();}/*** @Title: addContent* @Description: TODO 插入Chunk, Phrase, Paragraph, List* @throws DocumentException* @throws FileNotFoundException* @return: void*/public static void addContent() throws DocumentException, FileNotFoundException {FileOutputStream out = new FileOutputStream(FILE_DIR + "addContent.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();// Chunk對(duì)象: a String, a Font, and some attributesdocument.add(new Chunk("China"));document.add(new Chunk(" "));Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);Chunk id = new Chunk("chinese", font);id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);id.setTextRise(7);document.add(id);document.add(Chunk.NEWLINE);document.add(new Chunk("Japan"));document.add(new Chunk(" "));Font font2 = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);Chunk id2 = new Chunk("japanese", font2);id2.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);id2.setTextRise(3);id2.setUnderline(0.2f, -2f);document.add(id2);document.add(Chunk.NEWLINE);// Phrase對(duì)象: a List of Chunks with leadingdocument.newPage();document.add(new Phrase("Phrase page"));Phrase director = new Phrase();Chunk name = new Chunk("China");name.setUnderline(0.2f, -2f);director.add(name);director.add(new Chunk(","));director.add(new Chunk(" "));director.add(new Chunk("chinese"));director.setLeading(24);document.add(director);Phrase director2 = new Phrase();Chunk name2 = new Chunk("Japan");name2.setUnderline(0.2f, -2f);director2.add(name2);director2.add(new Chunk(","));director2.add(new Chunk(" "));director2.add(new Chunk("japanese"));director2.setLeading(24);document.add(director2);// Paragraph對(duì)象: a Phrase with extra properties and a newlinedocument.newPage();document.add(new Paragraph("Paragraph page"));Paragraph info = new Paragraph();info.add(new Chunk("China "));info.add(new Chunk("chinese"));info.add(Chunk.NEWLINE);info.add(new Phrase("Japan "));info.add(new Phrase("japanese"));document.add(info);// List對(duì)象: a sequence of Paragraphs called ListItemdocument.newPage();List list = new List(List.ORDERED);for (int i = 0; i < 10; i++) {ListItem item = new ListItem(String.format("%s: %d movies", "country" + (i + 1), (i + 1) * 100), new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE));List movielist = new List(List.ORDERED, List.ALPHABETICAL);movielist.setLowercase(List.LOWERCASE);for (int j = 0; j < 5; j++) {ListItem movieitem = new ListItem("Title" + (j + 1));List directorlist = new List(List.UNORDERED);for (int k = 0; k < 3; k++) {directorlist.add(String.format("%s, %s", "Name1" + (k + 1), "Name2" + (k + 1)));}movieitem.add(directorlist);movielist.add(movieitem);}item.add(movielist);list.add(item);}document.add(list);document.close();}/*** @Title: addExtraContent* @Description: TODO 插入Anchor, Image, Chapter, Section* @throws DocumentException* @throws MalformedURLException* @throws IOException* @return: void*/public static void addExtraContent() throws DocumentException, MalformedURLException, IOException {FileOutputStream out = new FileOutputStream(FILE_DIR + "addExtraContent.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();// Anchor對(duì)象: internal and external linksParagraph country = new Paragraph();Anchor dest = new Anchor("目的地", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));dest.setName("CN");dest.setReference("https://www.baidu.com/");// externalcountry.add(dest);country.add(String.format(": %d sites", 10000));document.add(country);document.newPage();Anchor toUS = new Anchor("跳轉(zhuǎn)->Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));toUS.setReference("#CN");// internaldocument.add(toUS);// Image對(duì)象document.newPage();Image img = Image.getInstance("resource/test.jpg");img.setAlignment(Image.LEFT | Image.TEXTWRAP);img.setBorder(Image.BOX);img.setBorderWidth(10);img.setBorderColor(BaseColor.WHITE);img.scaleToFit(1000, 72);// 大小img.setRotationDegrees(-30);// 旋轉(zhuǎn)document.add(img);// Chapter, Section對(duì)象(目錄)document.newPage();Paragraph title = new Paragraph("Title");Chapter chapter = new Chapter(title, 1);// 標(biāo)題和序號(hào)title = new Paragraph("Section A");Section section = chapter.addSection(title);section.setBookmarkTitle("bmk");section.setIndentation(30);section.setBookmarkOpen(false);section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);Section subsection = section.addSection(new Paragraph("Sub Section A"));subsection.setIndentationLeft(20);subsection.setNumberDepth(1);document.add(chapter);document.close();}/*** @Title: draw* @Description: TODO 畫圖* @throws Exception* @return: void*/public static void draw() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "draw.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();// 左右箭頭document.add(new VerticalPositionMark() {public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {canvas.beginText();BaseFont bf = null;try {bf = BaseFont.createFont(BaseFont.ZAPFDINGBATS, "", BaseFont.EMBEDDED);} catch (Exception e) {e.printStackTrace();}canvas.setFontAndSize(bf, 12);// LEFTcanvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), llx - 10, y, 0);// RIGHTcanvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char) 220), urx + 10, y + 8, 180);canvas.endText();}});// 直線Paragraph p1 = new Paragraph("LEFT");p1.add(new Chunk(new LineSeparator()));p1.add("R");document.add(p1);// 點(diǎn)線Paragraph p2 = new Paragraph("LEFT");p2.add(new Chunk(new DottedLineSeparator()));p2.add("R");document.add(p2);// 下滑線LineSeparator UNDERLINE = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);Paragraph p3 = new Paragraph("NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");p3.add(UNDERLINE);document.add(p3);document.close();}/*** @Title: setAlignment* @Description: TODO 設(shè)置段落* @throws Exception* @return: void*/public static void setAlignment() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "setAlignment.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();Paragraph p = new Paragraph("In the previous example, you added a header and footer with the showTextAligned() method. This example demonstrates that it’s sometimes more interesting to use PdfPTable and writeSelectedRows(). You can define a bottom border for each cell so that the header is underlined. This is the most elegant way to add headers and footers, because the table mechanism allows you to position and align lines, images, and text.");// 默認(rèn)p.setAlignment(Element.ALIGN_JUSTIFIED);document.add(p);// 慢慢的向右移動(dòng)document.newPage();p.setAlignment(Element.ALIGN_JUSTIFIED);p.setIndentationLeft(1 * 15f);p.setIndentationRight((5 - 1) * 15f);document.add(p);// 居右document.newPage();p.setAlignment(Element.ALIGN_RIGHT);p.setSpacingAfter(15f);document.add(p);// 居左document.newPage();p.setAlignment(Element.ALIGN_LEFT);p.setSpacingBefore(15f);document.add(p);// 居中document.newPage();p.setAlignment(Element.ALIGN_CENTER);p.setSpacingAfter(15f);p.setSpacingBefore(15f);document.add(p);document.close();}/*** @Title: deletePage* @Description: TODO 刪除 page* @throws Exception* @return: void*/public static void deletePage() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "deletePage.pdf");document = new Document();PdfWriter writer = PdfWriter.getInstance(document, out);document.open();document.add(new Paragraph("First page"));document.add(new Paragraph(Document.getVersion()));document.newPage();writer.setPageEmpty(false);document.newPage();document.add(new Paragraph("New page"));document.close();PdfReader reader = new PdfReader(FILE_DIR + "deletePage.pdf");reader.selectPages("1,3");PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR + "deletePage2.pdf"));stamp.close();reader.close();}/*** @Title: insertPage* @Description: TODO 插入 page* @throws Exception* @return: void*/public static void insertPage() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "insertPage.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();document.add(new Paragraph("1 page"));document.newPage();document.add(new Paragraph("2 page"));document.newPage();document.add(new Paragraph("3 page"));document.close();PdfReader reader = new PdfReader(FILE_DIR + "insertPage.pdf");PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR + "insertPage2.pdf"));stamp.insertPage(2, reader.getPageSize(1));ColumnText ct = new ColumnText(null);ct.addElement(new Paragraph(24, new Chunk("INSERT PAGE")));ct.setCanvas(stamp.getOverContent(2));ct.setSimpleColumn(36, 36, 559, 770);stamp.close();reader.close();}/*** @Title: splitPDF* @Description: TODO 分割 page* @throws Exception* @return: void*/public static void splitPDF() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "splitPDF.pdf");document = new Document();PdfWriter.getInstance(document, out);document.open();document.add(new Paragraph("1 page"));document.newPage();document.add(new Paragraph("2 page"));document.newPage();document.add(new Paragraph("3 page"));document.newPage();document.add(new Paragraph("4 page"));document.close();PdfReader reader = new PdfReader(FILE_DIR + "splitPDF.pdf");Document dd = new Document();PdfWriter writer = PdfWriter.getInstance(dd, new FileOutputStream(FILE_DIR + "splitPDF1.pdf"));dd.open();PdfContentByte cb = writer.getDirectContent();dd.newPage();cb.addTemplate(writer.getImportedPage(reader, 1), 0, 0);dd.newPage();cb.addTemplate(writer.getImportedPage(reader, 2), 0, 0);dd.close();writer.close();Document dd2 = new Document();PdfWriter writer2 = PdfWriter.getInstance(dd2, new FileOutputStream(FILE_DIR + "splitPDF2.pdf"));dd2.open();PdfContentByte cb2 = writer2.getDirectContent();dd2.newPage();cb2.addTemplate(writer2.getImportedPage(reader, 3), 0, 0);dd2.newPage();cb2.addTemplate(writer2.getImportedPage(reader, 4), 0, 0);dd2.close();writer2.close();}/*** @Title: mergePDF* @Description: TODO 合并 PDF 文件* @throws Exception* @return: void*/public static void mergePDF() throws Exception {PdfReader reader1 = new PdfReader(FILE_DIR + "splitPDF1.pdf");PdfReader reader2 = new PdfReader(FILE_DIR + "splitPDF2.pdf");FileOutputStream out = new FileOutputStream(FILE_DIR + "mergePDF.pdf");Document document = new Document();PdfWriter writer = PdfWriter.getInstance(document, out);document.open();PdfContentByte cb = writer.getDirectContent();@SuppressWarnings("unused")int totalPages = 0;totalPages += reader1.getNumberOfPages();totalPages += reader2.getNumberOfPages();java.util.List<PdfReader> readers = new ArrayList<PdfReader>();readers.add(reader1);readers.add(reader2);int pageOfCurrentReaderPDF = 0;Iterator<PdfReader> iteratorPDFReader = readers.iterator();// Loop through the PDF files and add to the output.while (iteratorPDFReader.hasNext()) {PdfReader pdfReader = iteratorPDFReader.next();// Create a new page in the target for each source page.while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {document.newPage();pageOfCurrentReaderPDF++;PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);cb.addTemplate(page, 0, 0);}pageOfCurrentReaderPDF = 0;}out.flush();document.close();out.close();}/*** @Title: sortpage* @Description: TODO 排序page* @throws Exception* @return: void*/public static void sortpage() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "sortpage.pdf");document = new Document();PdfWriter writer = PdfWriter.getInstance(document, out);writer.setLinearPageMode();document.open();document.add(new Paragraph("1 page"));document.newPage();document.add(new Paragraph("2 page"));document.newPage();document.add(new Paragraph("3 page"));document.newPage();document.add(new Paragraph("4 page"));document.newPage();document.add(new Paragraph("5 page"));int[] order = { 4, 3, 2, 1 };writer.reorderPages(order);document.close();}/*** @Title: addOutline* @Description: TODO 目錄* @throws Exception* @return: void*/public static void addOutline() throws Exception {document = new Document();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "addOutline.pdf"));document.open();// Code 1document.add(new Chunk("Chapter 1").setLocalDestination("1"));document.newPage();document.add(new Chunk("Chapter 2").setLocalDestination("2"));document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));document.newPage();document.add(new Chunk("Chapter 3").setLocalDestination("3"));// Code 2PdfContentByte cb = writer.getDirectContent();PdfOutline root = cb.getRootOutline();// Code 3@SuppressWarnings("unused")PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1"); PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");oline2.setOpen(false);@SuppressWarnings("unused")PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");@SuppressWarnings("unused")PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");@SuppressWarnings("unused")PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");document.close();}/*** @Title: setHeaderFooter* @Description: TODO // Header, Footer* @throws Exception* @return: void*/public static void setHeaderFooter() throws Exception {document = new Document();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_DIR + "setHeaderFooter.pdf"));writer.setPageEvent(new PdfPageEventHelper() {public void onEndPage(PdfWriter writer, Document document) {PdfContentByte cb = writer.getDirectContent();cb.saveState();cb.beginText();BaseFont bf = null;try {bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);} catch (Exception e) {e.printStackTrace();}cb.setFontAndSize(bf, 10);// Headerfloat x = document.top(-20);// 左cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "H-Left", document.left(), x, 0);// 中cb.showTextAligned(PdfContentByte.ALIGN_CENTER, writer.getPageNumber() + " page", (document.right() + document.left()) / 2, x, 0);// 右cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "H-Right", document.right(), x, 0);// Footerfloat y = document.bottom(-20);// 左cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "F-Left", document.left(), y, 0);// 中cb.showTextAligned(PdfContentByte.ALIGN_CENTER, writer.getPageNumber() + " page", (document.right() + document.left()) / 2, y, 0);// 右cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "F-Right", document.right(), y, 0);cb.endText();cb.restoreState();}});document.open();document.add(new Paragraph("1 page"));document.newPage();document.add(new Paragraph("2 page"));document.newPage();document.add(new Paragraph("3 page"));document.newPage();document.add(new Paragraph("4 page"));document.close();}/*** @Title: addColumnText* @Description: TODO 文字左右文字* @throws Exception* @return: void*/public static void addColumnText() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "addColumnText.pdf");Document document = new Document();PdfWriter writer = PdfWriter.getInstance(document, out);document.open();PdfContentByte canvas = writer.getDirectContent();Phrase phrase1 = new Phrase("This is a test!left");Phrase phrase2 = new Phrase("This is a test!right");Phrase phrase3 = new Phrase("This is a test!center");ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase1, 10, 500, 0);ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase2, 10, 536, 0);ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, phrase3, 10, 572, 0);document.close();}/*** @Title: setSlideshow* @Description: TODO slideshow* @throws Exception* @return: void*/public static void setSlideshow() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "setSlideshow.pdf");document = new Document();PdfWriter writer = PdfWriter.getInstance(document, out);writer.setPdfVersion(PdfWriter.VERSION_1_5);writer.setViewerPreferences(PdfWriter.PageModeFullScreen);// 全屏writer.setPageEvent(new PdfPageEventHelper() {public void onStartPage(PdfWriter writer, Document document) {writer.setTransition(new PdfTransition(PdfTransition.DISSOLVE, 3));writer.setDuration(5);// 間隔時(shí)間}});document.open();document.add(new Paragraph("1 page"));document.newPage();document.add(new Paragraph("2 page"));document.newPage();document.add(new Paragraph("3 page"));document.newPage();document.add(new Paragraph("4 page"));document.newPage();document.add(new Paragraph("5 page"));document.close();}/*** @Title: zipPDF* @Description: TODO 壓縮PDF到Zip* @throws Exception* @return: void*/public static void zipPDF() throws Exception {ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(FILE_DIR + "zipPDF.zip"));for (int i = 1; i <= 3; i++) {ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");zip.putNextEntry(entry);document = new Document();PdfWriter writer = PdfWriter.getInstance(document, zip);writer.setCloseStream(false);document.open();document.add(new Paragraph("Hello " + i));document.close();zip.closeEntry();}zip.close();}/*** @Title: setAnnotation* @Description: TODO Annotation* @throws Exception* @return: void*/public static void setAnnotation() throws Exception {FileOutputStream out = new FileOutputStream(FILE_DIR + "setAnnotation.pdf");Document doc = new Document();PdfWriter writer = PdfWriter.getInstance(doc, out);writer.setLinearPageMode();doc.open();doc.add(new Paragraph("1 page"));doc.add(new Annotation("Title", "This is a annotation!"));doc.newPage();doc.add(new Paragraph("2 page"));Chunk chunk = new Chunk("\u00a0");chunk.setAnnotation(PdfAnnotation.createText(writer, null, "Title", "This is a another annotation!", false, "Comment"));doc.add(chunk);// 添加附件doc.newPage();doc.add(new Paragraph("3 page"));Chunk chunk2 = new Chunk("\u00a0\u00a0");PdfAnnotation annotation = PdfAnnotation.createFileAttachment(writer, null, "Title", null, "resource/test2.jpg", "img.jpg");annotation.put(PdfName.NAME, new PdfString("Paperclip"));chunk2.setAnnotation(annotation);doc.add(chunk2);doc.close();}}?
private boolean rotatePdf(String src, String dest) throws IOException, DocumentException {try {PdfReader reader = new PdfReader(src);int n = reader.getNumberOfPages();PdfDictionary page;Rectangle rectangle ;boolean flag = false;for (int p = 1; p <= n; p++) {page = reader.getPageN(p);PdfNumber rotate = page.getAsNumber(PdfName.ROTATE);int rotateInt = 0;if(null != rotate){rotateInt = rotate.intValue();}rectangle = reader.getPageSizeWithRotation(p);if (rectangle.getWidth() > rectangle.getHeight()) {page.put(PdfName.ROTATE, new PdfNumber(rotateInt+90));flag = true;}}if(flag){PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));stamper.close();new File(src).renameTo(new File(src+".bak.pdf"));new File(dest).renameTo(new File(src));}reader.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();}return false; }?
轉(zhuǎn)載于:https://my.oschina.net/spinachgit/blog/1556060
總結(jié)
以上是生活随笔為你收集整理的使用ITEXT操作PDF文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis中接口和对应的mapper
- 下一篇: 基于JS实现回到页面顶部的五种写法(从实