C#开发 使用iTextSharp组件在pdf文件中写入表格

目录

准备工作

1.新建Project

2.下载iTextSharp

示例

1.实现效果

2.完整代码


iTextSharp是一个开源组件,可用于生成pdf文档。这里记录一下我用iTextSharp创建pdf文档、写入表格的方法。

目前iTextSharp已被iText7所取代,推荐新项目使用iText7。

NuGet Gallery | iTextSharp 5.5.13.3

C#开发 使用iTextSharp组件在pdf文件中写入表格_第1张图片

准备工作

1.新建Project

选择Console App类型(.NET Framework 4.5.2)。

C#开发 使用iTextSharp组件在pdf文件中写入表格_第2张图片

2.下载iTextSharp

右键Project,选择[Manage Nuget Package]。

C#开发 使用iTextSharp组件在pdf文件中写入表格_第3张图片

搜索iTextSharp并下载最后更新的版本。

C#开发 使用iTextSharp组件在pdf文件中写入表格_第4张图片

示例

1.实现效果

生成pdf文件,并写入表格。最终产物如下。

C#开发 使用iTextSharp组件在pdf文件中写入表格_第5张图片

2.完整代码

实现方法基本可以分成以下几步:

1) 创建FileStream,

2) 创建iTextSharp.text.Document,

3) 创建iTextSharp.text.pdf.PdfWriter,

4) Open document,

5) 写入数据,步骤大致为 Document <- Paragraph <- PdfPTable <- PdfPCell

6) Close document.

运行通过的完整代码如下。

using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;


namespace PdfSample
{
    public class Program
    {
		static void Main(string[] args)
        {
			// PDF文件目标生成路径
			string targetFilepath = "D:/output_table.pdf";

			// 创建文件流
			using (FileStream fs = new FileStream(targetFilepath, FileMode.Create, FileAccess.Write, FileShare.None))
			// 创建A4大小的document,设置页边距
			using (Document document = new Document(PageSize.A4,20,20,30,30)) 
			using (PdfWriter writer = PdfWriter.GetInstance(document, fs))
			{
				// 打开document
				document.Open();

				// 创建自定义字体
				Font boldFont = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD);
				Font cellFont = new Font(Font.FontFamily.TIMES_ROMAN, 10);

				// 添加文字,设置字体,设置水平居中
				Paragraph para1 = new Paragraph("Document Title", boldFont);
				para1.Alignment = Rectangle.ALIGN_CENTER;
				document.Add(para1);

				// 添加文字,设置段前距离
				Paragraph para2 = new Paragraph("Subtitle");
				para2.FirstLineIndent = 55;
				document.Add(para2);

				// 添加空行
				Paragraph emptyline = new Paragraph(" ");
				document.Add(emptyline);

				// 创建一个4列的表格,并设置列宽
				PdfPTable table = new PdfPTable(4);
				table.SetWidths(new float[] { 1f, 2f, 3f, 1f });

				string[] tableHeader1 = { "col1 ", "col2", "col3", "col4" };
				PdfPCell cell;
				foreach (string th1 in tableHeader1)
				{
					cell = new PdfPCell(new Phrase(th1, cellFont));
					// 设置单元格背景色
					cell.BackgroundColor = new BaseColor(176, 224, 230);

					// 设置文字水平居中、垂直居中
					cell.HorizontalAlignment = Element.ALIGN_CENTER;
					cell.VerticalAlignment = Element.ALIGN_MIDDLE;

					// 把cell添加到table
					table.AddCell(cell);
				}

				// 创建list作为数据源
				List list = new List();
				for (int i = 0; i < 8; i++)
				{
					list.Add("value_"+ i.ToString());
				}

				// 将list中的数据添加到table
				PdfPCell cell1;
				foreach (string data in list)
				{
					cell1 = new PdfPCell(new Phrase(data, cellFont));
					// 设置单元格最小高度
					cell1.MinimumHeight = 20;
					table.AddCell(cell1);
				}

				// 将table添加到document
				document.Add(table);

				// 关闭document
				document.Close();
			}

			Console.WriteLine("done");
			Console.ReadKey();
		}
	}

}

运行以后会在D盘下面生成'output_table.pdf'这个文件。

你可能感兴趣的:(C#开发,c#,visualstudio)