PDFsharp開源類庫

 

PDFSharp組件是.Net下的一個開源類庫,能夠輕鬆的在.Net語言中建立PDF文檔,在屏幕中顯示以及輸出到打印機,能夠修改,合併拆分已經存在的PDF文件。編程

整體來講,PDFSharp 組件主要特色有:安全

1.可使用任何.NET編程語言動態建立PDF文檔
2.很容易使用對象模型來構建文檔
3.所有用C#重寫設計和編寫代碼
4.能夠生成PDF文件和顯示在窗體或者打印,都使用同一源文件
5.能夠修改、合併或者分割PDF文件
6.能夠控制圖片的透明度,嵌入了字體編程語言

官方下載地址:ide

https:pdfsharp.codeplex.com/ 或者 https://sourceforge.net/projects/pdfsharp/佈局

當你有須要經過程序動態生成PDF,或者在.Net平臺對現有的PDF進行操做,好比修改,顯示,輸出打印等時,PDFSharp組件可以發揮很大做用。學習

PDFSharp組件擁有不少對PDF文檔操做的功能,有興趣能夠在官方給出的samples中逐一根據demo進行研究,文章接下來不會詳細介紹PDFSharp的具體每一個功能特色,但我會寫下一些我我的在學習中以爲須要注意的幾點以及在最後會給出一個畫表格的例子(僅供參考)字體

一:中文亂碼問題this

#region PDFsharp - A .NET library for processing PDF
//
// Authors:
//   PDFsharp Team (mailto:PDFsharpSupport@pdfsharp.de)
//
// Copyright (c) 2005-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace HelloWorld
{
  /// <summary>
  /// This sample is the obligatory Hello World program.
  /// 演示新建PDF並保存在指定路徑的helloworld PDF文件
  /// </summary>
  class Program
  {
    static void Main()
    {
      // Create a new PDF document
      PdfDocument document = new PdfDocument();
      document.Info.Title = "Created with PDFsharp";

      // Create an empty page
      PdfPage page = document.AddPage();

      // Get an XGraphics object for drawing
      XGraphics gfx = XGraphics.FromPdfPage(page);

      //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

      // Create a font
      //XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic);
      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);
      //PdfWriter.Wirte()裏的斷言註釋掉

      // Draw the text
      gfx.DrawString("你好, 世界!", font, XBrushes.Black,
        new XRect(0, 0, page.Width, page.Height),
        XStringFormats.Center);

      // Save the document...
      //const string filename = "PDF\\HelloWorld_tempfile2.pdf";
      string _path = YZRHelper.RPath.GetPath(System.AppDomain.CurrentDomain.BaseDirectory,2)+"\\PDF\\YZR.pdf";
      document.Save(_path);
      // ...and start a viewer.
      Process.Start(_path);
    }
  }
}
顯示中文

二:XRect之Inflate和Offsetspa

     在畫字體,或者畫形狀圖片等的時候,所畫的東西都是在一個XRect範圍內,.net

 

其實它只是一個矩形,用來裝載咱們畫的東西。這樣說比較抽象,咱們舉個例子。

打開電腦裏的畫圖軟件。如右圖所示,好比如今想在畫板上寫幾個字,你首先得

畫一個矩形,而後在矩形內輸入數據。此時此刻這個矩形其實就是XRect

     XRect類有一個Inflate方法,中文的意思是膨脹,有兩個重載方法:

     Inflate(XSize size);

     Inflate(double width,double height);

   以第二個重載來說,width是水平方向的,height是垂直方向的,當width和height的值

爲正數時是冷縮,爲負數是膨脹。

  怎麼理解呢?先看一下垂直方向的膨脹和冷縮。

  ===>rect.Inflate(0,-100);這句代碼產生的做用以下圖所示:

  

-100將會讓XRect對象垂直膨脹,上圖所示的黑色矩形變造成紅色矩形,而做爲參照物的字體顯示在PDF中的位置是相對於紅色矩形即變形以後的紅色XRect而言的。這樣可讓字體畫在指定距離頁面頂部的位置。相反,若是height的值是正數則會讓矩形發生冷縮變形,從上圖的參照物中,會讓參照物不會顯示在PDF中或者只是顯示一半,這要看你指定的height的大小。

  ===>rect.Inflate(100,0);也是同理,不同的是發生水平方向的膨脹:

 

 

  XRect類Offset方法,中文的意思是偏移,有兩個重載方法:

  Offset(XVector offsetVector);

  Offset(double offsetX, double offsetY);

  以第二個重載來講,矩形對象偏移xy軸關係=>  正數:x軸往左偏移 y軸往下偏移  負數:x軸往右偏移 y軸往上偏移

  ==>rect.Offset(0, 5);                                                  

  水平方向也是同理,往左或右進行偏移。

 

三:XStringAlignment枚舉

            XStringFormat format = new XStringFormat();
            //指定相對於佈局矩形的文本字符串的對齊方式
            
            //水平方向的遠近
            format.Alignment = XStringAlignment.Near;
            //垂直方向的遠近
            format.LineAlignment = XLineAlignment.Far;

 

好比添加頁腳信息時,format.LineAlignment = XLineAlignment.Far;可讓頁腳文字顯示在頁面底部。

四:畫表格demo

下面這個表格只是做用簡單的展現,僅供學習。

  在實際開發中,能夠將數據填充到上面的表格中,而後預覽再發送到打印機進行打印。預覽和打印的功能能夠參考官方samples。

  上面表格的代碼:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPrintConsole
{
    using PdfSharp.Drawing;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    using System.Diagnostics;
    class Program
    {
        
        static void Main(string[] args)
        {
            string filename = String.Format("YZR.pdf");
            s_document = new PdfDocument();
            s_document.Info.Title = "PDFsharp XTable Sample";
            s_document.Info.Author = "Yang ZhiRan";
            s_document.Info.Subject = "Output XTable";
            s_document.Info.Keywords = "XTable,PDFsharp";

            new XTable().DrawPage(s_document.AddPage());
            s_document.Save(filename);
            Process.Start(filename);
        }
        internal static PdfDocument s_document;
    }
}
Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace RPrintConsole
{
    using PdfSharp.Pdf;
    using PdfSharp.Drawing;
    using System.Drawing;
    /// <summary>
    /// Create By YZR 2016-04-26
    /// </summary>
    public class XTable
    {

        public void DrawPage(PdfPage page)
        {
            XGraphics gfx = XGraphics.FromPdfPage(page);
            //往page繪製一個標題
            //DrawTitle(page, gfx, "XTable(HuaGongBanner)");

            //BeginBox(gfx, 1, "DrawRectangle");

            XPen pen = new XPen(XColors.Black, 0.8);
            int marginLeft = 40;
            int marginTop = 10;
            XRect rect = new XRect(marginLeft, marginTop, page.Width - 2 * marginLeft, page.Height - 2 * marginTop);
            rect.Height = rect.Height - 150;
            gfx.DrawRectangle(pen, rect);
            rect.Height = rect.Height + 150;
            //標題
            //支持中文
            System.Drawing.Text.PrivateFontCollection pfcFonts = new System.Drawing.Text.PrivateFontCollection();
            string strFontPath = @"C:/Windows/Fonts/simfang.ttf";//字體設置爲微軟雅黑
            pfcFonts.AddFontFile(strFontPath);
            XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
            XFont font = new XFont(pfcFonts.Families[0], 20, XFontStyle.Regular, options);
            XFont exception = new XFont(pfcFonts.Families[0], 10, XFontStyle.Regular, options);//小一點的字體

            rect.Inflate(0, -10);
            //XFont font = new XFont("Verdana", 12, XFontStyle.Regular);
            gfx.DrawString("機動車查驗記錄表", font, XBrushes.Red, rect, XStringFormats.TopCenter);

            //業務類型
            rect.Location = new XPoint(marginLeft, marginTop + 40);
            XFont subfont = new XFont(pfcFonts.Families[0], 12, XFontStyle.Regular, options);
            gfx.DrawString("車牌號碼(車牌號或與其餘車輛能對應的號碼):", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);
            rect.Height = rect.Height - 120-130;
            rect.Y = rect.Y + 15;
            gfx.DrawRectangle(pen, rect);
            rect.Inflate(-5, -30);
            gfx.DrawString("業務類型:", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);
            rect.Inflate(5, 30);
            rect.Y = rect.Y + 7;
            gfx.DrawString("           √註冊登記 □變動車身顏色 □監督解體 □加裝/拆除操縱輔助裝置 ", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect.Y = rect.Y + 17;
            gfx.DrawString("           □移出登記 □變動使用性質 □更換髮動機 □更換車身或車架 ", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);
            rect.Y = rect.Y + 17;
            gfx.DrawString("           □變動遷出 □申領登記證書 □從新打刻VIN □轉入 ", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);
            rect.Y = rect.Y + 17;
            gfx.DrawString("           □更換整車 □補領登記證書 □從新打刻發動機號 □其餘 ", subfont, XBrushes.Black, rect, XStringFormats.TopLeft);
            rect.Y = rect.Y - 58;
            //通用項目
            rect.Height = rect.Height - 496;
            rect.Y = rect.Y + 85;
            //gfx.DrawRectangle(pen, rect);
            gfx.DrawLine(pen, rect.X, rect.Y, rect.Width + 40, rect.Y);
            //gfx.DrawRectangle(pen, rect.X, rect.Y + 80, page.Width - 2 * marginLeft, rect.Height - 500);
            //畫一根分隔標題的橫線
            gfx.DrawLine(pen, rect.X, rect.Y + 25, rect.Width + 40, rect.Y + 25);
            //7個豎線
            gfx.DrawLine(pen, rect.X + 35, rect.Y, rect.X + 35, 328 + 7 + 98);
            gfx.DrawLine(pen, rect.X + 65, rect.Y, rect.X + 65, 328 + 7 + 98);
            gfx.DrawLine(pen, rect.X + 228, rect.Y, rect.X + 228, 328 + 7 + 98);
            gfx.DrawLine(pen, rect.X + 258, rect.Y, rect.X + 258, 328 + 7 + 98);
            gfx.DrawLine(pen, rect.X + 293, rect.Y, rect.X + 293, 277 + 7);
            gfx.DrawLine(pen, rect.X + 323, rect.Y, rect.X + 323, 277 + 7);
            gfx.DrawLine(pen, rect.X + 486, rect.Y, rect.X + 486, 277 + 7);

            gfx.DrawLine(pen, rect.X + 230 + 30 + 63, rect.Y + 60 + 182, rect.X + 230 + 30 + 63, rect.Y + 60 + 405);

            gfx.DrawString("類別", subfont, XBrushes.Black, new XRect(rect.X + 3, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("序號", subfont, XBrushes.Black, new XRect(rect.X + 38, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("查驗項目", subfont, XBrushes.Black, new XRect(rect.X + 123, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("斷定", subfont, XBrushes.Black, new XRect(rect.X + 231, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("類別", subfont, XBrushes.Black, new XRect(rect.X + 263, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("序號", subfont, XBrushes.Black, new XRect(rect.X + 295, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("查驗項目", subfont, XBrushes.Black, new XRect(rect.X + 378, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("斷定", subfont, XBrushes.Black, new XRect(rect.X + 488, rect.Y + 4, rect.Width, rect.Height), XStringFormats.TopLeft);

            //八根橫線
            rect.Y = rect.Y + 25;
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 17, rect.X + 65 + 192, rect.Y + 17);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 37, rect.X + 65 + 192, rect.Y + 37);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 55, rect.Width + 40, rect.Y + 55);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 73, rect.X + 65 + 192, rect.Y + 73);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 91, rect.X + 65 + 192, rect.Y + 91);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 109, rect.Width + 40, rect.Y + 109);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 127, rect.X + 65 + 192, rect.Y + 127);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 145, rect.X + 65 + 192, rect.Y + 145);

            gfx.DrawLine(pen, rect.X + 65 + 192 + 36, rect.Y + 17, rect.Width + 40, rect.Y + 17);
            gfx.DrawLine(pen, rect.X + 65 + 192 + 36, rect.Y + 37, rect.Width + 40, rect.Y + 37);
            gfx.DrawLine(pen, rect.X + 65 + 192 + 36, rect.Y + 73, rect.Width + 40, rect.Y + 73);
            gfx.DrawLine(pen, rect.X + 65 + 192 + 36, rect.Y + 91, rect.Width + 40, rect.Y + 91);

            gfx.DrawString("通用", subfont, XBrushes.Black, new XRect(rect.X + 3, rect.Y + 65, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("項目", subfont, XBrushes.Black, new XRect(rect.X + 3, rect.Y + 80, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("1", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("2", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 20, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("3", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 40, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("4", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 58, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("5", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 76, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("6", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 94, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("7", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 112, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("8", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 130, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("9", subfont, XBrushes.Black, new XRect(rect.X + 45, rect.Y + 148, rect.Width, rect.Height), XStringFormats.TopLeft);


            gfx.DrawString("      車輛識別代號", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("    發動機型號/號碼", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 20, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("     車輛品牌/型號", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 40, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("       車身顏色", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 58, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("      覈定載人數", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 76, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("       車輛類型", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 94, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("   號牌/車輛外觀形狀", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 112, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("     輪胎無缺狀況", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 130, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("  安全帶丶三角警告牌", subfont, XBrushes.Black, new XRect(rect.X + 45 + 30, rect.Y + 148, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("大中型", exception, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("客車丶", exception, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 11, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("危險化", exception, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 22, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("學品運", exception, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 33, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("輸車", exception, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 44, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("其餘", subfont, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 74, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("查驗結論:", subfont, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 114, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("15", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("16", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y + 20, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("17", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y + 40, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("18", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y + 58, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("19", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y + 75, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("20", subfont, XBrushes.Black, new XRect(rect.X + 300, rect.Y + 93, rect.Width, rect.Height), XStringFormats.TopLeft);


            gfx.DrawString("        滅火器", subfont, XBrushes.Black, new XRect(rect.X + 300 + 30, rect.Y, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("行駛記錄裝置丶車內外監控錄像裝置", exception, XBrushes.Black, new XRect(rect.X + 300 + 25, rect.Y + 20, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("安全出口/安全手錘丶乘客門", subfont, XBrushes.Black, new XRect(rect.X + 300 + 30, rect.Y + 40, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("   外部標識/文字丶噴塗", subfont, XBrushes.Black, new XRect(rect.X + 300 + 30, rect.Y + 58, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("    標識燈具丶警報器", subfont, XBrushes.Black, new XRect(rect.X + 300 + 30, rect.Y + 75, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("      檢驗合格證實", subfont, XBrushes.Black, new XRect(rect.X + 300 + 30, rect.Y + 93, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("貨車", subfont, XBrushes.Black, new XRect(rect.X + 3, rect.Y + 200, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("掛車", subfont, XBrushes.Black, new XRect(rect.X + 3, rect.Y + 215, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("10", subfont, XBrushes.Black, new XRect(rect.X + 43, rect.Y + 162, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("11", subfont, XBrushes.Black, new XRect(rect.X + 43, rect.Y + 182, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("12", subfont, XBrushes.Black, new XRect(rect.X + 43, rect.Y + 202, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("13", subfont, XBrushes.Black, new XRect(rect.X + 43, rect.Y + 222, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("14", subfont, XBrushes.Black, new XRect(rect.X + 43, rect.Y + 242, rect.Width, rect.Height), XStringFormats.TopLeft);
            //五根橫線
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 178, rect.X + 65 + 192, rect.Y + 178);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 198, rect.X + 65 + 192, rect.Y + 198);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 218, rect.Width + 40, rect.Y + 218);
            gfx.DrawLine(pen, rect.X + 35, rect.Y + 238, rect.X + 65 + 192, rect.Y + 238);
            gfx.DrawLine(pen, rect.X, rect.Y + 258, rect.Width + 40, rect.Y + 258);

            gfx.DrawString("     外廓尺寸丶軸數", subfont, XBrushes.Black, new XRect(rect.X + 43 + 30, rect.Y + 162, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("        整備質量", subfont, XBrushes.Black, new XRect(rect.X + 43 + 30, rect.Y + 182, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("        輪胎規格", subfont, XBrushes.Black, new XRect(rect.X + 43 + 30, rect.Y + 202, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("     側後部防禦裝置", subfont, XBrushes.Black, new XRect(rect.X + 43 + 30, rect.Y + 222, rect.Width, rect.Height), XStringFormats.TopLeft);
            gfx.DrawString("車身反光標識和車輛尾部標誌版丶噴塗", exception, XBrushes.Black, new XRect(rect.X + 43 + 20, rect.Y + 242, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("查驗員:", subfont, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 162, rect.Width, rect.Height), XStringFormats.TopLeft);

            gfx.DrawString("複檢合格:", subfont, XBrushes.Black, new XRect(rect.X + 230 + 30, rect.Y + 222, rect.Width, rect.Height), XStringFormats.TopLeft);
        }
    }
}
XTable

 

最後說幾句,在實際開發中,你能夠根據本身的業務將pdf的操做再封裝一層,或者建立一個適合業務的模板,經過配置由代碼來動態操做,之後不須要更改源代碼就能夠打印出適合業務的表單表格。在上面的例子中只是做爲演示畫一個表格,不具備實際開發意義。另外,畫出一樣的效果圖可使用多種不同的方法,這些靠創做者的想法,而我這裏只是展現了最笨的一種一條條畫。dpfsharp類庫還有提供更多很強大的功能,這裏只是簡單介紹了其中的一小部分,有興趣的同志能夠在官網下載源代碼繼續瞭解。本文點到爲止。

相關文章
相關標籤/搜索