您的位置:首页 > 娱乐 > 明星 > iTextSharp 绘制pdf

iTextSharp 绘制pdf

2025/4/19 22:39:45 来源:https://blog.csdn.net/xiaoxionglove/article/details/139874367  浏览:    关键词:iTextSharp 绘制pdf

一、新建项目:pdfdemo

<ItemGroup><PackageReference Include="iTextSharp.LGPLv2.Core" Version="3.4.20" />
</ItemGroup>

二、HomeController.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;}}
}

运行效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com