.net 8,iText 7,export string,image, list<class>as table in pdf

install-package itext7;
install-package itext7.bouncy-castle-adapter;

 

using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.Diagnostics;

namespace ConsoleApp13
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IText7PdfDemo();
            Console.WriteLine("Hello, World!");
        }

        static void IText7PdfDemo()
        {
            try
            {
                string pdfFile = $"output_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.pdf";
                string imagePath = @"../../../Images/1.jpg";

                List<Book> booksList = new List<Book>();
                for (int i = 0; i<100; i++)
                {
                    booksList.Add(new Book()
                    {
                        Id=i+1,
                        Name=$"Name_{i+1}",
                        Author=$"Author_{i+1}",
                        Title=$"Title_{i+1}",
                        ISBN=$"ISBN_{i+1}_{Guid.NewGuid().ToString("N")}",
                        Summary=$"Summary_{i+1}",
                        Topic=$"Topic_{i+1}"
                    });
                }

                using (var writer = new PdfWriter(pdfFile))
                using (var pdf = new PdfDocument(writer))
                using (var document = new Document(pdf,iText.Kernel.Geom.PageSize.A3))
                {
                    PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);

                    //// Add a paragraph with font and size
                    //Paragraph p = new Paragraph("Hello iText7 with custom font & size!")
                    //    .SetFont(font)
                    //    .SetFontSize(18)       // font size
                    //    .SetBold()             // optional bold
                    //    .SetItalic();          // optional italic
                    // Add text
                    document.Add(new Paragraph("iText 7 Export string,image data list in PDF!")
                        .SetFont(font)
                        .SetFontSize(50)
                        .SimulateBold()
                        .SimulateBold()
                        .SetFontColor(ColorConstants.BLUE));
                    document.Add(new Paragraph($"Exported on {DateTime.Now:yyyy-MM-dd HH:mm:ss}"));

                    // Add image
                    if (File.Exists(imagePath))
                    {
                        ImageData imgData = ImageDataFactory.Create(imagePath);
                        var img = new Image(imgData).ScaleToFit(imgData.GetWidth(), imgData.GetHeight()); // resize if needed
                        document.Add(img);
                    }
                    else
                    {
                        document.Add(new Paragraph("⚠ Image file not found."));
                    }

                    // Add title
                    document.Add(new Paragraph("Books List")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(16)
                        .SimulateBold());

                    document.Add(new Paragraph($"Generated on {DateTime.Now:yyyy-MM-dd HH:mm:ss}")
                        .SetTextAlignment(TextAlignment.RIGHT)
                        .SetFontSize(10));

                    document.Add(new Paragraph("\n")); // spacing

                    // Define table with 7 columns
                    Table table = new Table(new float[] { 1, 2, 2, 4, 2, 3, 2 });
                    table.SetWidth(UnitValue.CreatePercentValue(100));

                    // Add headers
                    string[] headers = { "Id", "Name", "Author", "ISBN", "Title", "Summary", "Topic" };
                    foreach (var header in headers)
                    {
                        table.AddHeaderCell(new Cell().Add(new Paragraph(header).SimulateBold()));
                    }

                    // Add book data
                    foreach (var book in booksList)
                    {
                        table.AddCell(book.Id.ToString());
                        table.AddCell(book.Name ?? "");
                        table.AddCell(book.Author ?? "");
                        table.AddCell(book.ISBN ?? "");
                        table.AddCell(book.Title ?? "");
                        table.AddCell(book.Summary ?? "");
                        table.AddCell(book.Topic ?? "");
                    }

                    // Add table to document
                    document.Add(table);
                }

                Process proc = new Process()
                {
                    StartInfo=new ProcessStartInfo()
                    {
                        FileName=pdfFile,
                        UseShellExecute=true,
                    }
                };
                proc.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }            
        }  
    }

    public class Book
    {
        public int Id {  get; set; }
        public string Name {  get; set; }
        public string Author { get; set; }
        public string ISBN { get; set; }
        public string Summary { get; set; }
        public string Topic { get; set; }
        public string Title {  get; set; }
    }
}

 

 

 

 

image

 

 

 

 

image

 

 

image

 

 

 

image

 

posted @ 2025-08-22 19:11  FredGrit  阅读(10)  评论(0)    收藏  举报