C# 产生PDF

环境安装

到NuGet抓取4.1.6版
后面的版本要权限控管比较严格

目标

产生PDF表格有标题跟row

程式

using System;using System.Collections.Generic;using System.IO;using iTextSharp.text;using iTextSharp.text.pdf;internal class Program{    private static void Main(string[] args)    {        // 建立测试用的资料        List<Row> rows = new List<Row>();        List<Cell> cells = new List<Cell>();        // 假设要产生 5 列 3 栏的表格        int rowCount = 5;        int columnCount = 3;        // 产生乱数资料填充到 cells 阵列中        Random random = new Random();        for (int i = 0; i < rowCount * columnCount; i++)        {            cells.Add(new Cell(random.Next(100).ToString())); // 以乱数填充储存格的值        }        // 根据 rows 和 cells 阵列建立资料表        CreateTable(rows, cells, rowCount, columnCount);        // 建立 PDF 文件        string outputPath = "D://txt//test.PDF";        Document document = new Document();        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));        document.Open();        // 建立字型        BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);        Font font = new Font(baseFont, 12, Font.NORMAL);        // 建立 PDF 表格        PdfPTable table = new PdfPTable(columnCount);        table.WidthPercentage = 100;        // 加入表头        foreach (Cell cell in rows[0].Cells)        {            PdfPCell headerCell = new PdfPCell(new Phrase(cell.Value, font));            table.AddCell(headerCell);        }        // 加入资料        for (int i = 1; i < rows.Count; i++)        {            foreach (Cell cell in rows[i].Cells)            {                PdfPCell dataCell = new PdfPCell(new Phrase(cell.Value, font));                table.AddCell(dataCell);            }        }        // 将表格加入 PDF 文件        document.Add(table);        // 关闭文件        document.Close();        writer.Close();        Console.WriteLine("PDF 文件已建立完成。");    }    public static void CreateTable(List<Row> rows, List<Cell> cells, int rowCount, int columnCount)    {        // 建立表头        Row headerRow = new Row();        for (int i = 0; i < columnCount; i++)        {            headerRow.Cells.Add(new Cell($"Header {i + 1}"));        }        rows.Add(headerRow);        // 建立资料列        for (int i = 0; i < rowCount; i++)        {            Row dataRow = new Row();            for (int j = 0; j < columnCount; j++)            {                int index = i * columnCount + j;                if (index < cells.Count)                {                    dataRow.Cells.Add(cells[index]);                }            }            rows.Add(dataRow);        }    }    public class Row    {        public List<Cell> Cells { get; set; }        public Row()        {            Cells = new List<Cell>();        }    }    public class Cell    {        public string Value { get; set; }        public Cell(string value)        {            Value = value;        }    }}

https://hackmd.io/dRoFmR2sSI-7-cugfQDZPw


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章