iTextSharp 绘制pdf

2024-06-23 17:28
文章标签 itextsharp pdf 绘制

本文主要是介绍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;}}
}

运行效果:

这篇关于iTextSharp 绘制pdf的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087802

相关文章

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

XMG 绘制形状

1. 除非是绘制曲线直接使用原生的。如果绘制形状直接使用UIBezerPath  2. 命名原则,类方法以类名开头 UIBezierPath bezierPathWithRect 3.圆角半径 画圆的大小 以每个顶点为圆心。给定的半径为半径画一个1/4圆。把周边的给切掉 4.只有封闭的形状调用这个方法才有用 [path fill] 5. stroke 描边一下

Kimichat使用案例026:AI翻译英语PDF文档的3种方法

文章目录 一、介绍二、腾讯交互翻译TranSmart https://transmart.qq.com/三、沉浸式翻译三、谷歌网页翻译 一、介绍 短的文章,直接丢进kimichat、ChatGPT里面很快就可以翻译完成,而且效果很佳。但是,很长的PDF文档整篇需要翻译,怎么办呢? 二、腾讯交互翻译TranSmart https://transmart.qq.com/ 软件

【若依前后端分离】前端vue页面查看服务器本地的PDF

后端实现: 使用FileSystemResource包装文件,以便Spring MVC可以处理该资源 创建HttpHeaders对象以设置响应头 设置Content-Disposition头,使得浏览器以内联方式显示PDF(即在浏览器中直接打开) 设置Content-Type为application/pdf,指示响应体是一个PDF文件 构建并返回带有指定头信息和PDF资源的ResponseE

CesiumJS【Basic】- #008 通过canvas绘制billboard

文章目录 通过canvas绘制billboard1 目标2 实现 通过canvas绘制billboard 1 目标 通过canvas绘制billboard 2 实现 /** @Author: alan.lau* @Date: 2024-06-16 11:15:48* @LastEditTime: 2024-06-16 11:43:02* @LastEditors: al

使用AGG里面的clip_box函数裁剪画布, 绘制裁剪后的图形

// 矩形裁剪图片, 透明void agg_testImageClipbox_rgba32(unsigned char* buffer, unsigned int width, unsigned int height){// ========= 创建渲染缓冲区 =========agg::rendering_buffer rbuf;// BMP是上下倒置的,为了和GDI习惯相同,最后一个参数是

Canvas绘制图片和区域

如何使用Canvas在图片上绘制区域? 一. 首先,我们需要初始化三个canvas画布(初始化Canvas)   initCanvas() {// 初始化canvas画布let canvasWrap = document.getElementsByClassName("canvas-wrap");this.wrapWidth = canvasWrap[0].clientWidth;thi

解析PDF文件中的图片为文本

解析PDF文件中的图片为文本 1 介绍 解析PDF文件中的图片,由两种思路,一种是自己读取PDF文件中的图片,然后用OCR解析,例如:使用PyMuPDF读取pdf文件,再用PaddleOCR或者Tesseract-OCR识别文字。另一种使用第三方框架,直接读取文字,例如:OCRmyPDF。 读取pdf的包PyMuPDF可以获取PDF中的文本、布局和图片等,并且内嵌了Tesseract-OCR

PDF 文本复制自动去换行符,去格式

一、参考内容 GitHub:PDF 文本复制自动去换行符,去格式

Rmarkdown的PDF文件插入本地图片

借助在其他端上的文件时不可以的,测试成功,在本地指定文件路径即可,如下: ![Caption for the picture.](/home/rstudio/1.png)