iTextSharp 绘制pdf

一、新建项目:pdfdemo

复制代码
<ItemGroup>
   <PackageReference Include="iTextSharp.LGPLv2.Core" Version="3.4.20" />
</ItemGroup>

二、HomeController.cs

cs 复制代码
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using pdfdemo.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace pdfdemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var path = $"{AppDomain.CurrentDomain.BaseDirectory}/{DateTime.Now.ToFileTime()}.pdf";
            var document = new Document(PageSize.A4.Rotate());
            document.SetMargins(10, 10, 50, 10);
            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                var writer = PdfWriter.GetInstance(document, fileStream);
                document.Open();
                var rowHeight = 25;
                var table = new PdfPTable(6);
                table.WidthPercentage = 100;
                var cellTdDes = new PdfPCell(new Phrase("A"));
                cellTdDes.Colspan = 3;
                cellTdDes.HorizontalAlignment = Element.ALIGN_LEFT;
                cellTdDes.VerticalAlignment = Element.ALIGN_MIDDLE;
                cellTdDes.FixedHeight = rowHeight;
                table.AddCell(cellTdDes);

                var cellQuantityHeader = new PdfPCell(new Phrase("B"));
                WrapCell(rowHeight, cellQuantityHeader);
                var cellPriceHeader = new PdfPCell(new Phrase("C"));
                WrapCell(rowHeight, cellPriceHeader);
                var cellAmountHeader = new PdfPCell(new Phrase("D"));
                WrapCell(rowHeight, cellAmountHeader);
                table.AddCell(cellQuantityHeader);
                table.AddCell(cellPriceHeader);
                table.AddCell(cellAmountHeader);

                var baseFont = BaseFont.CreateFont(@"c:/windows/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                var font = new iTextSharp.text.Font(baseFont, 10, iTextSharp.text.Font.NORMAL);
                var fixedHeight = 40;
                for (int i = 0; i < 8; i++)
                {
                    var cellTd = new PdfPCell(new Phrase("你好", font));
                    cellTd.Colspan = 3;
                    cellTd.FixedHeight = fixedHeight;
                    cellTd.VerticalAlignment = Element.ALIGN_MIDDLE;
                    table.AddCell(cellTd);

                    var cellQuantity = new PdfPCell(new Phrase("66"));
                    WrapCell(fixedHeight, cellQuantity);

                    var cellPrice = new PdfPCell(new Phrase("666"));
                    WrapCell(fixedHeight, cellPrice);

                    var cellAmount = new PdfPCell(new Phrase("666"));
                    WrapCell(fixedHeight, cellAmount);
                    table.AddCell(cellQuantity);
                    table.AddCell(cellPrice);
                    table.AddCell(cellAmount);
                }

                document.Add(table);
                writer.Flush();
                document.Close();
                document.Dispose();
            }
            return View();
        }
        private static void WrapCell(int fixedHeight, PdfPCell cell)
        {
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.FixedHeight = fixedHeight;
        }

         
    }
}

运行效果:

相关推荐
张人玉25 分钟前
C# 常量与变量
java·算法·c#
Java技术小馆38 分钟前
GitDiagram如何让你的GitHub项目可视化
java·后端·面试
tan77º41 分钟前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp
Codebee1 小时前
“自举开发“范式:OneCode如何用低代码重构自身工具链
java·人工智能·架构
程序无bug1 小时前
手写Spring框架
java·后端
程序无bug1 小时前
Spring 面向切面编程AOP 详细讲解
java·前端
全干engineer1 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Fireworkitte2 小时前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
szxinmai主板定制专家2 小时前
【精密测量】基于ARM+FPGA的多路光栅信号采集方案
服务器·arm开发·人工智能·嵌入式硬件·fpga开发
GodKeyNet2 小时前
设计模式-责任链模式
java·设计模式·责任链模式