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;
        }

         
    }
}

运行效果:

相关推荐
覆东流5 小时前
7.Java数组
java·开发语言·后端
m0_719084115 小时前
限流和获取请求ip的方法
java
lzhdim5 小时前
提高 SQL 语句执行速度的方法
java·开发语言·数据库·sql·oracle
星野川崎2065 小时前
电商多店运维:云机24小时挂机频繁掉线、账号无故风控深度原因分析及解决方案
大数据·运维·服务器·云计算·电商
深漂的华哥6 小时前
Ruoyi-Plus前后端分离场景下,数据加密传输
java·spring boot·后端·开源·maven·ruoyi
RisunJan6 小时前
Linux命令-xauth(X11 认证授权管理)
linux·服务器·microsoft
落魄大学生之流水线上谋生计7 小时前
Java锁全面指南:从基础概念到企业级应用
java·开发语言
熊IT7 小时前
什么是原生住宅 IP?如何判断一个 IP 是否真正“原生”?
服务器·网络·tcp/ip
爱学习的小白柏8 小时前
【AI问数技术】多Agent协同架构:查询规划/SQL生成/洞察分析/报告生成
java·网络·人工智能·windows·sql·架构·llama
码匠许师傅8 小时前
【C++ 面试真题】30. 聊聊 C++ 的互斥锁与读写锁
java·c++·面试