PDF处理控件Aspose.PDF功能演示:使用C#创建带有表格、表单、图片的PDF文件

近年来,PDF文档的自动生成和处理已成为一项苛刻的功能。在各种应用程序中,PDF文档是动态生成的,例如发票,收据和不同类型的报告。因此,本文涵盖了.NET应用程序中PDF自动化的基本实现。在本文中,将学习如何使用C#创建PDF文件以及如何插入文本,图像,表格和其他组件。

  • 在C#中创建PDF文件
  • 在C#中编辑现有的PDF
  • 使用C#在PDF中插入图像
  • 使用C#在PDF中创建表格
  • 在C#中以PDF创建表单

点击下载最新版Aspose.PDF(qun:761297826)icon-default.png?t=N7T8https://www.evget.com/product/565/download


在C#中创建PDF文件

让我们从创建一个包含文本片段的简单PDF文档开始。以下是步骤以及API参考。

  • 创建一个Document类的对象。
  • 使用Document.Pages.Add()方法将页面添加到文档中。
  • 创建一个新的TextFragment对象并设置其文本。
  • 将TextFragment添加到页面的Paragraphs集合中。
  • 使用Document.Save(String)方法保存PDF文件。

下面的代码示例演示如何使用C#创建简单的PDF文件。

// Initialize document object
Document document = new Document();

// Add page
Page page = document.Pages.Add();

// Add text to new page
page.Paragraphs.Add(new Aspose.Pdf.Text.

// Save PDF 
document.Save

在C#中编辑现有的PDF

修改PDF文件就像创建一个新文件一样简单。只需使用Document类加载文件,执行所需的操作,然后保存即可。以下是修改PDF的步骤。

  • 创建Document类的对象,并提供其构造函数的PDF文件路径。
  • 处理文档的页面或内容。
  • 使用Document.Save()方法保存文档。

以下代码示例显示了如何使用C#修改PDF。

// Load PDF
var pdfDocument = new Aspose.Pdf.Document("document.pdf");

//
pdfDocument.Pages.Add();

// Save the updated PDF
pdfDocument.Save(modifiedFileName);

使用C#在PDF中插入图像

现在让我们检查如何将图像插入PDF文档。以下是执行此操作的步骤。

  • 创建Document类的对象以打开PDF文档。
  • 使用Page类访问要添加图像的页面。
  • 将图像添加到页面的资源集合中。
  • 使用运算符将图像放置在页面上:
    • GSave
    • ConcatenateMatrix运算符,用于指定要放置图像的位置。
    • 做操作员在页面上绘制图像。
    • 最后,使用GRestore运算符保存更新的图形状态。
  • 保存PDF文件。

下面的代码示例演示如何使用C#将图像添加到PDF文档。

// Open document
Document pdfDocument = new Document("document.pdf");

// Set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;

// Get the page where image needs to be added
Page page = pdfDocument.Pages[1];

// Load image into stream
FileStream imageStream = new FileStream("aspose-logo.jpg", FileMode.Open);

// Add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);

// Using GSave operator: this operator saves current graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GSave());

// Create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];

// Using Do operator: this operator draws image
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

// Using GRestore operator: this operator restores graphics state
page.Contents.Add(new Aspose.Pdf.Operators.GRestore()); 

// Save updated document
pdfDocument.Save("document.pdf");

使用C#在PDF中创建表格

表格是文档的重要组成部分,用于以行和列的形式组织数据。用于.NET的Aspose.PDF为您提供了一种非常简单的方法来在PDF文档中创建和插入表格。以下是执行此操作的步骤。

  • 使用Document类加载PDF文件。
  • 初始化表并使用Table类设置其列和行。
  • 设置表格的设置(即边框)。
  • 通过使用Table.Rows.Add()方法创建行来填充表。
  • 使用Document.Pages [1] .Paragraphs.Add(Table)方法将表添加到页面。
  • 保存PDF文件。

下面的代码示例演示如何在C#中的PDF文档中创建和添加表格。

// Load source PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document("document.pdf");

// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();

// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// Create a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++) { // Add row to table Aspose.Pdf.Row row = table.Rows.Add(); // Add table cells row.Cells.Add("Column (" + row_count + ", 1)"); row.Cells.Add("Column (" + row_count + ", 2)"); row.Cells.Add("Column (" + row_count + ", 3)"); } // Add table object to first page of input document doc.Pages[1].Paragraphs.Add(table); // Save updated document containing table object doc.Save("document_with_table_out.pdf");

在C#中以PDF创建表单

PDF中的表格用于从阅读器收集数据。您可以在PDF表单中插入文本框,复选框,单选按钮和其他受支持的控件。PDF格式支持两种形式的表格:Acro表单和XFA表单(请参阅详细信息)。以下是在PDF中创建和添加表单的步骤。

  • 使用Document类加载PDF文件。
  • 创建表单控件,例如TextBoxField。
  • 使用Document.Form.Add(textBoxField,1)方法将控件添加到表单。
  • 保存PDF文档。

以下代码示例显示了如何使用C#将表单添加到PDF文档。

// Open document
Document pdfDocument = new Document("document.pdf");

// Create a field
TextBoxField textBoxField = new TextBoxField(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(100, 200, 300, 300));
textBoxField.PartialName = "textbox1";
textBoxField.Value = "Text Box";

// TextBoxField.Border = new Border(
Border border = new Border(textBoxField);
border.Width = 5;
border.Dash = new Dash(1, 1);
textBoxField.Border = border;

textBoxField.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);

// Add field to the document
pdfDocument.Form.Add(textBoxField, 1);

// Save modified PDF
pdfDocument.Save("output.pdf");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值