.net core 使用 textSharp生成pdf

引入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);
        }
    }
}

  

  

转载于:https://www.cnblogs.com/IWings/p/7263766.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值