81 lines
2.8 KiB
C#
81 lines
2.8 KiB
C#
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Drawing.Printing;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
public class PdfPrinter
|
|
{
|
|
public static void PrintDocument(DataGridView dataGridView1)
|
|
{
|
|
Document doc = new Document();
|
|
|
|
try
|
|
{
|
|
// Nome do arquivo temporário
|
|
string tempFilePath = Path.GetTempFileName() + ".pdf";
|
|
|
|
// Criar o arquivo PDF temporário
|
|
using (FileStream fs = new FileStream(tempFilePath, FileMode.Create))
|
|
{
|
|
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
|
|
{
|
|
doc.Open();
|
|
|
|
// Adicionar cabeçalho "Dados de Pagamento"
|
|
Paragraph cabecalho = new Paragraph("Dados de Pagamento\n\n");
|
|
cabecalho.Alignment = Element.ALIGN_CENTER;
|
|
doc.Add(cabecalho);
|
|
|
|
// Adicionar conteúdo ao PDF a partir do DataGridView
|
|
PdfPTable table = new PdfPTable(dataGridView1.ColumnCount);
|
|
for (int i = 0; i < dataGridView1.ColumnCount; i++)
|
|
{
|
|
table.AddCell(new Phrase(dataGridView1.Columns[i].HeaderText));
|
|
}
|
|
table.HeaderRows = 1;
|
|
for (int i = 0; i < dataGridView1.Rows.Count; i++)
|
|
{
|
|
for (int j = 0; j < dataGridView1.Columns.Count; j++)
|
|
{
|
|
if (dataGridView1[j, i].Value != null)
|
|
{
|
|
table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString()));
|
|
}
|
|
}
|
|
}
|
|
doc.Add(table);
|
|
}
|
|
}
|
|
|
|
// Fechar o documento
|
|
doc.Close();
|
|
|
|
// Selecionar a impressora
|
|
PrintDialog printDialog = new PrintDialog();
|
|
if (printDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
// Imprimir o documento PDF na impressora selecionada
|
|
ProcessStartInfo info = new ProcessStartInfo();
|
|
info.Verb = "printto";
|
|
info.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
|
|
info.FileName = tempFilePath;
|
|
info.CreateNoWindow = true;
|
|
info.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
|
Process p = new Process();
|
|
p.StartInfo = info;
|
|
p.Start();
|
|
|
|
MessageBox.Show("Documento PDF enviado para impressão!");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Erro ao imprimir o documento PDF: " + ex.Message);
|
|
}
|
|
}
|
|
}
|