当前位置: 首页> 健康> 美食 > 河南省住房和城乡建设厅网站_冰燃建站_怎么写软文_郑州网站开发公司

河南省住房和城乡建设厅网站_冰燃建站_怎么写软文_郑州网站开发公司

时间:2025/8/27 4:37:19来源:https://blog.csdn.net/zxb11c/article/details/145029794 浏览次数:0次
河南省住房和城乡建设厅网站_冰燃建站_怎么写软文_郑州网站开发公司

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

1,.net8 环境,引入 包
itext7
itext7.bouncy-castle-adapter
2,直接上代码

   public class PDFEditor{public static void ExportFile(string outputPath, PDFEditorPdfModle m){// 创建PDF文档using (PdfWriter writer = new PdfWriter(outputPath)){using (PdfDocument pdf = new PdfDocument(writer)){iText.Layout.Document document = new iText.Layout.Document(pdf);// 加载支持中文的字体文件(例如:微软雅黑)string fontPath = @"Fonts\msyh.ttf"; // 字体文件路径PdfFont chineseFont = PdfFontFactory.CreateFont(fontPath, PdfEncodings.IDENTITY_H);// 创建页头表格Table headerTable = new Table(new float[] { 1, 1 }) // 两列,宽度均分.UseAllAvailableWidth() // 使用全部可用宽度.SetMarginTop(0).SetMarginBottom(1); // 设置下边距// 第一列:左边写10个汉字,居左对齐Cell leftCell = new Cell().Add(new Paragraph("红外热成像检测报告").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT)) // 居左对齐.SetBorder(Border.NO_BORDER); // 隐藏单元格边框// 第二列:右边写10个汉字,居右对齐Cell rightCell = new Cell().Add(new Paragraph($"检测机构: {m.HospitalName}").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.RIGHT)) // 居右对齐.SetBorder(Border.NO_BORDER); // 隐藏单元格边框// 将单元格添加到表格中headerTable.AddCell(leftCell);headerTable.AddCell(rightCell);// 将页头表格添加到文档中document.Add(headerTable);Text spaceText = new Text("   "); // 空格Text nameText = new Text(@"姓名:  "); // 正常文本Text nameTextVal = new Text($@"  {m.Name}    "); // 正常文本nameTextVal.SetUnderline(1f,-5f);Text sexText = new Text(@"性别:  "); //  Text sexTextVal = new Text($@"  {m.Sex}    "); //  sexTextVal.SetUnderline(1f,-5f);Text ageText = new Text(@"年龄:  "); //  Text ageTextVal = new Text($@"  {m.Age}    "); //  ageTextVal.SetUnderline(1f,-5f);Text dateText = new Text(@"检查日期:  "); //  Text dateTextVal = new Text($@"  {m.Date}    "); //  dateTextVal.SetUnderline(1f,-5f);Text departmentText = new Text(@"科室:  "); //  Text departmentTextVal = new Text($@"  {m.Department}    "); //  departmentTextVal.SetUnderline(1f,-5f);// 添加文字Paragraph paragraph = new Paragraph( ).Add(nameText).Add(nameTextVal).Add(spaceText).Add(sexText).Add(sexTextVal).Add(spaceText).Add(ageText).Add(ageTextVal).Add(spaceText).Add(dateText).Add(dateTextVal).Add(spaceText).Add(departmentText).Add(departmentTextVal).SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(11).SetMarginTop(16); // 设置上边距document.Add(paragraph);document.Add(new Paragraph("热图所见:").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(14).SetMarginTop(20));// 定义图片宽度和高度float imageWidth = 150; // 图片宽度float imageHeight = 200; // 图片高度// 创建一个2行2列的表格,设置列宽为图片宽度//Table table = new Table(new float[] { imageWidth, imageWidth }) // 两列,每列宽度为图片宽度//    .UseAllAvailableWidth(); // 不使用全部可用宽度List<float> tableColWidth = new List<float>();tableColWidth.Add(1f);if (m.Images.Count > 1)tableColWidth.Add(1f);Table table = new Table(tableColWidth.ToArray()).SetMarginBottom(10)// 设置下边距.UseAllAvailableWidth();table.SetBorder(Border.NO_BORDER);  // 隐藏表格线for (int i = 0; i < m.Images.Count; i++){string imagePath = m.Images[i]; // 图片路径,假设图片名为 sample1.jpg, sample2.jpg, 等等ImageData imageData = ImageDataFactory.Create(imagePath);iText.Layout.Element.Image image = new iText.Layout.Element.Image(imageData).SetWidth(imageWidth) // 设置图片宽度.SetHeight(imageHeight); // 设置图片高度// 创建一个 Paragraph 包裹图片,并设置对齐方式Paragraph imageParagraph = new Paragraph().Add(image).SetTextAlignment(i % 2 == 1 ? TextAlignment.LEFT : TextAlignment.RIGHT); // 第一列居右,第二列居左   if (m.Images.Count == 1){imageParagraph.SetTextAlignment(TextAlignment.CENTER);}// 将图片添加到单元格中Cell cell = new Cell().Add(imageParagraph).SetBorder(Border.NO_BORDER); //table.AddCell(cell);}// 创建一个 Div 容器,用于居中表格Div div = new Div().SetTextAlignment(TextAlignment.CENTER) // 设置内容居中.SetWidth(UnitValue.CreatePercentValue(100)) // 设置 Div 宽度占满页面//.SetBorder(new SolidBorder(ColorConstants.BLACK, 1)) // 设置 Div 边框.SetPadding(10) // 设置 Div 内边距.Add(table); // 将表格添加到 Div 中// 将 Div 添加到文档中document.Add(div);document.Add(new Paragraph("检查描述:").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(14).SetMarginTop(20));document.Add(new Paragraph(m.CheckDescription).SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(9).SetMarginTop(10));document.Add(new Paragraph("分析结论:").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(14).SetMarginTop(20));document.Add(new Paragraph().Add(new Text(m.ConclusionAndAnalysis)).SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(9).SetMarginTop(10));document.Add(new Paragraph($"报告时间:{m.Datetime}   检查医师:{m.DoctorName}    审核医师:").SetFont(chineseFont) // 设置中文字体.SetTextAlignment(TextAlignment.LEFT).SetFontSize(11).SetMarginTop(20));// 关闭文档document.Close();}}}public class PDFEditorPdfModle{public PDFEditorPdfModle(){Images = new List<string>();}public string HospitalName { get; set; }public string Name { get; set; }public int Age { get; set; }public string Sex { get; set; }public string Date { get; set; }public string Datetime { get; set; }public string Department { get; set; }public string CheckDescription { get; set; }public string ConclusionAndAnalysis { get; set; }public string DoctorName { get; set; }public List<string> Images { get; set; }}}
关键字:河南省住房和城乡建设厅网站_冰燃建站_怎么写软文_郑州网站开发公司

版权声明:

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

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

责任编辑: