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

         
    }
}

运行效果:

相关推荐
10km9 分钟前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
customer0810 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
灰色人生qwer17 分钟前
SpringBoot 项目配置日志输出
java·spring boot·后端
2301_7930698227 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
阿华的代码王国34 分钟前
【从0做项目】Java搜索引擎(6)& 正则表达式鲨疯了&优化正文解析
java·后端·搜索引擎·正则表达式·java项目·从0到1做项目
服务端相声演员34 分钟前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
是姜姜啊!35 分钟前
java连接redis
java·redis
hhw19911236 分钟前
spring boot知识点5
java·数据库·spring boot
EQUINOX138 分钟前
lab4 CSAPP:Cachelab
java·后端·spring
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS打卡健康评测系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源