我是C#的新手,这是我第一次使用 iTextSharp ( iText 移植C#),我对如何工作有以下疑问我从0重新制作的旧应用程序。
在此应用程序中,有一个控制器类,其中包含一个方法,该方法可将PDF文件生成为具有以下签名的方法: private byte [] GetPdf(int id,bool withcomments)
在这种方法中我可以找到这样的东西:
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)使用语句究竟是什么?为什么在前面的代码中,iTextSharp 文档对象被创建到使用语句中?从经典创作到代码有什么区别?
2)正如您在上面的代码中看到的那样,有一些图形设置放在{...}块中。
例如:
table.AddCell(new PdfPCell(new Phrase("Description:", bold)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
其中 PdfCell 图形设置在{SETTINGS} bloks设置中设置为边框,填充, MinimumHeight的值和 PaddingTop 属性(属于名为矩形
的类的对象实例)如果这些设置对象属于tu iTextSharp 框架,或者 Rectangle 类是由开发人员创建的自定义类,我无法理解这一点。我必须重建的应用程序。
我有这个疑问,因为我知道,在iTextSharp中,如果我想设置填充值,我只需要执行以下操作: cell.PaddingBottom = 10f; withoud使用填充对象
为了让您更好地了解这种情况,这是我的旧项目中存在的 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;
.................................................
.................................................
.................................................
}
}
答案 0 :(得分:1)
using语句确保始终处理正在使用的实例。 using语句还有其他一些好处。通常,您会将using语句与系统资源(如文件句柄和内存分配)结合使用。这可确保在函数失去范围时释放句柄。有关详细信息,请参阅此参考here。
这是一个封装类,用于封装itextsharp.dll库中的功能。此代码的开发人员重用现有类来封装存储/检索数据以配置pdf单元属性。这可能是出于多种原因而完成的,例如,可以从外部配置存储库中存储和读取单元配置,该外部配置存储库使用相同的类来获取和设置此文档的单元格attrinutes,但是可能有点过分。
我敢打赌,创建类是为了使包装器功能更具可扩展性。我可以设想一个类public class PdfMargin
来封装文档边距的配置。在那里,您可以在构造函数中定义一次边距,并简单地在代码中的其他位置引用边距类。
答案 1 :(得分:1)
使用关键字
using
关键字用于确保正确处理实现IDisposable
的类实例。 IDisposable
模式用于管理非托管资源的释放(文件句柄,与DB的连接,内存流......)。
可在msdn找到更多信息。
一般规则是拇指,如果您使用的是实现IDisposable
的类,您必须(或至少是强烈建议)通过using
关键字模式使用它。
<强> iTextSharp的强>
您引用的设置对象属于iTextSharp。例如,Rectangle
类在iTextSharp中的iTextSharp.text
命名空间中定义。
PdfPCell
也属于iTextSharp。
您可以看到Rectangle
类实现的唯一原因是因为iTextSharp在其DLL的可执行版本旁边提供源和符号,因此您可以查看其代码。但是,这不是您可以修改或拥有的某些代码。您只是引用了iTextSharp程序集。