• VS中生成pdf


     

     生成PDF代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using DB;
    //生成PDF
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    
    
    
    
    
    namespace Action
    {
        public partial class BaoBiao : Form
        {
            int sum;
            public BaoBiao()
            {
                InitializeComponent();
            }
    
            private void BaoBiao_Load(object sender, EventArgs e)
            {
               
                dataGridView1.AllowUserToAddRows = false;
    
    
                show("0","0");
                //this.reportViewer1.RefreshReport();
            }
     
    
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                createImg();
                createPDF();
            }
            public void createImg()
            {
                //String fileName = GetTimeStamp();//获取时间戳
                string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Imgpdf.jpg"; //临时文件路径
                chart1.SaveImage(imagePath, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);
            }
    
            //时间戳
            public string GetTimeStamp()
            {
                TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                return Convert.ToInt64(ts.TotalSeconds).ToString();
            }
      
            //PDF生成图片生成器
            public iTextSharp.text.Paragraph createPDFimg(PdfWriter writer)
            {
                string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Imgpdf.jpg"; //临时文件路径
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
                img.SetAbsolutePosition(100,400);
                writer.DirectContent.AddImage(img);
                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
                p.Alignment = Element.ALIGN_CENTER;
                return p;
            }
            //PDF生成表格生成器
            public PdfPTable createPDFtable(PdfWriter writer)
            {
                string start = this.start.Text;
                string over = this.over.Text;
                BaseFont baseFont = BaseFont.CreateFont("C:\WINDOWS\FONTS\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont, 11);
                iTextSharp.text.Font font2 = new iTextSharp.text.Font(baseFont, 15);
                float[] widths = new float[] { 120, 180, 240 };//三列列宽不同若果是浮点数需要加f
                
                PdfPTable table = new PdfPTable(3);
                table.SetWidths(widths);
                PdfPCell cell;
                cell = new PdfPCell(new Phrase("霸气鞋店销售单据",font2));
                cell.HorizontalAlignment = 1;
                cell.Rowspan = 2;
                cell.Colspan = 3;
                
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("销售状况",font2));
                cell.Rowspan = 2;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("时间区间", font1));
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(start+"-"+over, font1));
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("盈利", font1));
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(sum+"", font1));
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("单据时间", font1));
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(DateTime.Now.ToString("yyyy-MM-dd"), font1));
                cell.Colspan = 2;
                table.AddCell(cell);
                
                return table;
            }
            //生成PDF
            public void createPDF()
            {
                string fileName = string.Empty;
               SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = "我的第一个PDF";
                dlg.DefaultExt = ".pdf";
                dlg.Filter = "Text documents (.pdf)|*.pdf";
                DialogResult result = dlg.ShowDialog();
                if (result == DialogResult.OK && dlg.FileName.Length > 0)
                {
                    fileName = dlg.FileName;
                    Document document = new Document();
                    BaseFont baseFont = BaseFont.CreateFont("C:\WINDOWS\FONTS\STSONG.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 15);
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                    document.Open();
                    Paragraph p = new Paragraph("公司单据", font);
                    p.Alignment = 1;
    
                    document.Add(p);
                    Paragraph p0 = new Paragraph("  ", font);
                    p0.Alignment = 1;
    
                    document.Add(p0);
                    PdfPTable table=createPDFtable(writer);
                    document.Add(table);
                    Paragraph p1 = new Paragraph("领导签字:_______________", font);
                    p1.Alignment = 1;
    
                    document.Add(p1); 
                    iTextSharp.text.Paragraph img=createPDFimg(writer);
                    document.Add(img);
                    document.Close();
                }//end if
                
            
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                string start="";
                string over="";
                start = this.start.Text;
                over = this.over.Text;
                show(start, over);
            }
    
            //刷新表格
            public void show(string start,string over)
            {
                this.dataGridView1.Rows.Clear();
                List<string> xData = new List<string>();
                List<int> yData = new List<int>();
                List<XSBean> list = DB.Get.getXS(start, over);
                int sum = DB.Get.getSum(start, over);
    
                foreach (var date in list)
                {
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = date.getTime();
                    this.dataGridView1.Rows[index].Cells[1].Value = date.getProfit();
                    xData.Add(date.getTime());
                    yData.Add(date.getProfit());
                }
                
                chart1.Series[0]["PieLabelStyle"] = "Outside";//将文字移到外侧
                chart1.Series[0]["PieLineColor"] = "Black";//绘制黑色的连线。
                chart1.Series[0].Points.DataBindXY(xData, yData);
                this.number.Text = sum+"";
            }
    
            private void number_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
  • 相关阅读:
    HttpClient
    spring入门
    morphia进阶
    morphia基本API方法
    mangodb数据库框架morphia注解
    学与思
    解决vscode执行yarn启动项目报错
    使用swiper+动画实现轮播图自动播放
    vue中使用el-tree实现一行显示多条数据
    使用git命令提交部分修改代码
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14211902.html
Copyright © 2020-2023  润新知