引入Nuget包

  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System;
  4.  
  5. using System.IO;
  6.  
  7. namespace ConsoleApp1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13.  
  14. //生成pdf
  15. Document document = new Document();
  16.  
  17. var fileStream = File.Create("aa.pdf");
  18. PdfWriter pw = PdfWriter.GetInstance(document, fileStream);
  19.  
  20. document.Open();
  21. //写入数据
  22. //注册字体
  23. string fontpath = "a.ttf";
  24. BaseFont customfont = BaseFont.CreateFont(fontpath, BaseFont.CP1252, BaseFont.EMBEDDED);
  25.  
  26. var baseFont = new Font(customfont);
  27. baseFont.Color = new BaseColor(System.Drawing.Color.Red);
  28. baseFont.Size = 30;
  29.  
  30. #region 头部
  31. PdfPTable tableLogo = new PdfPTable(3);
  32. tableLogo.DefaultCell.Border = Rectangle.NO_BORDER;
  33. tableLogo.DefaultCell.MinimumHeight = 40f;
  34. float[] headWidths = new float[] { 60f, 60f, 150f };
  35. tableLogo.SetWidths(headWidths);
  36. //logo
  37. var logo = iTextSharp.text.Image.GetInstance(File.ReadAllBytes("aa.png"));
  38. // logo.ScaleToFit(130 * 0.667f, 43 * 0.667f);
  39. var logoCell = new PdfPCell();
  40. logoCell.PaddingLeft = -1f;
  41. logoCell.MinimumHeight = 45f;
  42. logoCell.HorizontalAlignment = Element.ALIGN_LEFT;
  43. logoCell.Border = 0;
  44. logoCell.AddElement(logo);
  45. tableLogo.AddCell(logoCell);
  46.  
  47. var sCell = new PdfPCell(new Paragraph("SmartFind", baseFont));
  48. sCell.Border = 0;
  49. sCell.PaddingLeft = 3f;
  50. sCell.PaddingBottom = 17f;
  51. sCell.MinimumHeight = 45f;
  52. sCell.HorizontalAlignment = Element.ALIGN_LEFT;
  53. sCell.VerticalAlignment = Element.ALIGN_BOTTOM;
  54. tableLogo.AddCell(sCell);
  55.  
  56. var aCell = new PdfPCell();
  57. aCell.Border = 0;
  58. aCell.MinimumHeight = 45f;
  59. aCell.PaddingBottom = 17f;
  60. aCell.HorizontalAlignment = Element.ALIGN_LEFT;
  61. aCell.VerticalAlignment = Element.ALIGN_BOTTOM;
  62.  
  63. var acc = new Paragraph("| Accessories", baseFont);
  64. aCell.AddElement(acc);
  65. tableLogo.AddCell(aCell);
  66.  
  67. document.Add(tableLogo);
  68. #endregion
  69.  
  70. document.Add(new Paragraph(""));
  71.  
  72. //页脚
  73. PDFFooter footer = new PDFFooter();
  74. footer.OnEndPage(pw, document);
  75. document.Close();
  76. fileStream.Dispose();
  77. }
  78. //This implementation is obviously not very good --> Though it should be enough for everyone to implement their own.
  79.  
  80. }
  81.  
  82. public class PDFFooter : PdfPageEventHelper
  83. {
  84. // write on top of document
  85. public override void OnOpenDocument(PdfWriter writer, Document document)
  86. {
  87. base.OnOpenDocument(writer, document);
  88. PdfPTable tabFot = new PdfPTable(new float[] { 1F });
  89. tabFot.SpacingAfter = 10F;
  90. PdfPCell cell;
  91. tabFot.TotalWidth = 300F;
  92. cell = new PdfPCell(new Phrase("Header"));
  93. tabFot.AddCell(cell);
  94. tabFot.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
  95. }
  96.  
  97. // write on start of each page
  98. public override void OnStartPage(PdfWriter writer, Document document)
  99. {
  100. base.OnStartPage(writer, document);
  101. }
  102.  
  103. // write on end of each page
  104. public override void OnEndPage(PdfWriter writer, Document document)
  105. {
  106. base.OnEndPage(writer, document);
  107. //PdfPTable tabFot = new PdfPTable(new float[] { 1F });
  108. //tabFot.TotalWidth = 700f;
  109. //tabFot.DefaultCell.Border = 0;
  110. //// var footFont = FontFactory.GetFont("Lato", 12 * 0.667f, new Color(60, 60, 60));
  111. //string fontpath = HttpContext.Current.Server.MapPath("~/App_Data");
  112. //BaseFont customfont = BaseFont.CreateFont(fontpath + "\\Lato-Regular.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
  113. //var footFont = new Font(customfont, 12 * 0.667f, Font.NORMAL, new Color(170, 170, 170));
  114.  
  115. //PdfPCell cell;
  116. //cell = new PdfPCell(new Phrase("@ 2016 . All Rights Reserved", footFont));
  117. //cell.VerticalAlignment = Element.ALIGN_CENTER;
  118. //cell.Border = 0;
  119. //cell.PaddingLeft = 100f;
  120. //tabFot.AddCell(cell);
  121. //tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
  122. }
  123.  
  124. //write on close of document
  125. public override void OnCloseDocument(PdfWriter writer, Document document)
  126. {
  127. base.OnCloseDocument(writer, document);
  128. }
  129. }
  130. }

  

  

.net core 使用 textSharp生成pdf的更多相关文章

  1. .net Core 使用 iTextSharp 生成PDF 简单示例

    在 Nuget 中导入需要的插件: 实现的代码: 1 [HttpGet, Route("CreatePdf")] 2 public Response CreatePdf() 3 { ...

  2. 使用Rotativa在ASP.NET Core MVC中创建PDF

    在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP.NET MVC,那么Rot​​ativa工具已经可用,我们可以使用 ...

  3. NET Core MVC中创建PDF

    使用Rotativa在ASP.NET Core MVC中创建PDF 在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP ...

  4. asp.net生成PDF文件参考 .

    TextSharp 是用来生成  PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 HTML 来生成报告,但是,使用 ...

  5. java生成pdf

    介绍 本篇博客主要是为了介绍如何使用:flying-saucer+itext+freemark实现导出复杂点的pdf文件. 思路 先把pdf的内容以html形式准备好 使用freemarker将htm ...

  6. 如何在ASP.NET Core 中快速构建PDF文档

    比如我们需要ASP.NET Core 中需要通过PDF来进行某些简单的报表开发,随着这并不难,但还是会手忙脚乱的去搜索一些资料,那么恭喜您,这篇帖子会帮助到您,我们就不会再去浪费一些宝贵的时间. 在本 ...

  7. C# Net Core 使用 itextsharp.lgplv2.core 把Html转PDF

    C# Net Core 使用 itextsharp.lgplv2.core 把Html转PDF 只支持英文(中文我不知道怎么弄,懂的朋友帮我看一下)!!!!![补充:评论区的小伙伴已解决] 引入包it ...

  8. 前端生成PDF,让后端刮目相看

    PDF 简介 PDF 全称Portable Document Format (PDF)(便携文档格式),该格式的显示与操作系统.分辨率.设备等因素没有关系,不论是在Windows,Unix还是在苹果公 ...

  9. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

随机推荐

  1. [Vue @Component] Define Props on a Vue Class with vue-property-decorator

    While traditional Vue components require a data function which returns an object and a method object ...

  2. SpriteBuilder&Cocos2D使用CCEffect特效实现天黑天亮过度效果

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 在动作或RPG类游戏中我们有时须要天黑和天亮过度的效果来完毕场 ...

  3. 微软公有云Azure是惠及全人类的计算资源

    回归往事,1975年,微软以DOS创业.在随后的三十年中,微软给人类贡献了视窗操作系统Windows,至今,人们对桌面操作系统XP仍然不离不弃.可是,面对互联网的兴起.微软应该怎么办呢? 微软内部不乏 ...

  4. 金典 SQL笔记 SQL语句汇总

    SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY id) AS rowN,FNumber, FName,FSalary ,Fage FROM dbo ...

  5. 用NuGet安装NewtonSoft.json

    因为要在C#里读取JSON字符串,资料查来查去,发现只能用第三方的NewtonSoft.json.本来.net也有自带的类库可以处理json,但TM的不停要你将JSON读进类对象里面.我靠,我只不过想 ...

  6. 通过scp批量推送文件到远程目录

    [root@openfire1 script]# cat test.sh  #!/bin/bash   #通过scp推送文件到远程目录 for ip in `cat iplist`  do       ...

  7. 安卓dex 文件结构简要说明

    #ifndef _DEX_FILE_HELPER_ #define _DEX_FILE_HELPER_ //此文件仅仅是起帮助作用,帮助不太了解DexFile结构的了解一下DexFile相关结构,想更 ...

  8. oc80--copy

    // // main.m // Copy基本使用,拷贝的本质:修改其中一个不能影响另外一个. // 每个oc对象都有copy和mutableCopy方法,前提是必须遵守NSCopying协议实现cop ...

  9. [Swift通天遁地]六、智能布局-(8)布局框架的使用:多分辨率适配和横竖屏布局

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. 判断人员js

    var allchooseEmpID = "";var allchooseEmpName = "";//自选经办人 function getJbrWinForM ...