gpt4 book ai didi

c# - 对这个 iTextSharp 应用程序如何工作有疑问?

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:27 24 4
gpt4 key购买 nike

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

在此应用程序中,有一个 Controller 类包含一个方法,该方法将 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) using 语句到底做了什么?为什么在前面的代码中,iTextSharp Document 对象被创建到 using 语句中?与经典创作成代码有什么区别?

2) 正如您在前面的代码中所见,{...} block 中放入了一些图形设置。

例如这样的事情:

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

其中PdfCell图形设置设置到{SETTINGS} bloks设置Border, Padding, MinimumHeight的值PaddingTop 属性(属于名为 Rectangle

的类的对象实例

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

我有这个疑问,因为我知道,在 iTextSharp 中,如果我想设置一个填充值,我可以简单地做一些类似的事情:cell.PaddingBottom = 10f; without using a 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;
.................................................
.................................................
.................................................
}
}

最佳答案

使用关键字

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

更多信息可以在 msdn 上找到.

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

iTextSharp

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

PdfPCell 也属于 iTextSharp。

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

关于c# - 对这个 iTextSharp 应用程序如何工作有疑问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22866714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com