代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.Diagnostics;
namespace AAA
{
class Program
{
static void Main()
{
Creat(@"D:\HelloWorld_.pdf", "大连成光网络技术有限公司", "名称", "123456789123456789", "浦发银行", DateTime.Now);
}
public static void Creat(string filename, string companyName, string name, string cardid, string bank, DateTime data)
{
// 创建PDF文档
PdfDocument document = new PdfDocument();
document.Info.Title = "关于移动支付平台商户账号变更的说明";
// 添加空白页
PdfPage page = document.AddPage();
// 获取画布
XGraphics gfx = XGraphics.FromPdfPage(page);
// 字体配置
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
// 使用自定义字体
string strFontPath = @".\Font\经典宋体简.TTF";// 字体设置为简宋
pfcFonts.AddFontFile(strFontPath);
// 定义两种大小字体
XFont fontTitle = new XFont(pfcFonts.Families[0], 17, XFontStyle.Regular, options);
XFont fontBody = new XFont(pfcFonts.Families[0], 14, XFontStyle.Regular, options);
XFont fontUndline = new XFont(pfcFonts.Families[0], 14, XFontStyle.Underline, options);
// 文档标题距离顶部距离
int Pading = 50;
int TopHeight = 50;
int linePading = 7;
int fontCount = ((int)page.Width - Pading * 2) / fontBody.Height; // 一行显示的字数
string TitleString = "关于移动支付平台商户账号变更的说明";
string line1 = "我公司通过与平安银行温州分行合作的移动支付业务入驻的商户";
string line2 = ",因银行卡信息有误,导致用户无法收到结算款项,现申请将银行卡信息变更如下:";
string[] lines = new string[] {
"用户名:" + name,
"银行账号:" + cardid,
"开户行:" + bank,
"请予以修正。"
};
// 标题
gfx.DrawString(TitleString, fontTitle, XBrushes.Black, new XRect(0, TopHeight, page.Width, fontTitle.Height), XStringFormats.Center);
// 换行
TopHeight += fontBody.Height + 20;
// 正文
gfx.DrawString(line1, fontBody, XBrushes.Black, new XRect(Pading + fontBody.Height * 2, TopHeight, page.Width - Pading, fontBody.Height), XStringFormats.TopLeft);
int leftCount = fontCount - line1.Length - 2;// 剩余能够显示的字数
int leftWidth = leftCount * fontBody.Height;
// 公司名称显示后,该行有空余空间
if (companyName.Length < leftCount)
{
int subLength = leftCount - companyName.Length;
string subline2 = line2.Substring(0, subLength);
line2 = line2.Replace(subline2, "");
gfx.DrawString(companyName, fontUndline, XBrushes.Black, new XRect(page.Width - Pading - leftWidth, TopHeight, leftWidth, fontBody.Height), XStringFormats.TopLeft);
gfx.DrawString(subline2, fontBody, XBrushes.Black, new XRect(page.Width - Pading - subLength * fontBody.Height, TopHeight, leftWidth, fontBody.Height), XStringFormats.TopLeft);
// 换行
TopHeight += fontBody.Height + linePading;
gfx.DrawString(line2, fontBody, XBrushes.Black, new XRect(Pading, TopHeight, page.Width - Pading, fontBody.Height), XStringFormats.TopLeft);
}
else // 公司名称较长导致换行显示的情况
{
string subCompany = companyName.Substring(0, leftCount);
int subLength = companyName.Length - subCompany.Length;
gfx.DrawString(subCompany, fontUndline, XBrushes.Black, new XRect(page.Width - Pading - leftWidth, TopHeight, leftWidth, fontBody.Height), XStringFormats.TopLeft);
// 换行
TopHeight += fontBody.Height + linePading;
gfx.DrawString(companyName.Replace(subCompany, ""), fontUndline, XBrushes.Black, new XRect(Pading, TopHeight, leftWidth, fontBody.Height), XStringFormats.TopLeft);
string subline2 = line2.Substring(0, fontCount - subLength);
gfx.DrawString(subline2, fontBody, XBrushes.Black, new XRect(Pading + subLength * fontBody.Height, TopHeight, leftWidth, fontBody.Height), XStringFormats.TopLeft);
line2 = line2.Replace(subline2, "");
// 换行
TopHeight += fontBody.Height + linePading;
gfx.DrawString(line2, fontBody, XBrushes.Black, new XRect(Pading, TopHeight, page.Width - Pading, fontBody.Height), XStringFormats.TopLeft);
}
// 变更信息
for (int i = 0; i < lines.Length; i++)
{
// 换行
TopHeight += fontBody.Height + linePading;
gfx.DrawString(lines[i], fontBody, XBrushes.Black, new XRect(Pading * 2, TopHeight, page.Width - Pading, fontBody.Height), XStringFormats.TopLeft);
}
// 换行
TopHeight += fontBody.Height + 20;
TopHeight += fontBody.Height + 20;
gfx.DrawString("公司(盖章)", fontBody, XBrushes.Black, new XRect(page.Width - Pading * 4, TopHeight, page.Width, fontBody.Height), XStringFormats.TopLeft);
TopHeight += fontBody.Height + 5;
gfx.DrawString("日期 " + data.ToString("yyy-MM-dd"), fontBody, XBrushes.Black, new XRect(page.Width - Pading * 4, TopHeight, page.Width, fontBody.Height), XStringFormats.TopLeft);
// 印章
XImage img = XImage.FromFile(@".\File\seal.gif");
gfx.DrawImage(img, new System.Drawing.Point() { X = 400, Y = 250 });
// Save the document...
document.Save(filename);
Process.Start(filename);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。