|
|
|
//左、右、上、下页边距Document doc =new Document(PageSize.A4, 50, 50, 10, 10); //A4横向 //Document doc = new Document(PageSize.A4.rotate());PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("d:/test.pdf"));//添加footer需要在document.open之前//页眉HeaderFooter header = new HeaderFooter(new Phrase("This is a header without a page number"), false);header.setAlignment(Element.ALIGN_CENTER);header.setBorder(Rectangle.BOTTOM);//下边框doc.setHeader(header);//页脚HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);footer.setAlignment(Element.ALIGN_RIGHT);footer.setBorder(Rectangle.NO_BORDER);//不要边框doc.setFooter(footer);//标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期doc.addTitle("标题");doc.addSubject("主题");doc.addKeywords("关键字");doc.addAuthor("作者");doc.addCreator("");doc.addProducer();doc.addCreationDate();doc.addHeader("html_header", "内容");//其中方法addHeader对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息doc.open();PdfContentByte cb = writer.getDirectContent();// 标题字体BaseFont bfTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);com.lowagie.text.Font titleFont = new com.lowagie.text.Font(bfTitle, 18, com.lowagie.text.Font.NORMAL);// 内容字体BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);com.lowagie.text.Font font = new com.lowagie.text.Font(bfComic, 9, com.lowagie.text.Font.NORMAL);Paragraph titleP = new Paragraph("儿童信息 Child Information",titleFont);titleP.setAlignment(Paragraph.ALIGN_CENTER);doc.add(titleP);//生成条形码BarcodeEAN codeEAN = new BarcodeEAN();codeEAN.setCodeType(BarcodeEAN.EAN13);codeEAN.setCode("9780201615883");codeEAN.setAltText("9780201615883");Image imageEAN = codeEAN.createImageWithBarcode(cb, Color.black, Color.blue);doc.add(new Phrase(new Chunk(imageEAN, 10, 10)));Barcode128 code = new Barcode128();code.setCode("9780201615883");Image code128 =code.createImageWithBarcode(cb, Color.black, Color.blue);doc.add(new Phrase(new Chunk(code128, 5, -25)));// 生成4列的表格PdfPTable table = new PdfPTable(4);table.setSpacingBefore(10f);//防止与上面文字重叠table.setWidthPercentage(100);table.addCell(new Paragraph("Children-id", font));PdfPCell cell = new PdfPCell(new Paragraph("09140800002", font));cell.setColspan(3);table.addCell(cell);// 添加第二行table.addCell(new Paragraph("Name(CN)", font));table.addCell(new Paragraph("党宁生", font));table.addCell(new Paragraph("Name(EN)", font));table.addCell(new Paragraph("DANG NING SHENG", font));// 添加第三行table.addCell(new Paragraph("Sex(CN)", font));table.addCell(new Paragraph("男", font));table.addCell(new Paragraph("Sex(EN)", font));table.addCell(new Paragraph("MALE", font));// 添加第四行table.addCell(new Paragraph("Note", font));cell = new PdfPCell(new Paragraph("儿童资料", font));cell.setColspan(3);table.addCell(cell);// 添加第五行table.addCell(new Paragraph("Pictures", font));Image photo = Image.getInstance("f:/eclipse09.gif");photo.setAlignment(Image.UNDERLYING);photo.scaleAbsolute(194,202);photo.scalePercent(30f);cell = new PdfPCell(photo);//cell.addElement(new Paragraph("文字", font));cell.setColspan(3);table.addCell(cell);// 添加第六行table.addCell(new Paragraph("Barcode", font));BarcodeDatamatrix bar = new BarcodeDatamatrix(); bar.setOptions(BarcodeDatamatrix.DM_AUTO); bar.generate("HEnSh0701003-2V1");cell = new PdfPCell(bar.createImage());cell.setColspan(3);table.addCell(cell);for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {for (PdfPCell cells : row.getCells()) {if (cells != null) {cells.setPadding(10.0f);}}}doc.add(table);//内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块Chunk chunk = new Chunk("Hello World", FontFactory.getFont(FontFactory.COURIER, 20, com.lowagie.text.Font.ITALIC, new Color(255, 0, 0)));doc.add(chunk);//一个段落有一个且仅有一个间距,如果你添加了一个不同字体的短句或块,原来的间距仍然有效,你可以通过SetLeading来改变间距,但是段落中所有内容将使用新的中的间距Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12)));p1.add("you can add strings, "); p1.add(new Chunk("you can add chunks ")); p1.add(new Phrase("or you can add phrases."));doc.add(p1);//如果我们使用FontFactory来创建字体,字体风格不会被延续,因为FontFactory使用了另外的技术构建一个字体:Phrase myPhrase = new Phrase("Hello 1bis! ", FontFactory.getFont(FontFactory.TIMES_BOLD, 8, com.lowagie.text.Font.BOLD));myPhrase.add(new Phrase("some other font ", FontFactory.getFont(FontFactory.HELVETICA, 8, com.lowagie.text.Font.ITALIC)));myPhrase.add(new Phrase("This is the end of the sentence.\n", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, com.lowagie.text.Font.ITALIC)));doc.add(myPhrase);//锚点Anchor anchor = new Anchor("website", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.UNDERLINE, new Color(0, 0, 255)));anchor.setReference("http://itextsharp.sourceforge.net");anchor.setName("website");doc.add(anchor);//如果你想添加内部链接,你需要选择该链接不同的名称,就象你相位在HTML中利用名称作为锚点一样。为达到该目的,你需要添加一个“#”。Anchor anchor1 = new Anchor("This is an internal link");anchor1.setName("link1");Anchor anchor2 = new Anchor("Click here to jump to the internal link");anchor2.setName("#link1");doc.add(anchor1);doc.add(anchor2);//列表//通过类List 和ListItem,你可以添加列表到PDF文件中,对于列表你还可以选择是否排序。List list = new List(true, 20);list.add(new ListItem("First line"));list.add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));list.add(new ListItem("Third line"));doc.add(list);//文本注释Annotation an = new Annotation("authors","Maybe it's because I wanted to be an author myself that I wrote iText.");doc.add(an);//章节和区域Paragraph cTitle = new Paragraph("This is chapter 1");Chapter chapter = new Chapter(cTitle, 1);Paragraph sTitle = new Paragraph("This is section 1 in chapter 1");Section section = chapter.addSection(sTitle, 1);doc.add(section);//创建了一个4行4列的表格然后添加一些单元格到随机的位置上Table aTable = new Table(4,4);//aTable.setSpacing(10);aTable.setAutoFillEmptyCells(true);//将AutoFillEmptyCells属性设置为true,这将自动、默认的单元格布局填充空的单元格aTable.addCell("2.2", new Point(2,2));aTable.addCell("3.3", new Point(3,3));aTable.addCell("2.1", new Point(2,1));Cell cell2 = new Cell("1.3");//对齐方式cell2.setHorizontalAlignment(Element.ALIGN_CENTER);cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);cell2.setBorderColor(new Color(255, 0, 0));cell2.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));aTable.addCell(cell2, new Point(1,3));doc.add(aTable);//本地转向Chunk localgoto = new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.NORMAL, new Color(0, 0, 255))).setLocalGoto("test");Chunk destination = new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test");doc.add(localgoto);doc.add(destination);//异地转向Chunk chunk2 = new Chunk("anchor", FontFactory.getFont(FontFactory.HELVETICA, 12)).setAnchor(new URL("http://www.lowagie.com/iText/"));doc.add(chunk2);Chunk chunk3 = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, com.lowagie.text.Font.ITALIC)).setRemoteGoto("test.pdf", 3);doc.add(chunk3);//有几种办法可以缩放图片://public void scaleAbsolute(int newWidth, int newHeight)//public void scalePercent(int percent)//public void scalePercent(int percentX, int percentY)//public void scaleToFit(int fitWidth, int fitHeight)//旋转//可以通过下面的方法旋转图片//public void setRotation(double r)//使用包含图片信息的数组来得到图片的实例//public static Image getInstance(byte[] img)//新的页面doc.newPage();/**细长的浅黄色背景的页面**/Rectangle pageSize = new Rectangle(595, 842);pageSize.setBackgroundColor(new Color(0xFF, 0xFF, 0xDE));doc.setPageSize(pageSize);// 插入图片doc.newPage();Image image1 = Image.getInstance("f:/forecolor.gif");image1.setAlignment(Image.ALIGN_CENTER);image1.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());doc.add(image1);doc.close(); |
|