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

         
    }
}

运行效果:

相关推荐
fxybg202215 分钟前
AI助力PPT制作:开启高效创作新时代
人工智能·学习·pdf·word·powerpoint
南浔Pyer18 分钟前
AI驱动的Java开发框架:Spring AI Alibaba实战部署教程
java·人工智能·spring boot·spring·maven·idea
深情的小陈同学1 小时前
Linux —— udp实现群聊代码
linux·运维·服务器
神的孩子都在歌唱2 小时前
行为设计模式 -模板方法模式- JAVA
java·设计模式·模板方法模式
Satan7124 小时前
【Java】虚拟机(JVM)内存模型全解析
java·开发语言·jvm
小城哇哇4 小时前
AI大模型对我国劳动力市场潜在影响研究报告(2024)|附19页PDF文件下载
人工智能·学习·ai·语言模型·pdf·大模型·agi
啵一杯4 小时前
leetcode621. 任务调度器
服务器·前端·数据结构·算法·c#
KookeeyLena85 小时前
提取出来的ip与我原本的ip是在同一个区吗
服务器·网络协议·tcp/ip
远望樱花兔5 小时前
【d54_2】【Java】【力扣】142.环形链表
java·leetcode·链表
IT学长编程5 小时前
计算机毕业设计 助农产品采购平台的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·助农产品采购平台