Reputation: 1108
I need to generate some reports in a system that I am developing...
Right Now I have a sample using itext sharp... however, due to it's simplicity, I can't create good looking reports...
So I was asked to switch to Crystal Reports, however I have no clue where or how to begin...
From the tutorials I have saw, it seems that the user can create his own reports using CR tools, but what I Want is that the report generates automatically taking some data that the user provides...
Here's a sample of what I did with iText sharp
public class GeneracionPDF
{
//I need a list for products and a list for services
List<Producto> listaProductos = new List<Producto>();
List<Servicio> listaServicios = new List<Servicio>();
private Producto objProducto = new Producto();
private Servicio objServicio = new Servicio();
private CarritoVenta objCV = new CarritoVenta();
public void GenerarPdf(List<Articulo> lstArticulos, string strNombreReporte, string strEmpresa, string strSlogan, string strNombreVendedor, string strNombreCliente, string strRuta)
{
Organizar(lstArticulos);
#region Inicio
Document documento = new Document(PageSize.LETTER, 50, 50, 50, 50);
PdfWriter writerPdf = PdfWriter.GetInstance(documento, new FileStream(strRuta + strNombreReporte, FileMode.Create));
documento.Open();
#endregion
#region Titulo
Font font1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18, Font.BOLD, BaseColor.BLUE);
Phrase phrEmpresa = new Phrase(strEmpresa + "\n", font1);
Font font2 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12, Font.ITALIC, BaseColor.DARK_GRAY);
Phrase phrSlogan = new Phrase(strSlogan, font2);
Font font16I = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16, Font.ITALIC, BaseColor.BLACK);
Phrase phrDescrip = new Phrase(("Cliente: " + strNombreCliente), font16I);
PdfContentByte cb = writerPdf.DirectContent;
documento.Add(phrEmpresa);
documento.Add(phrSlogan);
documento.Add(phrDescrip);
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
Font fontHelvetica16red = new Font(bfTimes, 12, Font.ITALIC, BaseColor.RED);
#endregion
#region Seccion Productos
if (listaProductos.Count > 0)
{
documento.Add(new Paragraph("\n Productos \n", fontHelvetica16red));
PdfPTable tableProductos = null;
tableProductos = new PdfPTable(6);
float[] colWidths = { 100, 200, 400, 150, 150, 150 };
tableProductos.SetWidths(colWidths);
Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL);
Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL);
PdfPCell celdaIdProducto = new PdfPCell(new Phrase("ID", LetraTituloTabla));
PdfPCell celdaNombreProducto = new PdfPCell(new Phrase("Nombre", LetraTituloTabla));
PdfPCell celdaDescripcionProducto = new PdfPCell(new Phrase("Descripción", LetraTituloTabla));
PdfPCell celdaPrecioProducto = new PdfPCell(new Phrase("Precio", LetraTituloTabla));
PdfPCell celdaCantidadProducto = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla));
PdfPCell celdaTotalProducto = new PdfPCell(new Phrase("Total", LetraTituloTabla));
celdaIdProducto.HorizontalAlignment = 1;
celdaNombreProducto.HorizontalAlignment = 1;
celdaDescripcionProducto.HorizontalAlignment = 1;
celdaPrecioProducto.HorizontalAlignment = 1;
celdaCantidadProducto.HorizontalAlignment = 1;
celdaTotalProducto.HorizontalAlignment = 1;
tableProductos.AddCell(celdaIdProducto);
tableProductos.AddCell(celdaNombreProducto);
tableProductos.AddCell(celdaDescripcionProducto);
tableProductos.AddCell(celdaPrecioProducto);
tableProductos.AddCell(celdaCantidadProducto);
tableProductos.AddCell(celdaTotalProducto);
foreach (Producto prod in listaProductos)
{
celdaIdProducto = new PdfPCell(new Phrase(prod.IdProducto.ToString(), LetraCeldaTabla));
celdaNombreProducto = new PdfPCell(new Phrase(prod.Nombre, LetraCeldaTabla));
celdaDescripcionProducto = new PdfPCell(new Phrase(prod.Descripcion, LetraCeldaTabla));
celdaPrecioProducto = new PdfPCell(new Phrase("$ " + prod.PrecioVenta, LetraCeldaTabla));
celdaCantidadProducto = new PdfPCell(new Phrase(prod.Cantidad.ToString(), LetraCeldaTabla));
celdaTotalProducto = new PdfPCell(new Phrase("$ " + prod.TotalCompra, LetraCeldaTabla));
celdaIdProducto.HorizontalAlignment = 1;
celdaNombreProducto.HorizontalAlignment = 1;
celdaDescripcionProducto.HorizontalAlignment = 1;
celdaPrecioProducto.HorizontalAlignment = 1;
celdaCantidadProducto.HorizontalAlignment = 1;
celdaTotalProducto.HorizontalAlignment = 1;
tableProductos.AddCell(celdaIdProducto);
tableProductos.AddCell(celdaNombreProducto);
tableProductos.AddCell(celdaDescripcionProducto);
tableProductos.AddCell(celdaPrecioProducto);
tableProductos.AddCell(celdaCantidadProducto);
tableProductos.AddCell(celdaTotalProducto);
}
documento.Add(tableProductos);
}
#endregion
#region Seccion Servicios
if (listaServicios.Count > 0)
{
documento.Add(new Paragraph("\n Servicios \n", fontHelvetica16red));
PdfPTable tableServicios = null;
tableServicios = new PdfPTable(6);
float[] colWidths = { 100, 200, 400, 150, 150, 150 };
tableServicios.SetWidths(colWidths);
Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL);
Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL);
PdfPCell celdaIdServicio = new PdfPCell(new Phrase("ID", LetraTituloTabla));
PdfPCell celdaNombreServicio = new PdfPCell(new Phrase("Nombre", LetraTituloTabla));
PdfPCell celdaDescripcionServicio = new PdfPCell(new Phrase("Descripción", LetraTituloTabla));
PdfPCell celdaPrecioServicio = new PdfPCell(new Phrase("Precio", LetraTituloTabla));
PdfPCell celdaCantidadServicio = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla));
PdfPCell celdaTotalServicio = new PdfPCell(new Phrase("Total", LetraTituloTabla));
celdaIdServicio.HorizontalAlignment = 1;
celdaNombreServicio.HorizontalAlignment = 1;
celdaDescripcionServicio.HorizontalAlignment = 1;
celdaPrecioServicio.HorizontalAlignment = 1;
celdaCantidadServicio.HorizontalAlignment = 1;
celdaTotalServicio.HorizontalAlignment = 1;
tableServicios.AddCell(celdaIdServicio);
tableServicios.AddCell(celdaNombreServicio);
tableServicios.AddCell(celdaDescripcionServicio);
tableServicios.AddCell(celdaPrecioServicio);
tableServicios.AddCell(celdaCantidadServicio);
tableServicios.AddCell(celdaTotalServicio);
foreach (Servicio ser in listaServicios)
{
celdaIdServicio = new PdfPCell(new Phrase(ser.IdServicio.ToString(), LetraCeldaTabla));
celdaNombreServicio = new PdfPCell(new Phrase(ser.Nombre, LetraCeldaTabla));
celdaDescripcionServicio = new PdfPCell(new Phrase(ser.Descripcion, LetraCeldaTabla));
celdaPrecioServicio = new PdfPCell(new Phrase("$ " + ser.Precio, LetraCeldaTabla));
celdaCantidadServicio = new PdfPCell(new Phrase(ser.Cantidad.ToString(), LetraCeldaTabla));
celdaTotalServicio = new PdfPCell(new Phrase("$ " + ser.Total, LetraCeldaTabla));
celdaIdServicio.HorizontalAlignment = 1;
celdaNombreServicio.HorizontalAlignment = 1;
celdaDescripcionServicio.HorizontalAlignment = 1;
celdaPrecioServicio.HorizontalAlignment = 1;
celdaCantidadServicio.HorizontalAlignment = 1;
celdaTotalServicio.HorizontalAlignment = 1;
tableServicios.AddCell(celdaIdServicio);
tableServicios.AddCell(celdaNombreServicio);
tableServicios.AddCell(celdaDescripcionServicio);
tableServicios.AddCell(celdaPrecioServicio);
tableServicios.AddCell(celdaCantidadServicio);
tableServicios.AddCell(celdaTotalServicio);
}
documento.Add(tableServicios);
}
#endregion
#region Finalizacion
//This method returns a decimal value, so no worries here
decimal decSubtotal = objCV.Subtotal(lstArticulos);
documento.Add(new Paragraph("\n Subtotal: $" + decSubtotal, fontHelvetica16red));
documento.Add(new Paragraph("IVA: $" + (decSubtotal * 0.11m), fontHelvetica16red));
documento.Add(new Paragraph("Total: $" + (decSubtotal * 1.11m), fontHelvetica16red));
documento.Add(new Paragraph("\nNota: Si no requiere factura, omita el IVA", fontHelvetica16red));
documento.Close();
#endregion
}
//Method to separate products and services in different lists, since lstArticulos contains both products and services
private void Organizar(List<Articulo> lstArticulos)
{
listaProductos.Clear();
listaServicios.Clear();
foreach (Articulo art in lstArticulos)
{
if (art.Tipo == "Producto")
{
objProducto = new Producto(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad);
listaProductos.Add(objProducto);
}
else if (art.Tipo == "Servicio")
{
objServicio = new Servicio(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad);
listaServicios.Add(objServicio);
}
}
}
}
So just like I create pdf's on server side like in the previous example, I would like to do the same but with Crystal Reports...
Is there a way I can do something similar?? any tutorials?? oh and BTW... how can I insert images on the pdf?
Thanks
Upvotes: 1
Views: 1770
Reputation: 3929
here a tutorial in order to create dynamically your crystal report column
http://csharp.net-informations.com/crystal-reports/csharp-dynamic-crystal-reports.htm
But it will be better:
Upvotes: 1