【发布时间】: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} 块设置 Border、Padding、MinimumHeight 的值 和 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