使用PdfSharp生成Pdf文件
上代码:
private void button1_Click(object sender, EventArgs e) { // 创建新的PDF文档 PdfDocument document = new PdfDocument(); // 创建空页 PdfPage page = document.AddPage(); page.Size = PdfSharp.PageSize.A4; //纸张横向 page.Orientation = PageOrientation.Landscape; // 设置一个画布 XGraphics gfx = XGraphics.FromPdfPage(page); // 设置字体 单位:px //System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection(); //string strFontPath = @"C:/Windows/Fonts/msyh.ttc";//字体设置为微软雅黑 //pfcFonts.AddFontFile(strFontPath); //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); //XFont font = new XFont(pfcFonts.Families[0], 15, XFontStyle.Regular, options); System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection(); string strFontPath = @"C:/Windows/Fonts/AdobeSongStd-Light.otf";//字体设置为微软雅黑 pfcFonts.AddFontFile(strFontPath); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont font = new XFont(pfcFonts.Families[0], 12, XFontStyle.Bold, options); XFont formFont = new XFont(pfcFonts.Families[0], 20, XFontStyle.Bold, options); XFont fontTitle = new XFont(pfcFonts.Families[0], 25, XFontStyle.Bold, options); var bottomTop = 25; var titleTop = 25; var formTop = 10; var formTitleTop = 10; // 设置(添加)文本 //添加表头 gfx.DrawString("采购合同", fontTitle, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormat.TopCenter); gfx.DrawString("PURCHAR ORDER", fontTitle, XBrushes.Black, new XRect(0, bottomTop, page.Width, page.Height), XStringFormat.TopCenter); //添加信息 gfx.DrawString("合同编号:123456 版本号:1", font, XBrushes.Black, new XRect(0, bottomTop * 2 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); gfx.DrawString("需方:XX有限公司", font, XBrushes.Black, new XRect(0, bottomTop * 3 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); gfx.DrawString("供方:XX有限公司", font, XBrushes.Black, new XRect(0, bottomTop * 4 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); gfx.DrawString("电话:", font, XBrushes.Black, new XRect(0, bottomTop * 5 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); gfx.DrawString("交货地点:", font, XBrushes.Black, new XRect(0, bottomTop * 6 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); gfx.DrawString("备注:(T/T 45DAY) 请提供增值税发票 (币别:人民币RMB)", font, XBrushes.Black, new XRect(0, bottomTop * 7 + titleTop, page.Width, page.Height), XStringFormat.TopLeft); // 定义旋转中心 //gfx.TranslateTransform(page.Width / 2, page.Height / 2); //gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); //gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // 图片 //string imgPath = @"C:\Users\dzw\Desktop\RFID制图\TIME AND TRU(TTSO56009)\TIME AND TRU(TTSO56009)正面图.png"; //XImage image = XImage.FromFile(imgPath); ////double x = (gfx.PageSize.Width - image.PixelWidth * 72 / image.HorizontalResolution) / 2; ////double y = (gfx.PageSize.Height - image.PixelHeight * 72 / image.VerticalResolution) / 2; //gfx.DrawImage(image, 10, 30); //// 设置(添加)文本 ////gfx.DrawString("123124121", font, XBrushes.Black, //// new XRect(0, 0, page.Width, page.Height) //// ); //gfx.DrawString("这是一行中文", font, XBrushes.Black, 0, 60 + image.PixelHeight); //绘制表单 //画线 XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1); XPoint startXPoint = new XPoint(0, bottomTop * 8 + titleTop + formTop); XPoint endXPoint = new XPoint(page.Width, bottomTop * 8 + titleTop + formTop); gfx.DrawLine(pen, startXPoint, endXPoint); //表头 gfx.DrawString("订单编号 RFID款式/Item Code Size 数量/Qty 交货日期/Delivery Date", formFont, XBrushes.Black, new XRect(0, bottomTop * 7 + titleTop + formTop + formTitleTop, page.Width, page.Height), XStringFormat.TopLeft); //画线 XPen pen2 = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1); XPoint startXPoint2 = new XPoint(0, bottomTop * 9 + titleTop + formTop + formTitleTop); XPoint endXPoint2 = new XPoint(page.Width, bottomTop * 9 + titleTop + formTop + formTitleTop); gfx.DrawLine(pen2, startXPoint2, endXPoint2); //内容 gfx.DrawString("202105180001 TREDSAFE (TRSO41011) 7½ 10000 2021-05-18", formFont, XBrushes.Black, new XRect(0, bottomTop * 10 + formTitleTop, page.Width, page.Height), XStringFormat.TopLeft); // 保存文档 string filename = @"C:\Users\dzw\Desktop\合同评审单pdf\666.pdf"; document.Save(filename); MessageBox.Show("PDF生成完成"); }
导出结果:
这里碰到7½导出的问题,后续查看一下
感谢:https://www.jb51.net/article/208776.htm
https://blog.csdn.net/qqqqqqqqqq198968/category_9282871.html