轉(zhuǎn)帖|使用教程|編輯:黃竹雯|2019-05-17 10:49:30.277|閱讀 725 次
概述:Spire.PDF支持在PDF文檔中直接繪制線段、矩形、橢圓、扇形、多邊形等基本圖形,根據(jù)已有的方法程序員可以創(chuàng)建自定義函數(shù)繪制特殊圖形。除此以外,Spire.PDF還支持為同一色彩創(chuàng)建不同的透明度,讓圖形的著色更有層次感。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
更多資源請查看:【Spire.PDF-文檔操作教程】 | 【Spire.Doc教程】 | 【Spire.XLS教程】
Spire.PDF是一個(gè)專業(yè)的PDF組件,能夠獨(dú)立地創(chuàng)建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。
Spire.PDF支持在PDF文檔中直接繪制線段、矩形、橢圓、扇形、多邊形等基本圖形,根據(jù)已有的方法程序員可以創(chuàng)建自定義函數(shù)繪制特殊圖形。除此以外,Spire.PDF還支持為同一色彩創(chuàng)建不同的透明度,讓圖形的著色更有層次感。
直接繪制基本圖形
//新建一個(gè)PDF文檔
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//設(shè)置畫筆和畫刷
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");效果圖:

繪制自定義圖形
static void Main(string[] args)
{
//新建一個(gè)PDF文檔
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//調(diào)用DrawStar方法繪入五角星
DrawStar(page);
//保存文檔
doc.SaveToFile("自定義圖形.pdf");
}
//自定義DrawStar方法畫幾個(gè)不同樣式的五角星
private static void DrawStar(PdfPageBase page)
{
//設(shè)置五角星的5個(gè)頂點(diǎn)坐標(biāo)
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);
}
//創(chuàng)建PdfPath類,在頂點(diǎn)之間添加線段組成五角星
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]);
//保存畫布狀態(tài)
PdfGraphicsState state = page.Canvas.Save();
//設(shè)置畫筆和畫刷
PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
PdfBrush brush1 = new PdfSolidBrush(Color.CadetBlue);
//將坐標(biāo)放大40倍
page.Canvas.ScaleTransform(40f, 40f);
//平移坐標(biāo)
page.Canvas.TranslateTransform(1f, 1.5f);
//繪入五角星
page.Canvas.DrawPath(pen, path);
//平移坐標(biāo)并在新的位置繪入五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Alternate;
page.Canvas.DrawPath(pen, brush1, path);
//平移坐標(biāo)并在新的位置繪入五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush1, path);
//平移坐標(biāo)并在新的位置繪入五角星
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);
//平移坐標(biāo)并在新的位置繪入五角星
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);
//平移坐標(biāo)并在新的位置繪入五角星
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);
//再次保存畫布狀態(tài)
page.Canvas.Restore(state);
}效果圖:

創(chuàng)建不同透明度的顏色
//新建一個(gè)PDF文檔
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//初始化一個(gè)PdfSeparationColorSpace的對象,用于創(chuàng)建基本色
PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor", Color.Purple);
//將基本色透明度設(shè)置為1
PdfSeparationColor color = new PdfSeparationColor(cs, 1f);
//根據(jù)顏色創(chuàng)建畫刷
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));
//將基本色透明度設(shè)置為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));
//將基本色透明度設(shè)置為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("設(shè)置透明度.pdf");效果圖:

想要購買正版授權(quán),或者獲取更多Spire.PDF相關(guān)信息的朋友可以點(diǎn)擊" "~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn