• .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);
            }
        }
    }
    

      

      

  • 相关阅读:
    android ksoap2 访问webservice,连续两次调用时,第二次调用异常(转)
    iOS Programming – 触摸事件处理 (转)
    iPhone的解锁、越狱、激活、固件等等是什么意思,有什么分别?(转)
    pb中 执行动态sql
    Java的中文字体
    fastreport一些使用方法
    网页数据抓取
    base64
    TSelect
    解决DLL包组织的项目运行报 a class named Txxx exists
  • 原文地址:https://www.cnblogs.com/IWings/p/7263766.html
Copyright © 2020-2023  润新知