Java tutorial
package com.solidmaps.webapp.report; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.util.Calendar; import java.util.List; 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.Font.FontFamily; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.solidmaps.webapp.entity.LicensePCEntity; import com.solidmaps.webapp.entity.LicensePCProductEntity; import com.solidmaps.webapp.utils.DateUtils; /** * Formulrio: PC02 - Incluso de Produtos * * @author brunorocca * */ public class LicenseCivilIncludeProductPDF { private String filePath; public LicenseCivilIncludeProductPDF(String filePath) { this.filePath = filePath; } private final String WINDOWS_LINE_SEPARATOR = "\r\n"; private final Font FONT_PARAGRAPH = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL); private final Font FONT_HEADER = new Font(FontFamily.TIMES_ROMAN, 14, Font.BOLD); public String generate(LicensePCEntity license, String type, List<LicensePCProductEntity> listProducts) { Document doc = new Document(); PdfWriter docWriter = null; String fileDocPath = ""; try { fileDocPath = this.createDocument(doc, docWriter, license, type); this.createParagraph(doc, license, type, listProducts); } catch (DocumentException dex) { dex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (doc != null) { // close the document doc.close(); } if (docWriter != null) { // close the writer docWriter.close(); } } return fileDocPath; } private String createDocument(Document doc, PdfWriter docWriter, LicensePCEntity license, String type) throws FileNotFoundException, DocumentException { String fileName = "Requerimento de " + type + " Licensa: " + license.getCompany().getCnpj() + ".pdf"; docWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath + fileName)); // document header attributes doc.addAuthor("EnforceMaps"); doc.addCreationDate(); doc.addProducer(); doc.addCreator("EnforceMaps"); doc.addTitle("Requerimento de " + type + " Licensa: " + license.getCompany().getCnpj()); doc.setPageSize(PageSize.A4); // open document doc.open(); return fileName; } private void createParagraph(Document doc, LicensePCEntity license, String type, List<LicensePCProductEntity> listProducts) throws DocumentException { StringBuilder sbParagraph = new StringBuilder().append("A sociedade empresria ") .append(license.getCompany().getName()).append(", estabelecida em ") .append(license.getCompany().getStreet()).append(", com ramo de ") .append(license.getCompany().getCodCnae()).append(", telefone ") .append(license.getCompany().getPhoneNumber()).append(", fax ") .append(license.getCompany().getFaxNumber()).append(", e-mail: ") .append(license.getCompany().getUserResponsable().getEmail()).append(" e CEP ") .append(license.getCompany().getCep()).append(", com CNPJ N") .append(license.getCompany().getCnpjFormatted()).append("e inscrio Estadual N") .append(license.getCompany().getNumInscription()).append(", atravs de seu ") .append(license.getCompany().getUserResponsable().getOffice()).append(", Sr. ") .append(license.getCompany().getUserResponsable().getName()) .append(", vem respeitosamente requerer de V. Ex. a ALTERAO de seu:") .append(WINDOWS_LINE_SEPARATOR) .append("Certificado de Vistoria para Produtos Controlados, fixando-se sua capacidade nos seguintes limites mximos ") .append("de estoque: (Conforme relao em Anexo)").append(WINDOWS_LINE_SEPARATOR) .append("De acordo com o Decreto No 6.911 de 10 de Janeiro de 1935."); StringBuilder sbTerms = new StringBuilder().append("Nestes termos").append(WINDOWS_LINE_SEPARATOR) .append(" Pede deferimento."); StringBuilder sbDate = new StringBuilder().append(license.getCompany().getCity()).append("/") .append(license.getCompany().getState()).append(", ") .append(DateUtils.format(Calendar.getInstance())); StringBuilder sbSignature = new StringBuilder().append("____________________________________________") .append(WINDOWS_LINE_SEPARATOR).append("Nome: ") .append(license.getCompany().getUserResponsable().getName()).append(WINDOWS_LINE_SEPARATOR) .append("Cargo: ").append(license.getCompany().getUserResponsable().getOffice()) .append(WINDOWS_LINE_SEPARATOR).append("RG: ") .append(license.getCompany().getUserResponsable().getRg()).append(WINDOWS_LINE_SEPARATOR) .append("CPF: ").append(license.getCompany().getUserResponsable().getCpf()); // Header Paragraph paragraphHeader = new Paragraph("DEPARTAMENTO DE IDENTIFICAO E REGISTROS DIVERSOS DIRD " + "DIVISO DE PRODUTOS CONTROLADOS DPC"); paragraphHeader.setFont(FONT_HEADER); paragraphHeader.setAlignment(Element.ALIGN_CENTER); // Texto da Empresa Paragraph paragraphCompany = new Paragraph(sbParagraph.toString()); paragraphCompany.setFont(FONT_PARAGRAPH); Paragraph paragraphTerms = new Paragraph(sbTerms.toString()); paragraphTerms.setFont(FONT_PARAGRAPH); paragraphTerms.setAlignment(Element.ALIGN_CENTER); Paragraph paragraphDate = new Paragraph(sbDate.toString()); paragraphDate.setFont(FONT_PARAGRAPH); Paragraph paragraphSignature = new Paragraph(sbSignature.toString()); paragraphSignature.setFont(FONT_PARAGRAPH); paragraphSignature.setAlignment(Element.ALIGN_CENTER); doc.add(paragraphHeader); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(paragraphCompany); doc.add(Chunk.NEWLINE); doc.add(paragraphTerms); doc.add(Chunk.NEWLINE); doc.add(paragraphDate); doc.add(Chunk.NEWLINE); doc.add(paragraphSignature); doc.add(Chunk.NEWLINE); this.generateProducts(doc, listProducts); } private void generateProducts(Document doc, List<LicensePCProductEntity> listProducts) throws DocumentException { DecimalFormat dc = new DecimalFormat("########0.00"); // specify column widths float[] columnWidths = { 2f, 2f, 2f }; // create PDF table with the given widths PdfPTable table = new PdfPTable(columnWidths); // set table width a percentage of the page width table.setWidthPercentage(100f); // insert column headings insertCell(table, "Nome da Substncia de Controle", Element.ALIGN_CENTER, FONT_PARAGRAPH); insertCell(table, "Volume mximo de estoque", Element.ALIGN_CENTER, FONT_PARAGRAPH); insertCell(table, "Unidade de medida", Element.ALIGN_CENTER, FONT_PARAGRAPH); table.setHeaderRows(1); for (LicensePCProductEntity product : listProducts) { // Cria a tabela com os Produtos insertCell(table, product.getProduct().getName(), Element.ALIGN_LEFT, FONT_PARAGRAPH); insertCell(table, dc.format(product.getQtdProduct()), Element.ALIGN_LEFT, FONT_PARAGRAPH); insertCell(table, product.getTypeQtdProduct(), Element.ALIGN_LEFT, FONT_PARAGRAPH); } doc.add(Chunk.NEXTPAGE); doc.add(table); } private void insertCell(PdfPTable table, String text, int align, Font font) { this.insertCell(table, text, align, font, true); } private void insertCell(PdfPTable table, String text, int align, Font font, boolean border) { if (text == null) { text = ""; } // create a new cell with the specified Text and Font PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font)); // set the cell alignment cell.setHorizontalAlignment(align); // in case there is no text and you wan to create an empty row if (text.trim().equalsIgnoreCase("")) { cell.setMinimumHeight(10f); } if (!border) { cell.setBorder(Rectangle.NO_BORDER); } // add the call to the table table.addCell(cell); } }