c# iText使用

引入包

用nuget安装itext和itext.bouncy-castle-adapter包:
在这里插入图片描述
在这里插入图片描述

创建pdf

string path = "a.pdf";
PdfWriter writer = new PdfWriter(path);
PdfDocument pdfDoc = new PdfDocument(writer);
var doc=new Document(pdfDoc);
Paragraph p = new Paragraph("hello,测试一下");
PdfFont font = PdfFontFactory.CreateFont(@"C:\Windows\Fonts\SIMKAI.TTF", PdfEncodings.IDENTITY_H);
p.SetFont(font);

doc.Add(p);
doc.Close();

操作表格

string path = "a.pdf";
PdfWriter writer = new PdfWriter(path);
PdfDocument pdfDoc = new PdfDocument(writer);
var doc = new Document(pdfDoc);

var headerTexts = new[] {
	"标识", "姓名"
};

var table = new Table(headerTexts.Length)  // 设置表格列数
	.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);

//设置字体
PdfFont font = PdfFontFactory.CreateFont(@"C:\Windows\Fonts\SIMKAI.TTF", PdfEncodings.IDENTITY_H);
table.SetFont(font);

//添加表头
foreach (var header in headerTexts)
{
	table.AddHeaderCell(new Cell()
		.Add(new Paragraph(header))
		.SetBold()//设置粗体
		);
}

//第一行
table.AddCell(new Cell(0, 1).Add(new Paragraph("1"))//序号
		  .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE))

	  .AddCell(new Cell(0, 1).Add(new Paragraph("abc,测试")).SetMinWidth(25)//姓名  设置最小列宽25,方便名字横向显示
		  .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE));

//增加一行
table.StartNewRow();

//第二行
table.AddCell(new Cell(0, 1).Add(new Paragraph("2"))//序号
		  .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE))

	  .AddCell(new Cell(0, 1).Add(new Paragraph("abc,测试2")).SetMinWidth(25)//姓名  设置最小列宽25,方便名字横向显示
		  .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE));

//增加到文档
doc.Add(table);

//关闭文档
doc.Close();

读取文本

string path = "a.pdf";
PdfReader reader = new PdfReader(path);

PdfDocument pdfDoc= new PdfDocument(reader);
int numberOfPages = pdfDoc.GetNumberOfPages();
for(int i = 1; i <= numberOfPages; i++)
{
	var page=pdfDoc.GetPage(i);

	//提取文本
	string s=PdfTextExtractor.GetTextFromPage(page);

	
}


reader.Close();

相关推荐

  1. conda使用,pip使用

    2024-06-10 09:46:01       41 阅读
  2. VueUse使用

    2024-06-10 09:46:01       53 阅读
  3. Git<span style='color:red;'>使用</span>

    Git使用

    2024-06-10 09:46:01      40 阅读
  4. netty使用

    2024-06-10 09:46:01       39 阅读
  5. gdb<span style='color:red;'>使用</span>

    gdb使用

    2024-06-10 09:46:01      46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-10 09:46:01       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-10 09:46:01       5 阅读
  3. 在Django里面运行非项目文件

    2024-06-10 09:46:01       4 阅读
  4. Python语言-面向对象

    2024-06-10 09:46:01       7 阅读

热门阅读

  1. 爬山算法详细介绍

    2024-06-10 09:46:01       20 阅读
  2. 4. 流程控制语句

    2024-06-10 09:46:01       18 阅读
  3. vscode 好用的插件

    2024-06-10 09:46:01       19 阅读
  4. 23种设计模式——创建型模式

    2024-06-10 09:46:01       20 阅读
  5. 2024年6月10日--6月16日(渲染+ue独立游戏)

    2024-06-10 09:46:01       22 阅读
  6. Terminal Multiplexer的使用

    2024-06-10 09:46:01       24 阅读
  7. 什么情况下需要用到动态IP

    2024-06-10 09:46:01       21 阅读
  8. node-mysql中占位符?的使用

    2024-06-10 09:46:01       20 阅读
  9. 007 CentOS 7.9 apache-tomcat-9.0.89安装及配置

    2024-06-10 09:46:01       18 阅读
  10. 设计模式-策略模式

    2024-06-10 09:46:01       19 阅读
  11. 密码学及其应用——安全邮件、公钥密码学和PKI

    2024-06-10 09:46:01       24 阅读
  12. 语法、语义、语用与向量化

    2024-06-10 09:46:01       22 阅读
  13. 给gRPC增加负载均衡功能

    2024-06-10 09:46:01       21 阅读