spire.pdf支持在pdf文档中直接绘制线段、矩形、椭圆、扇形、多边形等基本图形,根据已有的方法程序员可以创建自定义函数绘制特殊图形。除此以外,spire.pdf还支持为同一色彩创建不同的透明度,让图形的着色更有层次感。
直接绘制基本图形
c#
//新建一个pdf文档
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
//设置画笔和画刷
pdfpen pen = new pdfpen(pdfbrushes.black, 1f);
pdfbrush brush = pdfbrushes.red;
//绘入矩形
page.canvas.drawrectangle(pen, brush, new rectangle(new point(50, 50), new size(60, 60)));
//绘入椭圆(此处为圆形)
page.canvas.drawellipse(pen, brush, 210, 50, 60, 60);
//绘入线段
page.canvas.drawline(pen, new pointf(50, 115), new pointf(270, 115));
//绘入多边形(此处为三角形)
pointf p1 = new pointf(130, 172);
pointf p2 = new pointf(160, 120);
pointf p3 = new pointf(190, 172);
pointf[] points = new pointf[] {p1,p2,p3 };
page.canvas.drawpolygon(pen, points);
//保存文档
doc.savetofile("基本图形.pdf");
vb.net
'新建一个pdf文档
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add()
'设置画笔和画刷
dim pen as new pdfpen(pdfbrushes.black, 1f)
dim brush as pdfbrush = pdfbrushes.red
'绘入矩形
page.canvas.drawrectangle(pen, brush, new rectangle(new point(50, 50), new size(60, 60)))
'绘入椭圆(此处为圆形)
page.canvas.drawellipse(pen, brush, 210, 50, 60, 60)
'绘入线段
page.canvas.drawline(pen, new pointf(50, 115), new pointf(270, 115))
'绘入多边形(此处为三角形)
dim p1 as new pointf(130, 172)
dim p2 as new pointf(160, 120)
dim p3 as new pointf(190, 172)
dim points as pointf() = new pointf() {p1, p2, p3}
page.canvas.drawpolygon(pen, points)
'保存文档
doc.savetofile("基本图形.pdf")
绘制自定义图形
c#
static void main(string[] args)
{
//新建一个pdf文档
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
//调用drawstar方法绘入五角星
drawstar(page);
//保存文档
doc.savetofile("自定义图形.pdf");
}
//自定义drawstar方法画几个不同样式的五角星
private static void drawstar(pdfpagebase page)
{
//设置五角星的5个顶点坐标
pointf[] points = new pointf[5];
for (int i = 0; i < points.length; i )
{
float x = (float)math.cos(i * 2 * math.pi / 5);
float y = (float)math.sin(i * 2 * math.pi / 5);
points[i] = new pointf(x, y);
}
//创建pdfpath类,在顶点之间添加线段组成五角星
pdfpath path = new pdfpath();
path.addline(points[2], points[0]);
path.addline(points[0], points[3]);
path.addline(points[3], points[1]);
path.addline(points[1], points[4]);
path.addline(points[4], points[2]);
//保存画布状态
pdfgraphicsstate state = page.canvas.save();
//设置画笔和画刷
pdfpen pen = new pdfpen(color.deepskyblue, 0.02f);
pdfbrush brush1 = new pdfsolidbrush(color.cadetblue);
//将坐标放大40倍
page.canvas.scaletransform(40f, 40f);
//平移坐标
page.canvas.translatetransform(1f, 1.5f);
//绘入五角星
page.canvas.drawpath(pen, path);
//平移坐标并在新的位置绘入五角星
page.canvas.translatetransform(2f, 0f);
path.fillmode = pdffillmode.alternate;
page.canvas.drawpath(pen, brush1, path);
//平移坐标并在新的位置绘入五角星
page.canvas.translatetransform(2f, 0f);
path.fillmode = pdffillmode.winding;
page.canvas.drawpath(pen, brush1, path);
//平移坐标并在新的位置绘入五角星
pdflineargradientbrush brush2
= new pdflineargradientbrush(new pointf(-2, 0), new pointf(2, 0), color.red, color.blue);
page.canvas.translatetransform(-4f, 2f);
path.fillmode = pdffillmode.alternate;
page.canvas.drawpath(pen, brush2, path);
//平移坐标并在新的位置绘入五角星
pdfradialgradientbrush brush3
= new pdfradialgradientbrush(new pointf(0f, 0f), 0f, new pointf(0f, 0f), 1f, color.red, color.blue);
page.canvas.translatetransform(2f, 0f);
path.fillmode = pdffillmode.winding;
page.canvas.drawpath(pen, brush3, path);
//平移坐标并在新的位置绘入五角星
pdftilingbrush brush4 = new pdftilingbrush(new rectanglef(0, 0, 4f, 4f));
brush4.graphics.drawrectangle(brush2, 0, 0, 4f, 4f);
page.canvas.translatetransform(2f, 0f);
path.fillmode = pdffillmode.winding;
page.canvas.drawpath(pen, brush4, path);
//再次保存画布状态
page.canvas.restore(state);
}
vb.net
private shared sub main(args as string())
'新建一个pdf文档
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add()
'调用drawstar方法绘入五角星
drawstar(page)
'保存文档
doc.savetofile("自定义图形.pdf")
end sub
'自定义drawstar方法画几个不同样式的五角星
private shared sub drawstar(page as pdfpagebase)
'设置五角星的5个顶点坐标
dim points as pointf() = new pointf(4) {}
for i as integer = 0 to points.length - 1
dim x as single = csng(math.cos(i * 2 * math.pi / 5))
dim y as single = csng(math.sin(i * 2 * math.pi / 5))
points(i) = new pointf(x, y)
next
'创建pdfpath类,在顶点之间添加线段组成五角星
dim path as new pdfpath()
path.addline(points(2), points(0))
path.addline(points(0), points(3))
path.addline(points(3), points(1))
path.addline(points(1), points(4))
path.addline(points(4), points(2))
'保存画布状态
dim state as pdfgraphicsstate = page.canvas.save()
'设置画笔和画刷
dim pen as new pdfpen(color.deepskyblue, 0.02f)
dim brush1 as pdfbrush = new pdfsolidbrush(color.cadetblue)
'将坐标放大40倍
page.canvas.scaletransform(40f, 40f)
'平移坐标
page.canvas.translatetransform(1f, 1.5f)
'绘入五角星
page.canvas.drawpath(pen, path)
'平移坐标并在新的位置绘入五角星
page.canvas.translatetransform(2f, 0f)
path.fillmode = pdffillmode.alternate
page.canvas.drawpath(pen, brush1, path)
'平移坐标并在新的位置绘入五角星
page.canvas.translatetransform(2f, 0f)
path.fillmode = pdffillmode.winding
page.canvas.drawpath(pen, brush1, path)
'平移坐标并在新的位置绘入五角星
dim brush2 as new pdflineargradientbrush(new pointf(-2, 0), new pointf(2, 0), color.red, color.blue)
page.canvas.translatetransform(-4f, 2f)
path.fillmode = pdffillmode.alternate
page.canvas.drawpath(pen, brush2, path)
'平移坐标并在新的位置绘入五角星
dim brush3 as new pdfradialgradientbrush(new pointf(0f, 0f), 0f, new pointf(0f, 0f), 1f, color.red, color.blue)
page.canvas.translatetransform(2f, 0f)
path.fillmode = pdffillmode.winding
page.canvas.drawpath(pen, brush3, path)
'平移坐标并在新的位置绘入五角星
dim brush4 as new pdftilingbrush(new rectanglef(0, 0, 4f, 4f))
brush4.graphics.drawrectangle(brush2, 0, 0, 4f, 4f)
page.canvas.translatetransform(2f, 0f)
path.fillmode = pdffillmode.winding
page.canvas.drawpath(pen, brush4, path)
'再次保存画布状态
page.canvas.restore(state)
end sub
创建不同透明度的颜色
c#
//新建一个pdf文档
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
//初始化一个pdfseparationcolorspace的对象,用于创建基本色
pdfseparationcolorspace cs = new pdfseparationcolorspace("myspotcolor", color.purple);
//将基本色透明度设置为1
pdfseparationcolor color = new pdfseparationcolor(cs, 1f);
//根据颜色创建画刷
pdfsolidbrush brush = new pdfsolidbrush(color);
//绘入图形及文字并着色
page.canvas.drawpie(brush, 10, 30, 60, 60, 360, 360);
page.canvas.drawstring("透明度=1.0", new pdftruetypefont(new font("arial unicode ms", 10f), true), brush, new pointf(16, 100));
//将基本色透明度设置为0.5,并绘入图片及文字
color = new pdfseparationcolor(cs, 0.5f);
brush = new pdfsolidbrush(color);
page.canvas.drawpie(brush, 80, 30, 60, 60, 360, 360);
page.canvas.drawstring("透明度=0.5", new pdftruetypefont(new font("arial unicode ms", 10f),true), brush, new pointf(86, 100));
//将基本色透明度设置为0.25,并绘入图片及文字
color = new pdfseparationcolor(cs, 0.25f);
brush = new pdfsolidbrush(color);
page.canvas.drawpie(brush, 150, 30, 60, 60, 360, 360);
page.canvas.drawstring("透明度=0.25", new pdftruetypefont(new font("arial unicode ms", 10f),true), brush, new pointf(156, 100));
//保存文档
doc.savetofile("设置透明度.pdf");
vb.net
'新建一个pdf文档
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add()
'初始化一个pdfseparationcolorspace的对象,用于创建基本色
dim cs as new pdfseparationcolorspace("myspotcolor", color.purple)
'将基本色透明度设置为1
dim color__1 as new pdfseparationcolor(cs, 1f)
'根据颜色创建画刷
dim brush as new pdfsolidbrush(color__1)
'绘入图形及文字并着色
page.canvas.drawpie(brush, 10, 30, 60, 60, 360, _
360)
page.canvas.drawstring("透明度=1.0", new pdftruetypefont(new font("arial unicode ms", 10f), true), brush, new pointf(16, 100))
'将基本色透明度设置为0.5,并绘入图片及文字
color__1 = new pdfseparationcolor(cs, 0.5f)
brush = new pdfsolidbrush(color__1)
page.canvas.drawpie(brush, 80, 30, 60, 60, 360, _
360)
page.canvas.drawstring("透明度=0.5", new pdftruetypefont(new font("arial unicode ms", 10f), true), brush, new pointf(86, 100))
'将基本色透明度设置为0.25,并绘入图片及文字
color__1 = new pdfseparationcolor(cs, 0.25f)
brush = new pdfsolidbrush(color__1)
page.canvas.drawpie(brush, 150, 30, 60, 60, 360, _
360)
page.canvas.drawstring("透明度=0.25", new pdftruetypefont(new font("arial unicode ms", 10f), true), brush, new pointf(156, 100))
'保存文档
doc.savetofile("设置透明度.pdf")