Last active
December 14, 2017 18:23
-
-
Save dineshr93/bb63da0afce1eefe6e92a7f2eac3e31b to your computer and use it in GitHub Desktop.
Helper for creating pdf documents using itextpdf 5.5.9 (iText-4.2.0-com.itextpdf.jar)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Document document = new Document(); //required for pdfWriter instance | |
| document.setMarginMirroring(true); //margins are copies into page | |
| document.setMargins(36, 36, 108, 54); //set margins | |
| Instance Instance = new Instance(); // required for pdfWriter setpageevent instance | |
| //pdfWriter finally writes the document instance contents into the pdf file | |
| PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream("Din/file/pdfname.pdf")); | |
| pdfWriter.setCompressionLevel(0); //define compression | |
| pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_7); //define pdf version | |
| pdfWriter.setPageEvent(Instance); //sets the instance to the current page | |
| //define the footer | |
| HeaderFooter footer = new HeaderFooter(new Phrase("Copyright © Dinesh. All rights reserved.",new Font(Font.HELVETICA,6, Font.NORMAL)), true); | |
| document.setFooter(footer); | |
| //open to define the contents | |
| document.open(); | |
| //space using paragraph instance | |
| document.add(new Paragraph(" ")); | |
| //write in new page | |
| document.newPage(); | |
| PdfPTable pdfPTable = new PdfPTable(1);//1 column or new PdfPTable(6); //6 column | |
| PdfPCell pdfPCell = new PdfPCell(new Phrase("content", HeaderFont)); | |
| pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);or use Element.ALIGN_LEFT //ALIGN_CENTER mandate for all the cell | |
| pdfPCell.setBorder(Rectangle.NO_BORDER);//No border for this cell mandate for all the cell | |
| pdfPTable.addCell(pdfPCell); | |
| //for table start(not to use Rectangle.NO_BORDER) | |
| PdfPCell PdfPCell;//same cell instance for 1 row | |
| PdfPCell = new PdfPCell(new Phrase("Name",Font_desc)); | |
| PdfPCell.setColspan(3); | |
| PdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); //prepare cell and add to table | |
| PdfPCell = new PdfPCell(new Phrase("Dinesh",Font_desc1)); | |
| PdfPCell.setColspan(3); | |
| PdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT); | |
| PdfPTable.addCell(PdfPCell); //prepare cell and add to table | |
| //for table end | |
| //defining table width for each column | |
| Rectangle Rectangle = new Rectangle(275, 770); | |
| PdfPTable samplePdfPTable = new PdfPTable(3); | |
| samplePdfPTable.setWidthPercentage(new float[] { 40,80, 160 },Rectangle); | |
| //space between table using Phrase instance in cell and cell's colspan and fixedheight | |
| PdfPCell = new PdfPCell(new Phrase("")); | |
| PdfPCell.setColspan(7); | |
| PdfPCell.setFixedHeight(25); | |
| PdfPCell.setBorder(PdfPCell.NO_BORDER); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPCell1.setColspan(3); | |
| PdfPTable.addCell(PdfPCell1); | |
| //setting the width to 100% | |
| pdfPTable.setWidthPercentage(100); | |
| //aligning the table | |
| pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT); | |
| //always add cell to the table to the document | |
| document.add(pdfPTable); | |
| //left padding to the pdfPCell | |
| pdfPCell.setPaddingLeft(18); | |
| //font relted | |
| Font font = new Font(); or new Font(Font.HELVETICA, 6, Font.NORMAL); | |
| font.setColor(Color.BLACK); | |
| font.setSize(10); | |
| font.setStyle(com.itextpdf.text.Font.BOLD) | |
| //sample 6 column table //same name of pdfpcell instance and pdfptable | |
| //AFTER SETTING THE WIDTH PERCENTAGE DEFINE THE CELL AND ADD INTO SAME TABLE INSTANCE | |
| try { | |
| PdfPTable.setWidthPercentage(new float[] { 10, 65, 28, 65, 32,70 }, Rectangle); | |
| } catch (DocumentException e) { | |
| e.printStackTrace(); | |
| } | |
| Font Font = new Font(); | |
| Font.setColor(Color.BLACK); | |
| Font.setStyle(com.itextpdf.text.Font.BOLD); | |
| Font.setSize(8); | |
| PdfPCell = new PdfPCell(new Phrase("No", Font)); | |
| PdfPCell.setBackgroundColor(Color.MAGENTA); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPCell = new PdfPCell(new Phrase("Name of OSS Component", Font)); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPCell = new PdfPCell(new Phrase("Component Version", Font)); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| //PdfPCell = new PdfPCell(new Phrase("Download Link", Font)); | |
| //PdfPCell.setGrayFill(0.8f); | |
| //PdfPTable.addCell(PdfPCell); | |
| PdfPCell = new PdfPCell(new Phrase("License Name and Version", Font)); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPCell = new PdfPCell(new Phrase("License Chapter No. ", Font)); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPCell = new PdfPCell(new Phrase("CopyRight ", Font)); | |
| PdfPCell.setGrayFill(0.8f); | |
| PdfPTable.addCell(PdfPCell); | |
| PdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT); | |
| //PARAGRAPH | |
| Paragraph paragraph2 = new Paragraph("",font_license2); | |
| paragraph2.setSpacingBefore(15f); | |
| paragraph2.setSpacingAfter(50f); | |
| //document close finally | |
| document.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment