WPF iTextSharp, export data in datagrid in pdf via mvvm

Install-Package itextsharp;
Install-Package System.Drawing.Common;

 

 

 

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Image = iTextSharp.text.Image;



private ICommand saveDataInPDFCommand;
public ICommand SaveDataInPDFCommand
{
    get
    {
        if(saveDataInPDFCommand==null)
        {
            saveDataInPDFCommand=new DelCommand(SaveDataInPDFCommandExecuted);
        }
        return saveDataInPDFCommand;
    }
}

private void SaveDataInPDFCommandExecuted(object? obj)
{
    var dg = obj as DataGrid;
    if(dg!=null)
    {
        var booksList=dg.Items.Cast<Book>()?.ToList();
        string pdfFileName = $"Export_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.pdf";
        SaveListTInPDF<Book>(booksList,pdfFileName);

        var proc = new Process()
        {
            StartInfo=new ProcessStartInfo()
            {
                FileName=pdfFileName,
                UseShellExecute=true,
            }
        };
        proc.Start();
    }
}

public void SaveListTInPDF<T>(List<T> dataList,string pdfFileName)
{
    if (dataList!=null)
    {
        
        using (FileStream fileStream = new FileStream(pdfFileName, FileMode.Create))
        {
            using (Document doc = new Document(PageSize.A1, 20, 20, 200, 50))
            {
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fileStream))
                {
                    string logoPath = @"../../../Images/logo.png";
                    if (File.Exists(logoPath))
                    {
                        writer.PageEvent=new PdfHeaderFooter("SMOOTHTECH", logoPath);
                        doc.Open();

                        Font baseFont = FontFactory.GetFont(FontFactory.HELVETICA, 50f);
                        Paragraph p = new Paragraph("Data List From Datagrid", baseFont);
                        p.SpacingAfter=50;
                        doc.Add(p);

                        var props=typeof(T).GetProperties();
                        var columnNames=props.Select(x=>x.Name).ToList();

                        PdfPTable table = new PdfPTable(columnNames.Count);
                        table.SetWidths(new float[] { 0.05f, 0.15f, 0.1f, 0.1f, 0.3f, 0.1f, 0.1f, 0.1f });
                        table.WidthPercentage=100;

                        foreach (var column in columnNames)
                        {
                            PdfPCell cell = new PdfPCell(new Phrase(column, new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD)));
                            table.AddCell(cell);
                        }

                        foreach (var bk in dataList)
                        {
                            foreach (var prop in props)
                            {
                                var strValue = prop.GetValue(bk)?.ToString()??string.Empty;
                                table.AddCell(new Phrase(strValue, new Font(Font.FontFamily.HELVETICA, 20)));
                            }
                        }

                        doc.Add(table);
                        doc.Close();
                    }
                }
            }
        }
    }
}

 

image

 

image

 

 

 

 

 

image

 

 

 

 

 

 

 

 

image

 

posted @ 2025-08-27 17:32  FredGrit  阅读(6)  评论(0)    收藏  举报