-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
199 lines (171 loc) · 8.29 KB
/
Copy pathProgram.cs
File metadata and controls
199 lines (171 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace GeradorRelatorioPDF
{
class Program
{
static List<Pessoa> pessoas = new List<Pessoa>();
static BaseFont fonteBase = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
static void Main(string[] args)
{
DesserializarPessoas();
//foreach (var p in pessoas)
//{
// Console.WriteLine($"{p.IdPessoa} - {p.Nome} {p.Sobrenome}");
//}
GerarRelatorioPDF(186);
}
static void DesserializarPessoas()
{
if (File.Exists("pessoas.json"))
{
using (var sr = new StreamReader("pessoas.json"))
{
var dados = sr.ReadToEnd();
pessoas = JsonSerializer.Deserialize(dados, typeof(List<Pessoa>)) as List<Pessoa>;
}
}
}
static void GerarRelatorioPDF(int qtdePessoas)
{
var pessoasSelecionadas = pessoas.Take(qtdePessoas).ToList();
if(pessoasSelecionadas.Count > 0)
{
// Calculo da quantidade total de paginas (consigerando valores fixos de row)
int totalPaginas = 1;
int totalLinhas = pessoasSelecionadas.Count;
if (totalLinhas > 29)
{
totalPaginas += (int)Math.Ceiling((totalLinhas - 29) / 30F);
}
// Configuração do documento PDF
var pxPorMm = 72 / 32.2F;
var pdf = new Document(PageSize.A4, 15 * pxPorMm, 15 * pxPorMm, 15 * pxPorMm, 20 * pxPorMm);
var nomeArquivo = $"pessoas.{DateTime.Now.ToString("yyyy.MM.dd.hh.mm.ss")}.pdf";
var arquivo = new FileStream(nomeArquivo, FileMode.Create);
var writer = PdfWriter.GetInstance(pdf, arquivo);
writer.PageEvent = new EventosDePagina(totalPaginas);
pdf.Open();
// Adição do titulo
var fontParagrafo = new iTextSharp.text.Font(fonteBase, 22, iTextSharp.text.Font.NORMAL, BaseColor.Black);
var fontParagrafo2 = new iTextSharp.text.Font(fonteBase, 10, iTextSharp.text.Font.ITALIC, BaseColor.Black);
var titulo = new Paragraph("Relatório de Pessoas\n", fontParagrafo);
var titulo2 = new Paragraph("As empresas costumam fazer consultas a um CPF quando estão prestes a fechar\n um negócio ou quando o consumidor solicita crédito. \n\n", fontParagrafo2);
titulo.Alignment = Element.ALIGN_LEFT;
titulo.SpacingAfter = 4;
pdf.Add(titulo);
pdf.Add(titulo2);
// Adião da imagem
var caminhoImage = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "img\\youtube.png");
if (File.Exists(caminhoImage))
{
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(caminhoImage);
float razaoAlturaLargura = logo.Width / logo.Height;
float alturaLogo = 75;
float larguraLogo = alturaLogo * razaoAlturaLargura;
logo.ScaleToFit(larguraLogo, alturaLogo);
var margemEsquerda = pdf.PageSize.Width - pdf.RightMargin - larguraLogo;
var margemTopo = pdf.PageSize.Height - pdf.TopMargin - 55;
logo.SetAbsolutePosition(margemEsquerda, margemTopo);
writer.DirectContent.AddImage(logo, false);
}
// Adição da tabela de dados
var tabela = new PdfPTable(5);
float[] largurasColunas = { 0.6f, 2f, 1.5f, 1f, 1f };
tabela.SetWidths(largurasColunas);
tabela.DefaultCell.BorderWidth = 0;
tabela.WidthPercentage = 100;
// Adição da celulas de titulos das colunas
CriarCelulaTexto(tabela, "Código", PdfCell.ALIGN_CENTER, true);
CriarCelulaTexto(tabela, "Nome", PdfCell.ALIGN_LEFT, true);
CriarCelulaTexto(tabela, "Profissão", PdfCell.ALIGN_CENTER, true);
CriarCelulaTexto(tabela, "Salário", PdfCell.ALIGN_CENTER, true);
CriarCelulaTexto(tabela, "Empregada", PdfCell.ALIGN_CENTER, true);
// Adição dos valores das celulas
foreach(var p in pessoasSelecionadas)
{
CriarCelulaTexto(tabela, p.IdPessoa.ToString("D6"), PdfPCell.ALIGN_CENTER);
CriarCelulaTexto(tabela, p.Nome + " " + p.Sobrenome);
CriarCelulaTexto(tabela, p.Profissao.Nome, PdfPCell.ALIGN_CENTER);
CriarCelulaTexto(tabela, p.Salario.ToString("C2"), PdfPCell.ALIGN_RIGHT);
//CriarCelulaTexto(tabela, p.Empregado ? "Sim" : "Não", PdfPCell.ALIGN_CENTER);
var caminhoImagemCelula = p.Empregado ? "img\\emoji_feliz.png" : "img\\emoji_triste.png";
caminhoImagemCelula = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, caminhoImagemCelula);
CriarCelulaImagem(tabela, caminhoImagemCelula, 20, 20);
}
pdf.Add(tabela);
pdf.Close();
arquivo.Close();
// Abre o PDF no visualizador padrão
//var caminhoPDF = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, nomeArquivo);
//if (File.Exists(caminhoPDF))
//{
// Process.Start(new ProcessStartInfo()
// {
// Arguments = $"/c start {caminhoPDF}",
// FileName = "cmd.exe",
// CreateNoWindow = true
// }
// );
//}
}
}
static void CriarCelulaTexto(PdfPTable tabela, string texto, int alinhamentoHorizontal = PdfPCell.ALIGN_LEFT, bool negrito = false, bool italico = false, int tamanhoFonte = 12, int alturaCelula = 25 )
{
int estilo = iTextSharp.text.Font.NORMAL;
if (negrito && italico)
{
estilo = iTextSharp.text.Font.BOLDITALIC;
}else if (negrito)
{
estilo = iTextSharp.text.Font.BOLD;
}
else if (italico)
{
estilo = iTextSharp.text.Font.ITALIC;
}
var fonteCelula = new iTextSharp.text.Font(fonteBase, tamanhoFonte, estilo, BaseColor.Black);
var bgColor = iTextSharp.text.BaseColor.White;
if (tabela.Rows.Count % 2 == 1)
{
bgColor = new BaseColor(0.95F, 0.95F, 0.95F);
}
var celula = new PdfPCell(new Phrase(texto, fonteCelula));
celula.HorizontalAlignment = alinhamentoHorizontal;
celula.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
celula.Border = 0;
celula.BorderWidthBottom = 1;
celula.FixedHeight = alturaCelula;
celula.PaddingBottom = 5;
celula.BackgroundColor = bgColor;
tabela.AddCell(celula);
}
static void CriarCelulaImagem(PdfPTable tabela, string caminhoImagem, int larguraImagem, int alturaImagem, int alturaCelula = 25)
{
var bgColor = iTextSharp.text.BaseColor.White;
if (tabela.Rows.Count % 2 == 1)
{
bgColor = new BaseColor(0.95F, 0.95F, 0.95F);
}
if (File.Exists(caminhoImagem))
{
iTextSharp.text.Image imagem = iTextSharp.text.Image.GetInstance(caminhoImagem);
imagem.ScaleToFit(larguraImagem, alturaImagem);
var celula = new PdfPCell(imagem);
celula.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
celula.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
celula.Border = 0;
celula.BorderWidthBottom = 1;
celula.FixedHeight = alturaCelula;
celula.BackgroundColor = bgColor;
tabela.AddCell(celula);
}
}
}
}