Download: iText 4.2.1
1. Document Open
1 2 3 | Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); |
2. Document Close
1 | document.close(); |
3. Add Page
1 | document.newPage(); |
4. Add Text
1 | document.add(new Paragraph("Hello World")); |
5. Add Image
1 2 3 4 5 | Image jpg = Image.getInstance("a.jpg"); jpg.setBorder(Image.BOX); jpg.setBorderColor( new Color(0xFF, 0x00, 0xff)); jpg.setBorderWidth(5); document.add(jpg); |
1 2 3 4 5 6 7 8 | Image png = Image.getInstance("a.png"); png.setBorder(Image.BOX); png.setBorderColor( new Color(0xFF, 0x00, 0xff)); png.setBorderWidth(5); //Rectangle r = document.getPageSize(); //png.scaleAbsoluteWidth(r.getWidth()); png.scalePercent(50); document.add(png); |
6. Add Table
1 2 3 4 5 6 7 8 9 | PdfPTable table = new PdfPTable(10); for (int k = 1; k <= 100; ++k) { table.addCell("number " + k); } table.setTotalWidth(800); table.writeSelectedRows(0, 5, 0, -1, 50, 650, writer.getDirectContent()); document.newPage(); table.writeSelectedRows(5, -1, 0, -1, 50, 650, writer.getDirectContent()); document.newPage(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | PdfPTable datatable = new PdfPTable(10); int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 }; datatable.setWidths(headerwidths); datatable.setWidthPercentage(100); datatable.getDefaultCell().setPadding(5); datatable.setHeaderRows(2); datatable.getDefaultCell().setBorderWidth(1); for (int i = 1; i < 30; i++) { datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell h = new PdfPCell(new Paragraph("myUserId")); h.setGrayFill(0.7f);; h.setImage(png); datatable.addCell(h);; //datatable.addCell("myUserId"); datatable.addCell("long long name"); datatable.addCell("No Name Company"); datatable.addCell("D" + i); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (int j = 0; j < 6; j++) datatable.addCell(Math.random() > .5 ? "Yes" : "No"); } datatable.setSplitLate(false); document.add(datatable); |
7. Add Outline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | PdfOutline rootOutline = writer.getRootOutline(); writer.setPageEmpty(true); document.newPage(); writer.setPageEmpty(false); document.newPage(); PdfOutline helloBookmark = new PdfOutline(rootOutline, new PdfDestination( PdfDestination.FITH, writer.getVerticalPosition(true)), "Hello World", true); document.add(new Paragraph("Hello World")); document.newPage(); PdfOutline link = new PdfOutline(helloBookmark, new PdfDestination( PdfDestination.FITH, writer.getVerticalPosition(true)), "Next World"); document.add(new Paragraph("Next World")); |
8. Add Chapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); Paragraph hello = new Paragraph("hello, "); Chapter universe = new Chapter("A", 1); Section section; section = universe.addSection("B"); section.add(hello); document.add(universe); Chapter people = new Chapter("C", 2); section = people.addSection("D"); section.add(hello); document.add(people); document.setPageSize(PageSize.A4.rotate()); Chapter animals = new Chapter("E", 4); section = animals.addSection("F"); section.add(hello); document.add(animals); document.close(); |
9. Add Chapter with Auto Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.open(); Phrase text = new Phrase("test. "); ChapterAutoNumber chapter1 = new ChapterAutoNumber("This is a sample sentence:"); chapter1.setBookmarkTitle("The fox"); chapter1.setBookmarkOpen(false); Section section1 = chapter1.addSection("Quick"); section1.add(text); document.add(chapter1); ChapterAutoNumber chapter2 = new ChapterAutoNumber("Jumps"); Section section = chapter2.addSection("Over"); section.add(text); document.add(chapter2); document.close(); |
댓글 없음:
댓글 쓰기