PDFSharp组件是.Net下的一个开源类库,可以轻松的在.Net语言中创建PDF文档,在屏幕中显示以及输出到打印机,可以修改,合并拆分已经存在的PDF文件。

总体来说,PDFSharp 组件主要特点有:

1.可以使用任何.NET编程语言动态创建PDF文档
2.很容易使用对象模型来构建文档
3.全部用C#重写设计和编写代码
4.可以生成PDF文件和显示在窗体或者打印,都使用同一源文件
5.可以修改、合并或者分割PDF文件
6.可以控制图片的透明度,嵌入了字体

官方下载地址:

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

当你有需要通过程序动态生成PDF,或者在.Net平台对现有的PDF进行操作,比如修改,显示,输出打印等时,PDFSharp组件能够发挥很大作用。

PDFSharp组件拥有很多对PDF文档操作的功能,有兴趣可以在官方给出的samples中逐一根据demo进行研究,文章接下来不会详细介绍PDFSharp的具体每个功能特点,但我会写下一些我个人在学习中觉得需要注意的几点以及在最后会给出一个画表格的例子(仅供参考)

一:中文乱码问题

#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和Offset

     在画字体,或者画形状图片等的时候,所画的东西都是在一个XRect范围内,

 

其实它只是一个矩形,用来装载我们画的东西。这样说比较抽象,我们举个例子。

打开电脑里的画图软件。如右图所示,比如现在想在画板上写几个字,你首先得

画一个矩形,然后在矩形内输入数据。此时此刻这个矩形其实就是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类库还有提供更多很强大的功能,这里只是简单介绍了其中的一小部分,有兴趣的同志可以在官网下载源代码继续了解。本文点到为止。

 

 

 

           

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2016-04-29 11:52  巴夫巴夫  阅读(2681)  评论(0编辑  收藏  举报