翻譯|使用教程|編輯:李顯亮|2020-12-04 09:55:00.057|閱讀 368 次
概述:在各種應用程序中,PDF文檔是動態生成的,本文將學習如何使用C#創建PDF文件以及如何插入文本,圖像,表格和其他組件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
近年來,PDF文檔的自動生成和處理已成為一項苛刻的功能。在各種應用程序中,PDF文檔是動態生成的,例如發票,收據和不同類型的報告。因此,本文涵蓋了.NET應用程序中PDF自動化的基本實現。在本文中,將學習如何使用C#創建PDF文件以及如何插入文本,圖像,表格和其他組件。
(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
在C#中創建PDF文件
讓我們從創建一個包含文本片段的簡單PDF文檔開始。以下是步驟以及API參考。
下面的代碼示例演示如何使用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的步驟。
以下代碼示例顯示了如何使用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文檔。以下是執行此操作的步驟。
下面的代碼示例演示如何使用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文檔中創建和插入表格。以下是執行此操作的步驟。
下面的代碼示例演示如何在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中創建和添加表單的步驟。
以下代碼示例顯示了如何使用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");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn