Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Facturacion; import Cliente.Cliente; import java.util.ArrayList; import Productos.Producto; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.swing.text.Document; /** * * @author User */ public class Factura { ArrayList<Producto> producto = new ArrayList<Producto>(); public Factura() { } public void AgregarProducto(Producto t) { producto.add(t); } public void Generar(String numero, int cantidad, int total, Cliente c) throws FileNotFoundException, DocumentException { com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4, 80, 80, 50, 50); FileOutputStream salida = new FileOutputStream("FACTURA" + numero + ".pdf"); PdfWriter writer = PdfWriter.getInstance(document, salida); writer.setInitialLeading(0); PdfPTable table = new PdfPTable(6); table.addCell("CANTIDAD"); table.addCell("CODIGO"); table.addCell("NOMBRE"); table.addCell("DESCRIPCION"); table.addCell("PRECIO"); table.addCell("TOTAL"); for (int i = 0; i < producto.size(); i++) { table.addCell(String.valueOf(cantidad)); table.addCell(String.valueOf(producto.get(i).getCodigo())); table.addCell(producto.get(i).getNombreP()); table.addCell(producto.get(i).getDescripcion()); table.addCell(String.valueOf(producto.get(i).getPrecio())); table.addCell(String.valueOf(total)); } SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy Date now = new Date(); String strDate = sdfDate.format(now); document.open(); Paragraph paragraph = new Paragraph(); Paragraph paragraph1 = new Paragraph(); paragraph.add("DIMASPORT CIA. LTDA.\n\nFACTURA N" + numero); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph1.add("\n\nNOMBRE: " + c.getNombre() + " " + c.getApellido() + "\nCEDULA: " + c.getCedula() + "\nDIRECCION: " + c.getDireccion() + "\nCORREO: " + c.getCorreo() + "\nTELEFONO: " + c.getTelefono() + "\nFECHA: " + strDate + "\n\n"); paragraph.setAlignment(Paragraph.ALIGN_LEFT); document.add(paragraph); document.add(paragraph1); document.add(table); document.close(); } }