以上的类中,我们可以看到我们可以设置需要打印的纸张类型,根据实际情况可以选择。在最下面我们看到了两种方法,一个是PageSize()设置纸张大小,一个是GetRectangle()绘制矩形。
以上是对itextSharp组件的一些类和方法的简单介绍,对于表格,单元格等等类的介绍就不再继续,有兴趣的可以自己查看源代码信息。
三.itextSharp组件实例:
上面介绍了itextSharp组件的背景、特性,以及组件的核心类和方法,在这里给出一个简单的itextSharp组件操作的实例,这个实例只是一个简单的介绍。
/// <summary> /// 字体 /// </summary> private Font _font; /// <summary> /// 文档大小 /// </summary> private Rectangle _rect; /// <summary> /// 文档对象 /// </summary> private readonly Document _document; /// <summary> /// 基础字体 /// </summary> private BaseFont _basefont; /// <summary> /// 构造函数 /// </summary> public PDFOperation() { _rect = PageSize.A4; _document = new Document(_rect); } /// <summary> /// 构造函数 /// </summary> /// <param>页面大小(如"A4")</param> public PDFOperation(string type) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(type); } SetPageSize(type); _document = new Document(_rect); } /// <summary> /// 构造函数 /// </summary> /// <param>页面大小(如"A4")</param> /// <param>内容距左边框距离</param> /// <param>内容距右边框距离</param> /// <param>内容距上边框距离</param> /// <param>内容距下边框距离</param> public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(type); } SetPageSize(type); _document = new Document(_rect, marginLeft, marginRight, marginTop, marginBottom); } /// <summary> /// 设置字体 /// </summary> public void SetBaseFont(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } _basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } /// <summary> /// 设置字体 /// </summary> /// <param>字体大小</param> public void SetFont(float size) { _font = new Font(_basefont, size); } /// <summary> /// 设置页面大小 /// </summary> /// <param>页面大小(如"A4")</param> public void SetPageSize(string type) { if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException(type); } switch (type.Trim()) { //枚举需要的文档纸张大小 case "A3": _rect = PageSize.A3; break; case "A4": _rect = PageSize.A4; break; case "A8": _rect = PageSize.A8; break; } } /// <summary> /// 实例化文档 /// </summary> /// <param>文档相关信息(如路径,打开方式等)</param> public void GetInstance(Stream os) { if (os == null) { throw new ArgumentNullException("os"); } PdfWriter.GetInstance(_document, os); } /// <summary> /// 打开文档对象 /// </summary> /// <param>文档相关信息(如路径,打开方式等)</param> public void Open(Stream os) { if (os == null) { throw new ArgumentNullException("os"); } GetInstance(os); _document.Open(); } /// <summary> /// 关闭打开的文档 /// </summary> public void Close() { _document.Close(); } /// <summary> /// 添加段落 /// </summary> /// <param>内容</param> /// <param>字体大小</param> public void AddParagraph(string content, float fontsize) { SetFont(fontsize); var pra = new Paragraph(content, _font); _document.Add(pra); } /// <summary> /// 添加段落 /// </summary> /// <param>内容</param> /// <param>字体大小</param> /// <param>对齐方式(1为居中,0为居左,2为居右)</param> /// <param>段后空行数(0为默认值)</param> /// <param>段前空行数(0为默认值)</param> /// <param>行间距(0为默认值)</param> public void AddParagraph(string content, float fontsize, int alignment, float spacingAfter, float spacingBefore, float multipliedLeading) { SetFont(fontsize); var pra = new Paragraph(content, _font) { Alignment = alignment }; if (spacingAfter != 0) { pra.SpacingAfter = spacingAfter; } if (spacingBefore != 0) { pra.SpacingBefore = spacingBefore; } if (multipliedLeading != 0) { pra.MultipliedLeading = multipliedLeading; } _document.Add(pra); } /// <summary> /// 添加图片 /// </summary> /// <param>图片路径</param> /// <param>对齐方式(1为居中,0为居左,2为居右)</param> /// <param>图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param> /// <param>图片高</param> public void AddImage(string path, int alignment, float newWidth, float newHeight) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } var img = Image.GetInstance(path); img.Alignment = alignment; // ReSharper disable once CompareOfFloatsByEqualityOperator if (newWidth != 0) { img.ScaleAbsolute(newWidth, newHeight); } else { if (img.Width > PageSize.A4.Width) { img.ScaleAbsolute(_rect.Width, img.Width * img.Height / _rect.Height); } } _document.Add(img); } /// <summary> /// 添加链接 /// </summary> /// <param>链接文字</param> /// <param>字体大小</param> /// <param>链接地址</param> public void AddAnchorReference(string content, float fontSize, string reference) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } SetFont(fontSize); var auc = new Anchor(content, _font) { Reference = reference }; _document.Add(auc); } /// <summary> /// 添加链接点 /// </summary> /// <param>链接文字</param> /// <param>字体大小</param> /// <param>链接点名</param> public void AddAnchorName(string content, float fontSize, string name) { if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(content); } SetFont(fontSize); var auc = new Anchor(content, _font) { Name = name }; _document.Add(auc); }