【问题标题】:Some doubts about how it work this iTextSharp application?关于这个 iTextSharp 应用程序如何工作的一些疑问?
【发布时间】:2014-05-16 23:57:29
【问题描述】:

我是 C# 的新手,这是我第一次使用 iTextSharp(用于 C# 的 iText 移植),我对如何工作有以下疑问我从 0 重新制作的旧应用程序。

在此应用程序中,有一个控制器类包含一个方法,该方法将 PDF 文件生成为具有以下签名的方法:private byte[] GetPdf(int id, bool withcmets)

在这个方法中我可以找到这样的东西:

private byte[] GetPdf(int id, bool withcomments)
{

byte[] buffer = null;

using (Document document = new Document(PageSize.A4, 10, 10, 100, 50))
{
    using (MemoryStream ms = new MemoryStream())
    {
    PdfWriter writer = PdfWriter.GetInstance(document, ms);

    document.Open();

    Font arialtitle = FontFactory.GetFont("Arial", 14);
    Font arialtext = FontFactory.GetFont("Arial", 9);

    iTextSharp.text.Font title = new iTextSharp.text.Font(arialtitle);
    title.SetStyle(iTextSharp.text.Font.BOLD);

    iTextSharp.text.Font bold = new iTextSharp.text.Font(arialtext);
    title.SetStyle(iTextSharp.text.Font.BOLD);

    iTextSharp.text.Font text = new iTextSharp.text.Font(arialtext);
    title.SetStyle(iTextSharp.text.Font.NORMAL);

    PdfPTable table = null;
    PdfPCell cell = null;

    Image img = null;
    string filename = null;

    table = new PdfPTable(1);
    table.WidthPercentage = 98;
    cell = new PdfPCell(new Paragraph(a.Title, title)) { FixedHeight = 70 };
    cell.Border = Rectangle.BOTTOM_BORDER;
    table.AddCell(cell);
    document.Add(table);

    table = new PdfPTable(2);
    table.SetWidths(new int[] { 100, 500 });
    table.WidthPercentage = 98;
    table.AddCell(new PdfPCell(new Phrase("Description:", bold)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
    table.AddCell(new PdfPCell(new Phrase(a.ShortSummary, text)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
     document.Add(table);

     .........................................................
     .........................................................
     .........................................................

     return buffer;
}

我有以下疑惑:

1) using 语句究竟做了什么?为什么在前面的代码中,iTextSharp Document 对象被创建到 using 语句中?与经典创作成代码有什么区别?

2) 正如您在前面的代码中看到的,有一些图形设置放入 {...} 块中。

例如这样的:

table.AddCell(new PdfPCell(new Phrase("Description:", bold)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });

其中 PdfCell 图形设置设置到 {SETTINGS} 块设置 BorderPaddingMinimumHeight 的值PaddingTop 属性(属于名为 Rectangle

的类的对象实例

我无法理解这些设置对象是否属于 iTextSharp 框架,或者 Rectangle 类是否是由应用程序开发人员创建的自定义类我必须重建。

我有这个疑问,因为我知道,在 iTextSharp 中,如果我想设置一个填充值,我可以简单地执行以下操作:cell.PaddingBottom = 10f; 而不使用 Padding 对象

为了让您更好地了解情况,这是我的旧项目中出现的 Rectangle 类内容:

#region Assembly itextsharp.dll, v5.3.4.0
// C:\Develop\EarlyWarning\public\Implementazione\Ver1\ExternalAssemblies\itextsharp.dll
#endregion

using System;
using System.Collections.Generic;

namespace iTextSharp.text
{
    public class Rectangle : Element, IElement
    {
        public const int BOTTOM_BORDER = 2;
        public const int BOX = 15;
        public const int LEFT_BORDER = 4;
        public const int NO_BORDER = 0;
        public const int RIGHT_BORDER = 8;
        public const int TOP_BORDER = 1;
        public const int UNDEFINED = -1;

        protected BaseColor backgroundColor;
        protected int border;
        protected BaseColor borderColor;
        protected BaseColor borderColorBottom;
        .................................................
        .................................................
        .................................................
     }
}

【问题讨论】:

    标签: c# .net pdf-generation itextsharp itext


    【解决方案1】:

    using 语句确保正在使用的实例将始终被丢弃。 using 语句还有其他好处。您经常会看到 using 语句与系统资源(如文件句柄和内存分配)结合使用。这可确保在函数失去作用域时释放句柄。请参阅此参考here 了解更多信息。

    这是一个封装类,用于封装 itextsharp.dll 库中的功能。这段代码的开发者重用了一个现有的类来封装存储/检索数据来配置 pdf 单元格属性。这样做的原因有很多,例如可以从外部配置存储库存储和读取单元配置,该存储库使用相同的类来获取和设置此文档的单元属性,但是可能有点矫枉过正。

    我敢打赌,创建该类是为了使包装器功能更具可扩展性。我可以设想一个类public class PdfMargin 来封装文档边缘的配置。在那里,您可以在构造函数中定义一次边距,然后在代码中的其他地方简单地引用边距类。

    【讨论】:

      【解决方案2】:

      使用关键字

      using 关键字用于确保正确处理实现IDisposable 的类实例。 IDisposable 模式用于管理非托管资源(文件句柄、与数据库的连接、内存流......)的释放。

      更多信息请访问msdn

      一般规则是,如果您使用实现 IDisposable 的类,则必须(或者至少是强烈推荐)通过 using 关键字模式使用它。

      iTextSharp

      您引用的设置对象属于 iTextSharp。例如,Rectangle 类在 iTextSharp 中定义,位于 iTextSharp.text 命名空间中。

      PdfPCell 也属于 iTextSharp。

      您可以看到Rectangle 类实现的唯一原因是,iTextSharp 在其 DLL 的可执行版本旁边提供了源代码和符号,以便您能够查看其代码。但是,这不是您可以修改或您拥有的代码。您只是在引用 iTextSharp 程序集。

      【讨论】:

      • 好的,但现在我的疑问是:要使用 Rectangle 类和 PdfCell 类,我首先必须构建它,但在这里我看不到对这些类的构造函数的调用......为什么?
      • 我可以在您的代码中看到对 PdfPCell 类 (new PdfPCell()) 的调用。对于 Rectangle 类,您实际上并没有使用该类的 instance,而只是访问它定义的常量之一。要使用类中定义的常量,您无需实例化它。
      • @yorah,如果您想详细说明 OP,new XYZ() {A = B} 语法是 C# 的对象初始化器语法,用于在构造的同时设置属性,而不是稍后一次设置。 msdn.microsoft.com/en-us/library/bb397680.aspx
      猜你喜欢
      • 2016-02-04
      • 2016-03-18
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      相关资源
      最近更新 更多