iTEXT常用属性设置
生活随笔
收集整理的這篇文章主要介紹了
iTEXT常用属性设置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iTEXT官方網站:
https://developers.itextpdf.com/examples
常用屬性:
public static void main(String[] args) throws DocumentException, IOException {//創建文件Document document = new Document();// 也可以自定義文件頁面大小// Rectangle pagesize = new Rectangle(216f, 720f);// Document document = new Document(pagesize, 36f, 72f, 108f, 180f);//建立一個書寫器PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));//打開文件document.open();//中文字體,解決中文不能顯示問題BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font blackFont = new Font(bfChinese);blackFont.setColor(BaseColor.BLACK); // 設置字體顏色為黑色Font blackFontB = new Font(bfChinese,8); // 設置字體大小為8document.add(new Paragraph("你好",blackFont)); //添加內容document.addTitle("標題"); //標題document.addAuthor("作者"); //作者document.addSubject("主題"); //主題document.addKeywords("關鍵字"); //關鍵字document.addCreationDate(); //創建時間document.addCreator("應用程序"); //應用程序// 添加圖片Image image1 = Image.getInstance("D:hello.JPG");image1.setAbsolutePosition(100f, 550f); //設置圖片位置的x軸和y軸image1.scaleAbsolute(200, 200); //設置圖片的寬度和高度document.add(image1); //將圖片1添加到pdf文件中//添加有序列表List orderList = new List(List.ORDERED);orderList.add(new ListItem("Item one"));orderList.add(new ListItem("Item two"));orderList.add(new ListItem("Item three"));document.add(orderList);//創建章節Paragraph chapterTitle = new Paragraph("段落標題xxxx", greenFont);Chapter chapter1 = new Chapter(chapterTitle, 1);chapter1.setNumberDepth(0);Paragraph sectionTitle = new Paragraph("部分標題", greenFont);Section section1 = chapter1.addSection(sectionTitle);Paragraph sectionContent = new Paragraph("部分內容", blueFont);section1.add(sectionContent);document.add(chapter1);// 設置段落Paragraph paragraph = new Paragraph("段落", titlefont);paragraph.setAlignment(1); //設置文字居中 0靠左 1,居中 2,靠右paragraph.setIndentationLeft(12); //設置左縮進paragraph.setIndentationRight(12); //設置右縮進paragraph.setFirstLineIndent(24); //設置首行縮進paragraph.setLeading(50f); //行間距paragraph.setSpacingBefore(5f); //設置段落上空白paragraph.setSpacingAfter(10f); //設置段落下空白document.add(paragraph);// 直線 Paragraph p1 = new Paragraph(); p1.add(new Chunk(new LineSeparator()));document.add(p1);// 點線 Paragraph p2 = new Paragraph(); p2.add(new Chunk(new DottedLineSeparator()));document.add(p2);// 超鏈接 Anchor anchor = new Anchor("baidu");anchor.setReference("www.baidu.com");document.add(anchor);// 定位 Anchor gotoP = new Anchor("goto"); gotoP.setReference("#top");document.add(gotoP);// 設置密碼(要先設置密碼,再打開文件document.open();)//用戶密碼String userPassword = "123456";//擁有者密碼String ownerPassword = "hd";writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打開文件document.open();//添加內容document.add(new Paragraph("password !!!!"));// 設置只讀權限(也是先設置權限,再打開文件document.open();)writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);//關閉文檔document.close();//關閉書寫器writer.close(); }表格
public static void main(String[] args) throws DocumentException, IOException {//創建文件Document document = new Document();//建立一個書寫器PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/hello.pdf"));//打開文件document.open();//中文字體,解決中文不能顯示問題BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);Font blackFont = new Font(bfChinese);blackFont.setColor(BaseColor.BLACK);// 創建表格(1代表只有1列)PdfPTable table = new PdfPTable(1);table.setWidthPercentage(100); // 表格寬度比例為100%table.setTotalWidth(500); // 設置表格的寬度table.setTotalWidth(new float[] { 160, 70, 130, 100 }); // 設置每列寬度table.setLockedWidth(true); // 鎖住寬度table.setSpacingBefore(10f); // 設置表格上面空白寬度table.getDefaultCell().setBorder(0); // 設置表格默認為無邊框PdfPCell cell = new PdfPCell(new Phrase("單元格")); // 新建單元格cell.setBorderColor(BaseColor.BLUE); // 設置邊框顏色cell.setBackgroundColor(BaseColor.ORANGE); // 設置背景顏色cell.setRowspan(2); // 設置跨兩行cell.setColspan(2); // 設置占用列數2列cell.setPaddingLeft(10); // 設置距左邊的距離cell.setFixedHeight(20); // 設置高度cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 設置內容水平居中顯示cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 設置垂直居中cell.disableBorderSide(1); //隱藏上邊框cell.disableBorderSide(2); //隱藏下邊框cell.disableBorderSide(3); //隱藏上、下邊框cell.disableBorderSide(4); //隱藏左邊框cell.disableBorderSide(5); //隱藏左、上邊框cell.disableBorderSide(6); //隱藏左、下邊框cell.disableBorderSide(7); //隱藏左、上、下邊框cell.disableBorderSide(8); //隱藏右邊框cell.disableBorderSide(9); //隱藏右、上邊框cell.disableBorderSide(10); //隱藏右、下邊框cell.disableBorderSide(11); //隱藏右、上、下邊框cell.disableBorderSide(12); //隱藏左、右邊框cell.disableBorderSide(13); //隱藏上、左、右邊框cell.disableBorderSide(14); //隱藏下、左、右邊框cell.disableBorderSide(15); //隱藏全部table.addCell(cell); // 將單元格加入表格中// 增加一個條形碼到表格Barcode128 code128 = new Barcode128();code128.setCode("14785236987541");code128.setCodeType(Barcode128.CODE128);// 生成條形碼圖片Image code128Image = code128.createImageWithBarcode(cb, null, null);// 加入到表格PdfPCell cellcode = new PdfPCell(code128Image, true);cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);cellcode.setFixedHeight(30);table.addCell(cellcode);document.add(table);//關閉文檔document.close();//關閉書寫器writer.close(); }其中表格是可以嵌套的,也就是說一個表格可以加入到另一個表格的單元格中。
壓縮PDF文件
import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter;public class PDFZip {public static void main(String[] args)throws DocumentException, IOException {// 壓縮多個PDF文件ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(RESULT));for (int i = 1; i <= 3; i++) {ZipEntry entry = new ZipEntry("test" + i + ".pdf");zip.putNextEntry(entry);Document document = new Document();PdfWriter writer = PdfWriter.getInstance(document, zip);writer.setCloseStream(false);document.open();document.add(new Paragraph("testpdfzip " + i));document.close();zip.closeEntry();}zip.close();} }總結
以上是生活随笔為你收集整理的iTEXT常用属性设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《MATLAB智能算法30个案例》:第5
- 下一篇: 利用哈夫曼编码英文字母表