引入Nuget包

using iTextSharp.text;
using iTextSharp.text.pdf;
using System; using System.IO; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{ //生成pdf
Document document = new Document(); var fileStream = File.Create("aa.pdf");
PdfWriter pw = PdfWriter.GetInstance(document, fileStream); document.Open();
//写入数据
//注册字体
string fontpath = "a.ttf";
BaseFont customfont = BaseFont.CreateFont(fontpath, BaseFont.CP1252, BaseFont.EMBEDDED); var baseFont = new Font(customfont);
baseFont.Color = new BaseColor(System.Drawing.Color.Red);
baseFont.Size = 30; #region 头部
PdfPTable tableLogo = new PdfPTable(3);
tableLogo.DefaultCell.Border = Rectangle.NO_BORDER;
tableLogo.DefaultCell.MinimumHeight = 40f;
float[] headWidths = new float[] { 60f, 60f, 150f };
tableLogo.SetWidths(headWidths);
//logo
var logo = iTextSharp.text.Image.GetInstance(File.ReadAllBytes("aa.png"));
// logo.ScaleToFit(130 * 0.667f, 43 * 0.667f);
var logoCell = new PdfPCell();
logoCell.PaddingLeft = -1f;
logoCell.MinimumHeight = 45f;
logoCell.HorizontalAlignment = Element.ALIGN_LEFT;
logoCell.Border = 0;
logoCell.AddElement(logo);
tableLogo.AddCell(logoCell); var sCell = new PdfPCell(new Paragraph("SmartFind", baseFont));
sCell.Border = 0;
sCell.PaddingLeft = 3f;
sCell.PaddingBottom = 17f;
sCell.MinimumHeight = 45f;
sCell.HorizontalAlignment = Element.ALIGN_LEFT;
sCell.VerticalAlignment = Element.ALIGN_BOTTOM;
tableLogo.AddCell(sCell); var aCell = new PdfPCell();
aCell.Border = 0;
aCell.MinimumHeight = 45f;
aCell.PaddingBottom = 17f;
aCell.HorizontalAlignment = Element.ALIGN_LEFT;
aCell.VerticalAlignment = Element.ALIGN_BOTTOM; var acc = new Paragraph("| Accessories", baseFont);
aCell.AddElement(acc);
tableLogo.AddCell(aCell); document.Add(tableLogo);
#endregion document.Add(new Paragraph("")); //页脚
PDFFooter footer = new PDFFooter();
footer.OnEndPage(pw, document);
document.Close();
fileStream.Dispose();
}
//This implementation is obviously not very good --> Though it should be enough for everyone to implement their own. } public class PDFFooter : PdfPageEventHelper
{
// write on top of document
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.SpacingAfter = 10F;
PdfPCell cell;
tabFot.TotalWidth = 300F;
cell = new PdfPCell(new Phrase("Header"));
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, document.Top, writer.DirectContent);
} // write on start of each page
public override void OnStartPage(PdfWriter writer, Document document)
{
base.OnStartPage(writer, document);
} // write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
//PdfPTable tabFot = new PdfPTable(new float[] { 1F });
//tabFot.TotalWidth = 700f;
//tabFot.DefaultCell.Border = 0;
//// var footFont = FontFactory.GetFont("Lato", 12 * 0.667f, new Color(60, 60, 60));
//string fontpath = HttpContext.Current.Server.MapPath("~/App_Data");
//BaseFont customfont = BaseFont.CreateFont(fontpath + "\\Lato-Regular.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
//var footFont = new Font(customfont, 12 * 0.667f, Font.NORMAL, new Color(170, 170, 170)); //PdfPCell cell;
//cell = new PdfPCell(new Phrase("@ 2016 . All Rights Reserved", footFont));
//cell.VerticalAlignment = Element.ALIGN_CENTER;
//cell.Border = 0;
//cell.PaddingLeft = 100f;
//tabFot.AddCell(cell);
//tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
} //write on close of document
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
}

  

  

.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. Android 学习历程摘要(三)

    1.proguard混淆:对与Activity等不应进行混淆.否则系统将找不到生命周期函数:JSON字符串要考虑混淆之后是否会影响字段名:native方法也不混淆,否则调用不到 2.ListView的 ...

  2. lua中的闭包小结

    function newCounter() return function() i=i+ return i end end c1=newCounter() print(c1()) print(c1() ...

  3. namenode启动成功,但是不能通过web访问50070问题

    我在CentOS遇到这个问题,50070不行但8088可以,尝试了各种方法无法解决,各个进程全都启动,格式化namenode,各种配置正常均无法解决.后来觉得是默认访问端口没有生效,所以尝试添加端口配 ...

  4. Git flow的分支模型与及经常使用命令简单介绍

    Git flow是git的一个扩展集,它基于Vincent Driessen 的分支模型,文章"A successful Git branching model"对这一分支模型进行 ...

  5. 推断client手机类型,并跳转到对应的app下载页面

    实现的原理,是检測浏览器的 USER-AGENT 这个header,然后依据正則表達式来确定client类型. 假设都不匹配,Fallback回退策略是显示相应的页面.让用户自己选择. 适合採用二维码 ...

  6. java线程和线程池的使用

    java线程和线程池 一.创建多线程的方式 java多线程非经常见.怎样使用多线程,怎样创建线程.java中有两种方式,第一种是让自己的类实现Runnable接口.另外一种是让自己的类继承Thread ...

  7. Filters.h各种信号恢复滤波器头文件

    这篇文章有部分原理:http://blog.csdn.net/u013467442/article/details/41125473 代码下载地址:http://read.pudn.com/downl ...

  8. myeclipse中Servlet出错

    在myeclipse中Servlet总是出错,执行的时候一直提示找不到出现404错误, 然后把代码拷贝到eclipse ee中就没有问题,一直不理解怎么回事. 然后发了好长时间试了好些方法,结果把my ...

  9. sed 之 -n p

    sed是一个面向字符流的编辑器,一般情况下每次读入一行到一个名为模式空间的地方,进行编辑:但是也可以读入多行数据进行编辑. -n:抑制默认输出 p打印模式空间内容 cat test a b sed ' ...

  10. Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0

    今天使用Android Studio 2.0打开我之前的项目时,编译报了如下错误: Error:Cause: com/android/build/gradle/internal/model/Defau ...