翻譯|使用教程|編輯:李顯亮|2020-05-06 10:39:09.733|閱讀 423 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹添加表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何添加表格。
>>Aspose.PDF for .NET更新至最新版v20.5,歡迎下載體驗。
使用PDF文檔時,表格很重要。它們提供了用于以系統方式顯示信息的強大功能。該Aspose.PDF.Generator命名空間包含同名的類Table,Cell并且Row它從頭開始生成PDF文檔時創建表提供的功能。
要將表格添加到帶有.NET for Aspose.PDF的現有PDF文件中,請執行以下步驟:
以下代碼段顯示了如何在現有的PDF文件中添加文本。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Load source PDF document Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir+ "AddTable.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); dataDir = dataDir + "document_with_table_out.pdf"; // Save updated document containing table object doc.Save(dataDir);
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Instntiate the Pdf object by calling its empty constructor Document doc = new Document(); // Create the section in the Pdf object Page sec1 = doc.Pages.Add(); // Instantiate a table object Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table(); // Add the table in paragraphs collection of the desired section sec1.Paragraphs.Add(tab1); // Set with column widths of the table tab1.ColumnWidths = "50 50 50"; tab1.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow; // Set default cell border using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F); // Set table border using another customized BorderInfo object tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F); // Create MarginInfo object and set its left, bottom, right and top margins Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); margin.Top = 5f; margin.Left = 5f; margin.Right = 5f; margin.Bottom = 5f; // Set the default cell padding to the MarginInfo object tab1.DefaultCellPadding = margin; // Create rows in the table and then cells in the rows Aspose.Pdf.Row row1 = tab1.Rows.Add(); row1.Cells.Add("col1"); row1.Cells.Add("col2"); row1.Cells.Add("col3"); Aspose.Pdf.Row row2 = tab1.Rows.Add(); row2.Cells.Add("item1"); row2.Cells.Add("item2"); row2.Cells.Add("item3"); dataDir = dataDir + "AutoFitToWindow_out.pdf"; // Save updated document containing table object doc.Save(dataDir);
有時,需要動態獲取表寬度。Aspose.PDF.Table類具有用于此目的的GetWidth()方法。例如,您尚未顯式設置表列的寬度并將ColumnAdjustment設置為AutoFitToContent。在這種情況下,您可以按以下方式獲取表格寬度。
// Create a new document Document doc = new Document(); // Add page in document Page page = doc.Pages.Add(); // Initialize new table Table table = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToContent }; // Add row in table Row row = table.Rows.Add(); // Add cell in table Cell cell = row.Cells.Add("Cell 1 text"); cell = row.Cells.Add("Cell 2 text"); // Get table width Console.WriteLine(table.GetWidth());
用于.NET的Aspose.PDF支持將表格單元格添加到PDF文件中的功能。創建表格時,可以將文本或圖像添加到單元格中。此外,API還提供了將SVG文件轉換為PDF格式的功能。結合使用這些功能,可以加載SVG圖像并將其添加到表格單元格中。
以下代碼段顯示了創建表實例并在表單元格內添加SVG圖像的步驟。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Instantiate Document object Document doc = new Document(); // Create an image instance Aspose.Pdf.Image img = new Aspose.Pdf.Image(); // Set image type as SVG img.FileType = Aspose.Pdf.ImageFileType.Svg; // Path for source file img.File = dataDir + "SVGToPDF.svg"; // Set width for image instance img.FixWidth = 50; // Set height for image instance img.FixHeight = 50; // Create table instance Aspose.Pdf.Table table = new Aspose.Pdf.Table(); // Set width for table cells table.ColumnWidths = "100 100"; // Create row object and add it to table instance Aspose.Pdf.Row row = table.Rows.Add(); // Create cell object and add it to row instance Aspose.Pdf.Cell cell = row.Cells.Add(); // Add textfragment to paragraphs collection of cell object cell.Paragraphs.Add(new TextFragment("First cell")); // Add another cell to row object cell = row.Cells.Add(); // Add SVG image to paragraphs collection of recently added cell instance cell.Paragraphs.Add(img); // Create page object and add it to pages collection of document instance Page page = doc.Pages.Add(); // Add table to paragraphs collection of page object page.Paragraphs.Add(table); dataDir = dataDir + "AddSVGObject_out.pdf"; // Save PDF file doc.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn