使用的开源库名字叫PDFsharp
下载地址为:
PDFsharp
使用举例:
先下载好开源代码,拷贝例子代码,加引用,然后就能使用了。
就是简单的写字和画图(代码拷贝过去就能用):
class Program
{
static void Main(string[] args)
{
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
string strFontPath = @"C:/Windows/Fonts/msyh.ttf";//字体设置为微软雅黑
pfcFonts.AddFontFile(strFontPath);
XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
XFont font = new XFont(pfcFonts.Families[0], 15, XFontStyle.Regular, options);
// gfx.DrawString("你好, 世界!", font, XBrushes.Black,new XRect(0, 0, page.Width, page.Height),XStringFormats.Center);
List<int> cd = GetResult(100, 4);
XPen pen = new XPen(XColors.Red,0.1);
XBrush brush = new XSolidBrush(XColor.FromArgb(96, 182, 100));
XBrush brush1 = new XSolidBrush(XColor.FromArgb(235, 74, 70));
XBrush brush2 = new XSolidBrush(XColor.FromArgb(168, 0, 168));
XBrush brush3 = new XSolidBrush(XColor.FromArgb(102, 153, 255));
int y = 20;
foreach (var item in cd)
{
gfx.DrawString(item.ToString(), font, XBrushes.Black, new XPoint(10, y));
y = y + 20;
}
// gfx.DrawRoundedRectangle(brush, new XRect(20, 50, 100, 10), new XSize(1, 1));
gfx.DrawRoundedRectangle(brush, new XRect(20, 150, 100, 5), new XSize(5, 5));
gfx.DrawRoundedRectangle(brush1, new XRect(cd[0] + 20, 150, cd[1], 5), new XSize(0, 0));
gfx.DrawRoundedRectangle(brush3, new XRect(cd[0] + 20 + cd[1], 150, cd[3] + cd[2], 5), new XSize(5, 5));
gfx.DrawRoundedRectangle(brush2, new XRect(cd[0] + 20 + cd[1], 150, cd[2], 5), new XSize(0, 0));
foreach (var item in cd)
{
Console.WriteLine(item);
}
byte[] arr = Convert.F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0EOPsFwlgDvwGM8zzPLof4iJdT3LNa4MjbaG/tvLdQx0uwrDuTyx5F+cn2+FkRWdjf3z9/woQJVoMjNs6XU90JVdqlWCzOUtUNYRi+m06nO0eMamUQP0AMVCwWd4dh6PT19T0yadKkvpGCX3RkGiQIguXA8jAMX0un05+NBNi6kkGXRKFQaLAWR0TO1NfXLx4zZszvww1f2p09LDB6wK2798K1aBDf95eIyFMiciiZTNr5PWwjaoEckZao+buo9QmCwEqg2XXdVYlE4uBwkDNd2TUIq6ubvlrNnjV4TdYYeJ736bXAo5bnss1eBCkUCi2O47wIfAls9zzv6NUYUE6kUNciPFTd5EV6hmzoS6XS1DAMX7BTDTDwQdd19yUSiV9rGTGooVd6hIFM1NxV/3PJJ8zx48dvCMPQbpoZqjqt958Tpz4+tfMeRH4Ezas9aES9cgOg5SZg1DU/YaotLBaLTXtO733y2F89o86/lcpXpBneC9IL2ovw9ZU+2v4DnerYhytiiykAAAAASUVORK5CYII=");
MemoryStream ms = new MemoryStream(arr);
XImage im = XImage.FromStream(ms);
gfx.DrawImage(im, 130, 155 - im.Height * 0.7 / 2, im.Width *0.7, im.Height*0.7);
XPen pen2 = new XPen(XColors.LightGray, 2);
gfx.DrawArc(pen2, 300, 200, 20, 20, 0, 360);
pen2.Color = XColors.Red;
gfx.DrawArc(pen2, 300, 200, 20, 20, -90, 180);
XFont font2 = new XFont(pfcFonts.Families[0], 6, XFontStyle.Regular, options);
gfx.DrawString("50%", font2, XBrushes.Black, new XRect(300, 200, 20, 20), XStringFormats.Center);
document.Save("d:\\test.pdf");
Console.WriteLine("over");
Console.Read();
}
///<summary>
/// count个随机数之和为定值sum,并返回count个数据的数值的集合
///</summary>
///<param name="sum">随机数之和</param>
///<param name="count">count个随机数</param>
///<returns>count个随机数的数值集合</returns>
static List<int> GetResult(int sum, int count)
{
int[] array = Enumerable.Range(0, sum).ToArray();
List<int> result = new List<int>(count);
int remain;
while (true)
{
remain = sum;
while (result.Count < count - 1)
{
int avg = (remain / (count - result.Count));
//根据鸽笼原理,必须有一个值在范围
//avg + Environment.TickCount % (remain - avg)之内
int value = array[avg + Environment.TickCount % (remain - avg)] / 2;
result.Add(value);
remain -= value;
}
result.Add(sum - result.Sum());
//如果都不相同,表明取值成功,否则重新取值
if (result.Distinct().Count() == result.Count)
{
break;
}
else
{
result.Clear();
}
}
return result;
}
}
网友评论