Java tutorial
package com.solidmaps.webapp.report; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Calendar; 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.pdf.PdfWriter; import com.solidmaps.webapp.entity.LicenseEXEntity; import com.solidmaps.webapp.utils.DateUtils; /** * Formulrio: REQUERIMENTO PARA CONCESSO E REVALIDAO DE CERTIFICADO DE REGISTRO * * @author brunorocca * */ public class RenovationRequireExercitoPDF { private String filePath; public RenovationRequireExercitoPDF(String filePath) { this.filePath = filePath; } private final Font FONT_PARAGRAPH = new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL); private final Font FONT_HEADER = new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL); public void generate(LicenseEXEntity license) { Document doc = new Document(); PdfWriter docWriter = null; try { this.createDocument(doc, docWriter, license); this.createParagraph(doc, license); } 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(); } } } private String createDocument(Document doc, PdfWriter docWriter, LicenseEXEntity license) throws FileNotFoundException, DocumentException { String fileName = "REQUERIMENTO PARA CONCESSO E REVALIDAO DE CERTIFICADO DE REGISTRO " + license.getNumRegister() + ".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 PARA CONCESSO E REVALIDAO DE CERTIFICADO DE REGISTRO " + license.getNumRegister() + ".pdf"); doc.setPageSize(PageSize.A4); // open document doc.open(); return fileName; } private void createParagraph(Document doc, LicenseEXEntity license) throws DocumentException { StringBuilder sbHeader = new StringBuilder().append("Exmo Sr Comandante da ") .append(license.getCompany().getRegion()).append(" Regio Militar"); StringBuilder sbParagraph = new StringBuilder().append(license.getCompany().getName()) .append(", estabelecida em ").append(license.getCompany().getCity()).append("/") .append(license.getCompany().getState()).append(", ").append(license.getCompany().getStreet()) .append(", telefone n ").append(license.getCompany().getUserResponsable().getPhoneDDD()) .append(" ").append(license.getCompany().getUserResponsable().getPhoneNumber()) .append(", representada, neste ato, por seu proprietrio (scio ou diretor, etc.), ") .append(license.getCompany().getUserResponsable().getName()).append(", ") .append(license.getCompany().getUserResponsable().getCountry()).append(", ") .append(license.getCompany().getUserResponsable().getMaritalStatus()).append(", ") .append(license.getCompany().getUserResponsable().getOffice()).append(", ") .append(" (domiciliado ) ").append(license.getCompany().getUserResponsable().getStreet()) .append(", vem, pelo presente, requerer V Exa. (concesso ou revalidao) do Certificado de Registro n ") .append(license.getNumRegister()) .append(", de acordo com o art. 84 do Regulamento para a Fiscalizao de ") .append("Produtos Controlados (R-105), para importar, comerciar (ou manipular, utilizar industrialmente, ou o que for) ") .append("com armas, munies, plvora para caa (ou explosivos, produtos qumicos controlados), durante o trinio ") .append(DateUtils.format(license.getDateExpiration())).append(" - ") .append(DateUtils.format(DateUtils.addYear(license.getDateExpiration(), 3))); // Anexo Paragraph paragraphHeaderAnexo = new Paragraph("ANEXO XVI"); paragraphHeaderAnexo.setFont(FONT_HEADER); paragraphHeaderAnexo.setAlignment(Element.ALIGN_CENTER); // Texto cabealho Paragraph paragraphHeaderText = new Paragraph( "REQUERIMENTO PARA CONCESSO E REVALIDAO DE CERTIFICADO DE REGISTRO"); paragraphHeaderText.setFont(FONT_HEADER); paragraphHeaderText.setAlignment(Element.ALIGN_CENTER); // Texto Comandante Paragraph paragraphComand = new Paragraph(sbHeader.toString()); paragraphComand.setFont(FONT_PARAGRAPH); // Texto impresso Paragraph paragraphPrint = new Paragraph("(Impresso em papel liso com 16 espaos simples)"); paragraphPrint.setFont(FONT_PARAGRAPH); // Texto da Empresa Paragraph paragraphCompany = new Paragraph(sbParagraph.toString()); paragraphCompany.setFont(FONT_PARAGRAPH); // Texto Footer Paragraph paragraphFooter = new Paragraph("Nestes termos, pede deferimento"); paragraphCompany.setFont(FONT_PARAGRAPH); // Texto data Paragraph paragraphDate = new Paragraph(DateUtils.format(Calendar.getInstance())); paragraphDate.setFont(FONT_PARAGRAPH); doc.add(paragraphHeaderAnexo); doc.add(Chunk.NEWLINE); doc.add(paragraphHeaderText); doc.add(Chunk.NEWLINE); doc.add(paragraphComand); doc.add(Chunk.NEWLINE); doc.add(paragraphPrint); doc.add(Chunk.NEWLINE); doc.add(paragraphCompany); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(Chunk.NEWLINE); doc.add(paragraphFooter); doc.add(Chunk.NEWLINE); doc.add(paragraphDate); doc.add(Chunk.NEWLINE); } }