Se imprime mediante un documento pdf, uno tamano carta y otro tamano ticket. Se debe incluir la libreria itextpdf.5.4.3.jar en libraries del proyecto.
package pktFactu;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import static com.itextpdf.text.Element.ALIGN_RIGHT;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class frmFactu extends javax.swing.JFrame {
DefaultTableModel model=new DefaultTableModel();
public frmFactu() {
initComponents();
cargaCli();
tblPro.setModel(model);
String titu[]={"Producto","Precio_Uni","Cantidad","Importe"};
model.setColumnIdentifiers(titu);
tblPro.getColumnModel().getColumn(0).setPreferredWidth(200);
}
private void cargaCli(){
cmbCli.addItem("[Seleccionar]");
cmbCli.addItem("Miguel Nunez Varillas");
cmbCli.addItem("Gladis Zegarra Cordova");
cmbCli.addItem("Rosa Castro Espinoza");
cmbCli.addItem("Ronaldo Altamirez");
cmbCli.addItem("Julio Ortiz Yason");
cmbCli.addItem("Liliana Vergaray Returto");
cmbCli.addItem("Teofilo Cubillas Nieto");
}
private void cmbCliItemStateChanged(java.awt.event.ItemEvent evt) { String Ruc="";
if (cmbCli.getSelectedIndex()>0){
switch (cmbCli.getSelectedIndex()) {
case 1:Ruc="11235689654";break;
case 2:Ruc="44895623147";break;
case 3:Ruc="33226688964";break;
case 4:Ruc="77553311992";break;
default : Ruc="33366689641";
}
txtRuc.setText(Ruc);
}
}
private void cmbProItemStateChanged(java.awt.event.ItemEvent evt) { String pre="";
if (cmbPro.getSelectedIndex()>0){
switch (cmbPro.getSelectedIndex()) {
case 1:pre="400";break;
case 2:pre="460";break;
case 3:pre="550";break;
case 4:pre="600";break;
case 5:pre="300";break;
case 6:pre="40";break;
case 7:pre="68";break;
default : pre="120";
}
txtPre.setText(pre);
txtCan.setText("1");
}
}
private double redondear(double num){
return Math.rint(num*100)/100;
}
private void calTot(){
double S=0,igv,tot;
for(int i=0;i<tblPro.getRowCount();i++){
S=S+Double.parseDouble(model.getValueAt(i, 3).toString());
}
S=redondear(S);
lblSub.setText(String.valueOf(S));
igv=0.09*S;
igv=redondear(igv);
tot=S*1.09;
tot=redondear(tot);
lblIGV.setText(String.valueOf(igv));
lblTotal.setText(String.valueOf(tot));
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) { String rowData[]=new String[4];
rowData[0]=cmbPro.getSelectedItem().toString();
rowData[1]=txtPre.getText();
rowData[2]=txtCan.getText();
Double imp=Double.parseDouble(rowData[1])*Double.parseDouble(rowData[2]);
imp=redondear(imp);
rowData[3]=imp.toString();
model.addRow(rowData);
calTot();
}
private void eliminar(){
int fil;
if (model.getRowCount()>0){
if (tblPro.getSelectedRow()!=-1){
fil=tblPro.getSelectedRow();
model.removeRow(fil);
calTot();
}else{
JOptionPane.showMessageDialog(null, "Seleccione Una Fila");
}
}else{ JOptionPane.showMessageDialog(null, "No hay Filas en la Tabla");}
}
private void btnEliActionPerformed(java.awt.event.ActionEvent evt) { String cad="";
cad = JOptionPane.showInputDialog("Password Manager");
if (cad.contentEquals("1234")) {
eliminar();
} else {
JOptionPane.showMessageDialog(null, "PassWord Erroneo");
}
}
private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {
imprimir();
}
private void imprimir2ActionPerformed(java.awt.event.ActionEvent evt) {
imp2();
}
private void imp2() {
try{
OutputStream file = new FileOutputStream(new File("Test5.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
Rectangle one = new Rectangle(288,400);
document.setPageSize(one);
document.setMargins(2, 2, 2, 2);
document.open();
Paragraph p=new Paragraph("MinuvaHard10 Market",
FontFactory.getFont("arial",14,Font.ITALIC,BaseColor.BLUE));
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
document.add(new Paragraph("Comprobante de Pago"));
PdfPTable tabla = new PdfPTable(4);
float[] medida = {1.95f, 0.5f,0.5f,0.55f};
tabla.addCell(new Paragraph("hello",FontFactory.getFont("arial",12)));
tabla.setWidths(medida);
int n=model.getRowCount();
int i = 0;
tabla.getDefaultCell().setBorder(0);
while (i<n){
tabla.addCell(new Paragraph(model.getValueAt(i, 0).toString(), FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 1).toString(),FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 2).toString(),FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 3).toString(),FontFactory.getFont("arial",10)));
i++;
}
document.add(tabla);
Paragraph st=new Paragraph("SubTotal "+lblSub.getText(),
FontFactory.getFont("arial",
10,
Font.ITALIC,
BaseColor.BLUE));
st.setAlignment(Element.ALIGN_RIGHT);
document.add(st);
document.close();
file.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Error:"+e.getMessage());
}
try {
File file = new File("Test5.pdf");
Desktop.getDesktop().open(file);
} catch(Exception e) {
e.printStackTrace();
}
}
private void imprimir() {
try{
OutputStream file = new FileOutputStream(new File("Test4.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.setPageSize(one);
document.open();
PdfPTable tabla = new PdfPTable(4);
Paragraph p=new Paragraph("Factura",
FontFactory.getFont("arial",
14,
Font.ITALIC,
BaseColor.BLUE));
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
document.add(new Paragraph(" "));
document.add(new Paragraph("Cliente "+cmbCli.getSelectedItem().toString()));
document.add(new Paragraph("RUC "+txtRuc.getText()));
float[] medidaCeldas = {1.5f, 1.25f, 0.55f, 0.5f};
tabla.addCell(new Paragraph("hello",FontFactory.getFont("arial",12)));
tabla.setWidths(medidaCeldas);
tabla.addCell(new Paragraph("Producto",FontFactory.getFont("arial",12)));
tabla.addCell(new Paragraph("Precio Uni",FontFactory.getFont("arial",12)));
tabla.addCell(new Paragraph("Cantidad",FontFactory.getFont("arial",12)));
tabla.addCell(new Paragraph("Importe",FontFactory.getFont("arial",12)));
int n=model.getRowCount();
int i = 0;
while (i<n){
tabla.addCell(new Paragraph(model.getValueAt(i, 0).toString(), FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 1).toString(),FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 2).toString(),FontFactory.getFont("arial",10)));
tabla.addCell(new Paragraph(model.getValueAt(i, 3).toString(),FontFactory.getFont("arial",10)));
i++;
}
document.add(tabla);
String cad2 = "-----------------------------------";
document.add(new Paragraph(cad2+"SubTotal "+lblSub.getText()));
document.add(new Paragraph("Tax "+lblIGV.getText()));
document.add(new Paragraph("Total "+lblTotal.getText()));
document.close();
file.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Error:"+e.getMessage());
}
try {
File file = new File("Test4.pdf");
Desktop.getDesktop().open(file);
} catch(Exception e) {
e.printStackTrace();
}
}
download aqui
e-mail : minuvasoft10@gmail.com