- 在C#中创建PDF文件
- 使用C#在PDF中插入图像
- 使用C#在PDF中创建表格
- 在C#中以PDF创建表单
(安装包仅提供部分功能,并设置限制,如需试用完整功能请申请免费授权。)
17周年庆来啦!整合所有格式API处理控件Aspose.Total永久授权火热促销中,新购乐享85折起!联系客服立马1分钟了解全部!
在C#中创建PDF文件
让我们从创建一个包含文本片段的简单PDF文档开始。以下是步骤以及API参考。
- 创建一个Document类的对象。
- 使用Document.Pages.Add()方法将页面添加到文档中。
- 创建一个新的TextFragment对象并设置其文本。
- 将TextFragment添加到页面的Paragraphs集合中。
- 使用Document.Save(String)方法保存PDF文件。
下面的代码示例演示如何使用C#创建简单的PDF文件。
// Initialize document objectDocument document = new Document();// Add pagePage page = document.Pages.Add();// Add text to new pagepage.Paragraphs.Add(new Aspose.Pdf.Text.// Save PDFdocument.Save
修改PDF文件就像创建一个新文件一样简单。只需使用Document类加载文件,执行所需的操作,然后保存即可。以下是修改PDF的步骤。
- 创建Document类的对象,并提供其构造函数的PDF文件路径。
- 处理文档的页面或内容。
- 使用Document.Save()方法保存文档。
以下代码示例显示了如何使用C#修改PDF。
// Load PDFvar pdfDocument = new Aspose.Pdf.Document("document.pdf");//pdfDocument.Pages.Add();// Save the updated PDFpdfDocument.Save(modifiedFileName);
使用C#在PDF中插入图像
现在让我们检查如何将图像插入PDF文档。以下是执行此操作的步骤。
- 创建Document类的对象以打开PDF文档。
- 使用Page类访问要添加图像的页面。
- 将图像添加到页面的资源集合中。
-
使用运算符将图像放置在页面上:
- GSave
- ConcatenateMatrix运算符,用于指定要放置图像的位置。
- 做操作员在页面上绘制图像。
- 最后,使用GRestore运算符保存更新的图形状态。
- 保存PDF文件。
下面的代码示例演示如何使用C#将图像添加到PDF文档。
// Open documentDocument pdfDocument = new Document("document.pdf");// Set coordinatesint lowerLeftX = 100;int lowerLeftY = 100;int upperRightX = 200;int upperRightY = 200;// Get the page where image needs to be addedPage page = pdfDocument.Pages[1];// Load image into streamFileStream imageStream = new FileStream("aspose-logo.jpg", FileMode.Open);// Add image to Images collection of Page Resourcespage.Resources.Images.Add(imageStream);// Using GSave operator: this operator saves current graphics statepage.Contents.Add(new Aspose.Pdf.Operators.GSave());// Create Rectangle and Matrix objectsAspose.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 placedpage.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));XImage ximage = page.Resources.Images[page.Resources.Images.Count];// Using Do operator: this operator draws imagepage.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));// Using GRestore operator: this operator restores graphics statepage.Contents.Add(new Aspose.Pdf.Operators.GRestore()); // Save updated documentpdfDocument.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 documentAspose.Pdf.Document doc = new Aspose.Pdf.Document("document.pdf");// Initializes a new instance of the TableAspose.Pdf.Table table = new Aspose.Pdf.Table();// Set the table border color as LightGraytable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));// Set the border for table cellstable.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));// Create a loop to add 10 rowsfor (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 documentDocument pdfDocument = new Document("document.pdf");// Create a fieldTextBoxField 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 documentpdfDocument.Form.Add(textBoxField, 1);// Save modified PDFpdfDocument.Save("output.pdf");
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!