1
我需要在我正在开发的系统中生成一些报告...如何使用Crystal Reports与C#/ ASP.NET生成pdf服务器端?
现在我有一个使用itext sharp的示例...但是,由于它的简单性,我无法创建好看的报告。 ..
所以有人问我切换到Crystal Reports,但我不知道在哪里,如何开始......
从我已经看到了,似乎用户可以创建自己的报告教程使用CR工具,但我想要的是,报告生成自动采取一些数据,用户提供...
这里是我与iText的尖锐
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);
}
}
}
}
所以就像我创建服务器端PDF的像前面的例子中,我想这样做,但用水晶报表做了一个样本...
有没有一种方法可以做类似的事情?任何教程? 哦,顺便说一句...我如何在PDF上插入图片?
感谢