PDFsharp开源类库

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:[email protected])
//
// 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类库还有提供更多很强大的功能,这里只是简单介绍了其中的一小部分,有兴趣的同志可以在官网下载源代码继续了解。本文点到为止。

时间: 2024-08-24 09:58:26

PDFsharp开源类库的相关文章

C#常用开源类库

一.AOP框架        Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部署方面(aspects)的方法帮助缺少经验的开发人员提高开发效率. NKalore是一款编程语言,它扩展了C#允许在.net平台使用AOP.NKalore的语法简单.直观,它的编译器是基于Mono C#编译器(MCS).NKalore目前只能在命令行或#Develop内部使用.NKalore兼容公

[转]C#常用开源类库收集

.net PDF 类库 PDFsharp PDFsharp是一款可以让.NET框架支持的任何语言很容易的创建PDF文件的类库. ASP.NET FO PDF FO PDF 是一款C#编写类似于ASP.NET服务器控件的控件.它接受DataTable 和一些其它参数来创建XSL FO,并使用NFOP (Apache FOP Port in J#) PDF Formatter来绘制一个类似PDF Report 的DataGrid .今后将会增加更多的标签来可以生成XSL FO . Report.NE

使用CocoaPods管理第三方开源类库

iOS开发中经常会用到许多第三方类库,比如AFNetworking.FMDB.JSONKit等等,使用CocoaPods这个工具就能很方便得对工程中用到的类库进行管理,包括自动下载配置以及更新. 首先需要下载安装CocoaPods,系统要求有ruby环境,至少OS X 10.9以后是自带ruby环境的(Python也自带了),使用"ruby -v"命令可以查看当前ruby版本. 配置过程如下: 1.ruby环境 不放心的话可以先确认下ruby环境,据说由于GFW的影响,大都把Ruby镜

Json序列化之.NET开源类库Newtonsoft.Json的研究

一.Json简介                                                                                                                    JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在各种网络.平台和程序之间传输.JSON的语法很简单,

IOS 一些开源类库

因 为iOS SDK相对比较底层,所以开发者就得受累多做一些体力活.不过幸运的是,有很多第三方的类库可以用来简化很多不必要的工作.笔者整理了一下在本人学习过程 中用到的一些比较有用Objective-C开源类库,既是做一个总结,同时也希望通过这些分享,能提高各位的开发效率. KissXml——xml解析库 相关教程: github:https://github.com/Threew/KissXML http://sencho.blog.163.com/blog/static/8305622820

iCarousel——在iOS和Mac OS应用中实现3D CoverFlow旋转木马效果的开源类库

前言 iCarousel一个简单.可高度定制的3D CoverFlow开源类库,旨在简化在 iPhone, iPad和Mac OS中生成各种类型的cover flow(视图切换)效果(分页.滚动视图).用户手指划动图片,图片将不断以3D的形式切换. Github托管地址:https://github.com/nicklockwood/iCarousel 说明 iOS开发中如果想要你实现滑动效果,可以使用icarousel这个第三方库,还比较好用,感觉用起来有点像tableView.在网上下载这个

Atitit java onvif 开源类库 getProfiles&#160;getStreamUri

Atitit java onvif 开源类库 getProfiles getStreamUri 1. ONVIF Java Library by Milgo1 1.1. https://github.com/milg0/onvif-java-lib4 1.2. getProfiles  respones file4 1.3. getStreamUri:rtsp://192.168.31.144:10554/tcp/av0_04 1.4. Code---5 1. ONVIF Java Librar

IM开发之Socket通信开源类库CocoaAsyncSocket

CocoaAsyncSocket:https://github.com/robbiehanson/CocoaAsyncSocket CocoaAsyncSocket是用的最广泛的socket开发库,在iOS开发中使用socket,一般都是用第三方库CocoaAsyncSocket 打开连接看了下作者的开源类库,满满的崇拜感!!! 相关教程:https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro_GCDAsyncSocket http

Android之开源类库Pinyin4j的使用----搜索联系人

Pinyin4j只能算是Java开源类库,但是在Android的应用开发中也经常被用到.大家都用过手机的通讯录,通讯录的搜索联系人的功能,用pinyin4j就可以实现.下面我给大家带来这个例子,使用pinyin4j实现搜索联系人功能. 首先先对pinyin4j做一个简单的介绍: pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,拼音的输出格式也可以定制,并且支持汉字的多音字.pinyin4j的官方网是:http://pinyin4j.sourceforge.net/ piny