1 Star 0 Fork 0

idwyx / PDFSharpDemo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Program.cs 6.26 KB
一键复制 编辑 原始数据 按行查看 历史
Gui.H 提交于 2017-08-06 14:58 . 完成
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);
}
}
}
C#
1
https://gitee.com/idwyx/PDFSharpDemo.git
git@gitee.com:idwyx/PDFSharpDemo.git
idwyx
PDFSharpDemo
PDFSharpDemo
master

搜索帮助