当前位置: 首页 > news >正文

NetCore itext7 创建、编辑PDF插入表格、图片、文字(三)

NetCore 创建、编辑PDF插入表格、图片、文字
NetCore 创建、编辑PDF插入表格、图片、文字(二)
NetCore 创建、编辑PDF插入表格、图片、文字(三)

直接上代码

nuget引入 itext7

using System;
using System.IO;using iText.IO.Image;
using iText.Kernel.Colors;  // 导入颜色类库
using iText.Kernel.Geom;
using iText.Kernel.Pdf;     // 导入PDF操作类库
using iText.Layout;
using iText.Layout.Borders; // 导入布局类库
using iText.Layout.Element; // 导入元素类库
using iText.Layout.Properties;
using iText.Pdfa;
using TextAlignment = iText.Layout.Properties.TextAlignment; // 导入属性类库
private void PDF(){string textsss ="ssssssssssssssssssssssdddddddddddddddddddddddddddddddddddddddffffffffffffffffffffffffffffffffffffffffffffffffffff";string filePath = "path/to/yourDocument.pdf";// 检查目录是否存在,如果不存在则创建string directory = System.IO.Path.GetDirectoryName(filePath);if (!Directory.Exists(directory)){Directory.CreateDirectory(directory);}// 创建PDF文档var writer = new PdfWriter(filePath); // 创建PDF写入器,指定文件路径var pdf = new PdfDocument(writer); // 创建PDF文档var document = new Document(pdf, PageSize.A4); // 创建文档对象// 设置分页尺寸//PageSize customPageSize = new PageSize(500, 500); // 设置自定义的分页尺寸,单位为点(1英寸=72点)//pdf.SetDefaultPageSize(customPageSize);float x = PageSize.A3.GetWidth() / 2;float y = PageSize.A3.GetTop() - 20;#region 文本写入string ss ="Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1Text 1";// 文字自定义坐标var wb = new Paragraph(ss).SetWidth(200).SetFontSize(12).SetFixedPosition(1, 50, 750, 800);document.Add(wb); // 第1页,X=50,Y=750document.Add(new Paragraph($"Text 2{textsss}").SetFixedPosition(1, 50, 700, 800)); // 第1页,X=50,Y=700document.Add(new Paragraph($"Text 3{textsss}").SetFixedPosition(1, 50, 650, 500)); // 第1页,X=50,Y=650//从上到下document.Add(new Paragraph($"居左{textsss}").SetTextAlignment(TextAlignment.LEFT));document.Add(new Paragraph($"居中{textsss}").SetTextAlignment(TextAlignment.CENTER));document.Add(new Paragraph($"局右{textsss}").SetTextAlignment(TextAlignment.RIGHT));#endregion// 表格设置Table table = new Table(5) // 创建表格,5列.SetMarginTop(10) // 设置上边距.SetFixedPosition(1, 36, 600, 200) // 设置固定位置.SetBorder(Border.NO_BORDER); // 设置无边框// 单元格设置Cell cell1 = new Cell(1, 5).Add(new Paragraph("Merged Row 1")) // 创建合并单元格,跨5列.SetBackgroundColor(ColorConstants.LIGHT_GRAY) // 设置背景色为浅灰色.SetFontColor(ColorConstants.BLUE) // 设置字体颜色为蓝色.SetFontSize(10) // 设置字体大小为10.SetTextAlignment(TextAlignment.CENTER); // 设置文本居中对齐Cell cell2 = new Cell(1, 2).Add(new Paragraph("Merged Row 2")) // 创建合并单元格,跨2列.SetBackgroundColor(ColorConstants.YELLOW) // 设置背景色为黄色.SetFontColor(ColorConstants.GREEN) // 设置字体颜色为绿色.SetFontSize(10) // 设置字体大小为10.SetTextAlignment(TextAlignment.CENTER); // 设置文本居中对齐Cell cell3 = new Cell(1, 3).Add(new Paragraph("Merged Row 3")) // 创建合并单元格,跨3列.SetBackgroundColor(ColorConstants.ORANGE) // 设置背景色为橙色.SetFontColor(ColorConstants.MAGENTA) // 设置字体颜色为品红色.SetFontSize(10) // 设置字体大小为10.SetTextAlignment(TextAlignment.CENTER); // 设置文本居中对齐// 创建单元格并设置背景颜色for (int i = 0; i < 5; i++) // 遍历5列{Cell cell = new Cell().Add(new Paragraph($"Cell {i + 1}")) // 创建单元格,添加文本.SetFontSize(10) // 设置字体大小为10.SetTextAlignment(TextAlignment.CENTER); // 设置文本居中对齐// 设置不同的背景色switch (i){case 0:cell.SetBackgroundColor(ColorConstants.LIGHT_GRAY); // 第一列背景色为浅灰色break;case 1:cell.SetBackgroundColor(ColorConstants.YELLOW); // 第二列背景色为黄色break;case 2:cell.SetBackgroundColor(ColorConstants.ORANGE); // 第三列背景色为橙色break;case 3:cell.SetBackgroundColor(ColorConstants.CYAN); // 第四列背景色为青色break;case 4:cell.SetBackgroundColor(ColorConstants.PINK); // 第五列背景色为粉色break;}table.AddCell(cell); // 将单元格添加到表格}// 将合并单元格添加到表格table.AddCell(cell1);table.AddCell(cell2);table.AddCell(cell3);// 将表格添加到文档document.Add(table);ImageData imageData = ImageDataFactory.Create("path/to/123222.jpg");Image image1 = new Image(imageData).SetFixedPosition(2, 50, 300, 200) // 指定坐标  第2页,X=50,Y=300,宽度200;Image image2 = new Image(imageData);image2.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER);  //居中image2.SetWidth(200);;document.Add(image1);document.Add(image2);// 关闭文档document.Close();}

相关文章:

  • R语言使用dietaryindex包计算NHANES数据多种营养指数(2)
  • 自动化组高度件切割计算
  • 为什么Python不适合写游戏?
  • React 应用实现监控可观测性最佳实践
  • 【中间件】docker数据卷
  • 使用Docker搭建YesPlayMusic网易云音乐播放器并发布至公网访问
  • 小米汽车正式发布:开启智能电动新篇章
  • MongoDB内存过高问题分析解决
  • ChatGPT与传统搜索引擎的区别:智能对话与关键词匹配的差异
  • |行业洞察·趋势报告|《2024旅游度假市场简析报告-17页》
  • VSCode 如何同步显示网页在手机或者平板上
  • C语言数据结构基础——排序
  • 影视文件数字指纹签名检验系统的用户操作安全大多数
  • 数据库管理开发工具Navicat for MySQL Mac版下载
  • 在js中push的使用方法总结;
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • extjs4学习之配置
  • JavaScript新鲜事·第5期
  • js继承的实现方法
  • JS题目及答案整理
  • k个最大的数及变种小结
  • leetcode388. Longest Absolute File Path
  • MYSQL 的 IF 函数
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • React-flux杂记
  • Yeoman_Bower_Grunt
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 创建一种深思熟虑的文化
  • 计算机在识别图像时“看到”了什么?
  • 经典排序算法及其 Java 实现
  • 前嗅ForeSpider教程:创建模板
  • 手写一个CommonJS打包工具(一)
  • 探索 JS 中的模块化
  • 通信类
  • 小程序开发中的那些坑
  • 延迟脚本的方式
  • 函数计算新功能-----支持C#函数
  • 正则表达式-基础知识Review
  • # include “ “ 和 # include < >两者的区别
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (九)信息融合方式简介
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (算法)求1到1亿间的质数或素数
  • ****Linux下Mysql的安装和配置
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .net core控制台应用程序初识
  • .net mvc 获取url中controller和action
  • .NET框架设计—常被忽视的C#设计技巧
  • .net专家(高海东的专栏)
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • [ SNOI 2013 ] Quare