Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Eu preciso gerar um relatório em PDF, varrendo um DataGridView.
Porém não basta adicionar apenas parágrafos, quero que no PDF gere igual na aplicação, quando executo o DataGridView. É possível?


Google translation:
I need to generate a PDF report by scanning a DataGridView.
But not just add paragraphs, I want the PDF to generate the same in the application, when I run the DataGridView. It's possible?

What I have tried:

C#
Document doc = new Document();

// Open window where the pdf report file will be saved
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Arquivo PDF (.pdf)| *.pdf"; // restrict the file format to save, and set the extension as .pdf
saveFileDialog.Title = "Selecione aonde deseja salvar o arquivo"; // checkbox title that opens
saveFileDialog.ShowDialog(); // open the check box

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(saveFileDialog.FileName, FileMode.Create));

// Open document
doc.Open();

// Parameter creation to create the header
Paragraph paragrafo1 = new Paragraph(Font.BOLD);

               
paragrafo1.Add("Cliente     ");
paragrafo1.Add("Livro         ");
paragrafo1.Add("Date de Empréstimo        ");
paragrafo1.Add("Data de Devolução        ");
paragrafo1.Add("Status       ");
paragrafo1.Add("Multa        ");
                
// adding header
doc.Add(paragrafo1);

// Creating pdfTable
PdfPTable pdfTable = new PdfPTable(5);

foreach (DataGridViewRow row in dataGrid.Rows)
{
    // Populated pdfTable with information from the current row of the dataGrid passed as parameter
    pdfTable.Rows.Add(row.DataBoundItem.ToString());

    // Creating new paragraph
    Paragraph paragrafo = new Paragraph();

    // Adding PDFTable Line to Paragraph
    paragrafo.AddAll(pdfTable.Rows.ToArray());

    // Adding paragraph to document
    doc.Add(paragrafo);
}

// Closing the document
doc.Close();
Posted
Updated 27-Feb-18 9:08am
v2
Comments
Richard MacCutchan 27-Feb-18 4:19am    
Please write your question in English.
Richard Deeming 27-Feb-18 12:12pm    
I've translated your question to English using Google Translate[^].

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900