static public Font GetFont(string familyName, float size, int style) { if (familyName == null) { familyName = ""; } Font font; if (familyName.Trim() == "") { font = new Font(Font.FontFamily.UNDEFINED, size, style); } else { Font.FontFamily familyIndex = Font.GetFamilyIndex(familyName); if (familyIndex != Font.FontFamily.UNDEFINED) { font = new Font(familyIndex, size, style); } else { font = new Font(GetBaseFont(familyName), size, style); } } return(font); }
private void AddTitleRows(float fontSize, Font.FontFamily fontFamily, DateTime date, IList <PdfPCell> cells) { cells.Add(new PdfPCell(new Phrase("Aggregated Sales Report", new Font(fontFamily, fontSize, Font.BOLD))) { Colspan = 5, PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase("Date: " + date.ToShortDateString(), new Font(fontFamily, fontSize))) { Colspan = 5, BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, }); cells.Add(new PdfPCell(new Phrase("Product", new Font(fontFamily, fontSize, Font.BOLD))) { BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase("Quantity", new Font(fontFamily, fontSize, Font.BOLD))) { BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase("UnitPrice", new Font(fontFamily, fontSize, Font.BOLD))) { BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase("Location", new Font(fontFamily, fontSize, Font.BOLD))) { BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase("Sum", new Font(fontFamily, fontSize, Font.BOLD))) { BackgroundColor = new BaseColor(217, 217, 217), PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); }
public void CreatePdfAggregatedSalesReports(string destinationDirectoryPath) { float fontSize = 12f; Font.FontFamily fontFamily = new Font.FontFamily(); fontFamily = Font.FontFamily.COURIER; using (var supermarketContext = new SupermarketContext()) { var distinctDates = supermarketContext.Sales .Select(s => s.Date).Distinct().ToList(); foreach (var date in distinctDates) { using (var currentAggregateSalesReport = new Document()) { PdfWriter.GetInstance(currentAggregateSalesReport, new FileStream( string.Format("{0}/Aggregated-Sales-Report-{1}.pdf", destinationDirectoryPath, date.ToShortDateString()), FileMode.Create)); currentAggregateSalesReport.Open(); PdfPTable table = new PdfPTable(5); IList <PdfPCell> cells = new List <PdfPCell>(); AddTitleRows(fontSize, fontFamily, date, cells); var sales = supermarketContext.Sales.Include("Product") .Include("Supermarket").Where(s => s.Date == date).ToList(); var measures = supermarketContext.Measures; foreach (var sale in sales) { string productName = supermarketContext.Products .Find(sale.ProductID).Name; string location = supermarketContext.Supermarkets .Find(sale.SupermarketID).Name; string measure = measures.Find(sale.Product.MeasureID).MeasureName; AddSaleReportRow(fontSize, fontFamily, cells, sale, measure, productName, location); } foreach (var cell in cells) { table.AddCell(cell); } currentAggregateSalesReport.Add(table); } } } }
private void AddSaleReportRow(float fontSize, Font.FontFamily fontFamily, IList <PdfPCell> cells, Models.Sale sale, string measure, string productName, string location) { cells.Add(new PdfPCell(new Phrase(productName, new Font(fontFamily, fontSize, Font.BOLD))) { PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase(string.Format("{0} {1}", sale.Quantity, measure), new Font(fontFamily, fontSize, Font.BOLD))) { PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase(sale.UnitPrice.ToString(), new Font(fontFamily, fontSize, Font.BOLD))) { PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase(location, new Font(fontFamily, fontSize, Font.BOLD))) { PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); cells.Add(new PdfPCell(new Phrase(sale.Sum.ToString(), new Font(fontFamily, fontSize, Font.BOLD))) { PaddingBottom = 10, PaddingTop = 10, HorizontalAlignment = 1 }); }
public void CreatePdfAggregatedSalesReports(string destinationDirectoryPath) { float fontSize = 12f; Font.FontFamily fontFamily = new Font.FontFamily(); fontFamily = Font.FontFamily.COURIER; using (var supermarketContext = new SupermarketContext()) { var distinctDates = supermarketContext.Sales .Select(s => s.Date).Distinct().ToList(); foreach (var date in distinctDates) { using (var currentAggregateSalesReport = new Document()) { PdfWriter.GetInstance(currentAggregateSalesReport, new FileStream( string.Format("{0}/Aggregated-Sales-Report-{1}.pdf", destinationDirectoryPath, date.ToShortDateString()), FileMode.Create)); currentAggregateSalesReport.Open(); PdfPTable table = new PdfPTable(5); IList<PdfPCell> cells = new List<PdfPCell>(); AddTitleRows(fontSize, fontFamily, date, cells); var sales = supermarketContext.Sales.Include("Product") .Include("Supermarket").Where(s => s.Date == date).ToList(); var measures = supermarketContext.Measures; foreach (var sale in sales) { string productName = supermarketContext.Products .Find(sale.ProductID).Name; string location = supermarketContext.Supermarkets .Find(sale.SupermarketID).Name; string measure = measures.Find(sale.Product.MeasureID).MeasureName; AddSaleReportRow(fontSize, fontFamily, cells, sale, measure, productName, location); } foreach (var cell in cells) { table.AddCell(cell); } currentAggregateSalesReport.Add(table); } } } }
private void CriarDocCabecalhoRodape() { Document document = new Document(PageSize.A4, 40f, 40f, 20f, 40f); string docname = $"{DateTime.Now.Second}teste.pdf"; try { // cria o arquivo pdf PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(docname, FileMode.Create)); // cria um objeto do tipo FontFamily, que contem as propriedades de uma fonte Font.FontFamily familha = new Font.FontFamily(); // atribui a familia da fonte, no caso Courier familha = iTextSharp.text.Font.FontFamily.HELVETICA; // cria uma fonte atribuindo a familha, o tamanho da fonte e o estilo (normal, negrito...) Font fonte = new Font(familha, 8, (int)System.Drawing.FontStyle.Bold); // cria uma instancia da classe eventos, é uma classe que mostrarei posteriormente // esta clase trata a criação do cabeçalho e rodapé da página //ADICIONAR O LOGOTIPO iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("Logo SBDE.jpg"); ConfigureReportItextSharp configuredocument = new ConfigureReportItextSharp(fonte, img); // seta o atributo de eventos da classe com a variavel de eventos criada antes writer.PageEvent = configuredocument; // altera a fonte para normal, a negrito era apenas para o cabeçalho e rodapé da página fonte = new Font(familha, 8, (int)System.Drawing.FontStyle.Regular); // abre o documento para começar a escrever o pdf document.Open(); //iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle( new System.util.RectangleJ(document.PageSize.Width - 200f,document.PageSize.Height -140f,140f,90f)); //rect.BorderColor = BaseColor.LIGHT_GRAY; //rect.BackgroundColor = BaseColor.GREEN; //rect.BorderWidth = 2f; //document.Add(rect); // aqui faz um for para simular diversas linhas de um relatorio for (int i = 0; i < 100; i++) { // adiciona um novo paragrafo com o texto da respectiva linha. document.Add(new Paragraph("Teste linha", fonte)); } } catch (Exception de) { MessageBox.Show(de.Message); } // fecha o documento document.Close(); // manda abrir o pdf Process.Start(docname); }