您好,登錄后才能下訂單哦!
在PDF中我們可以通過C#程序代碼來添加非常豐富的元素來呈現我們想要表達的內容,如繪制表格、文字,添加圖形、圖像等等。在本篇文章中,我將介紹如何在PDF中繪制圖形,并設置圖形屬性的操作。
文章中將分以下要點進行介紹:
提示:安裝后,直接引用安裝路徑下Bin文件夾中的dll文件到項目程序中即可。
步驟1:新建一個PDF文檔,添加頁,添加畫筆、畫刷
//新建一個PDF文檔,添加頁
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//設置畫筆和畫刷
PdfPen pen = new PdfPen(PdfBrushes.Black, 1f);
PdfBrush brush2 = PdfBrushes.RosyBrown;
PdfBrush brush3 = PdfBrushes.Goldenrod;
步驟2:繪制圓形、矩形、線段、三角形
//繪入矩形(此處通過設置值來繪制成正方形)
page.Canvas.DrawRectangle(pen, brush2, new Rectangle(new Point(50, 50), new Size(60, 60)));
//繪入橢圓(此處通過設置值來繪制成圓形)
page.Canvas.DrawEllipse(pen, brush3, 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);
步驟3:保存文檔
//保存并打開文檔
doc.SaveToFile("基本圖形.pdf");
System.Diagnostics.Process.Start("基本圖形.pdf");
圖形添加效果:
步驟1:創建pdf文檔,調用方法DrawStar()繪制自定義圖形,并保存
//新建一個PDF文檔,添加頁
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
//調用DrawStar()方法繪入五角星圖形
DrawStar(page);
//保存并打開文檔
doc.SaveToFile("自定義圖形.pdf");
System.Diagnostics.Process.Start("自定義圖形.pdf");
步驟2:自定義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();
//實例化畫筆和畫刷1、畫刷2
PdfPen pen = new PdfPen(Color.DeepSkyBlue, 0.02f);
PdfBrush brush2 = new PdfSolidBrush(Color.PaleGreen);
PdfBrush brush3 = new PdfSolidBrush(Color.Bisque);
//將坐標放大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, brush2, path);
//平移坐標并在新的位置繪入第三個五角星
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush3, path);
//實例化畫刷3,平移坐標并在新的位置繪入第四個五角星
PdfLinearGradientBrush brush4
= new PdfLinearGradientBrush(new PointF(-2, 0), new PointF(2, 0), Color.OrangeRed, Color.Yellow);
page.Canvas.TranslateTransform(-4f, 2f);
path.FillMode = PdfFillMode.Alternate;
page.Canvas.DrawPath(pen, brush4, path);
//實例化畫刷4,平移坐標并在新的位置繪入第五個五角星
PdfRadialGradientBrush brush5
= new PdfRadialGradientBrush(new PointF(0f, 0f), 0f, new PointF(0f, 0f), 1f, Color.Orchid, Color.LightBlue);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush5, path);
//實例化畫刷5,平移坐標并在新的位置繪入第六個五角星
PdfTilingBrush brush6 = new PdfTilingBrush(new RectangleF(0, 0, 4f, 4f));
brush6.Graphics.DrawRectangle(brush4, 0, 0, 4f, 4f);
page.Canvas.TranslateTransform(2f, 0f);
path.FillMode = PdfFillMode.Winding;
page.Canvas.DrawPath(pen, brush6, path);
//再次保存畫布狀態
page.Canvas.Restore(state);
}
自定義圖形添加效果:
步驟1:新建一個PDF文檔,添加頁
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
步驟2:初始化一個PdfSeparationColorSpace的對象,用于創建基本色,并將基本色透明度設置為1
PdfSeparationColorSpace cs1 = new PdfSeparationColorSpace("MySpotColor", Color.DarkGreen);
PdfSeparationColorSpace cs2 = new PdfSeparationColorSpace("MySpotColor", Color.Yellow);
PdfSeparationColorSpace cs3 = new PdfSeparationColorSpace("MySpotColor", Color.DeepPink);
PdfSeparationColor color1 = new PdfSeparationColor(cs1, 1f);
PdfSeparationColor color2 = new PdfSeparationColor(cs2, 1f);
PdfSeparationColor color3 = new PdfSeparationColor(cs3, 1f);
步驟3:根據顏色創建畫刷
PdfSolidBrush brush2 = new PdfSolidBrush(color1);
PdfSolidBrush brush3 = new PdfSolidBrush(color2);
PdfSolidBrush brush4 = new PdfSolidBrush(color3);
步驟4:繪入圖形及文字,應用色彩透明度到圖形
//繪入圖形及文字并著色
page.Canvas.DrawPie(brush2, 10, 30, 60, 60, 360, 360);
page.Canvas.DrawPie(brush3, 10, 120, 60, 60, 360, 360);
page.Canvas.DrawPie(brush4, 10, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=1.0", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(16, 100));
//將基本色透明度設置為0.5,并繪入圖片及文字
color1 = new PdfSeparationColor(cs1, 0.5f);
brush2 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush2, 80, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.5f);
brush3 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush3, 80, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.5f);
brush4 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush4, 80, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.5", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(86, 100));
//將基本色透明度設置為0.25,并繪入圖片及文字
color1 = new PdfSeparationColor(cs1, 0.25f);
brush2 = new PdfSolidBrush(color1);
page.Canvas.DrawPie(brush2, 150, 30, 60, 60, 360, 360);
color2 = new PdfSeparationColor(cs2, 0.25f);
brush3 = new PdfSolidBrush(color2);
page.Canvas.DrawPie(brush3, 150, 120, 60, 60, 360, 360);
color3 = new PdfSeparationColor(cs3, 0.25f);
brush4 = new PdfSolidBrush(color3);
page.Canvas.DrawPie(brush4, 150, 210, 60, 60, 360, 360);
page.Canvas.DrawString("透明度=0.25", new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true), brush2, new PointF(156, 100));
步驟5:保存文檔
doc.SaveToFile("設置透明度.pdf");
System.Diagnostics.Process.Start("設置透明度.pdf");
色彩透明度實現效果:
以上是關于C#繪制PDF圖形的全部內容,如需轉載,請注明出處!
(本文完)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。