PDF轉換控件Aspose.PDF for .Net使用教程(十四):在表格中添加HTML標簽和分頁符
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。
>>Aspose.PDF for .NET更新至最新版v19.9,歡迎下載體驗。
致改變世界的程序員——現在購買Aspose系列產品最高可享10000元高額減免!更多活動詳情可哦~
第五章:有關表格的處理
▲第二節:在表格內添加HTML標簽
有時需要導入具有一些HTML標記的數據庫內容,然后將其導入Table對象。導入內容時,應在PDF文檔中相應地呈現HTML標記。新版Aspose.PDF增強了ImprotDataTable()方法,以實現如下要求:
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); DataTable dt = new DataTable("Employee"); dt.Columns.Add("data", System.Type.GetType("System.String")); DataRow dr = dt.NewRow(); dr[0] = "Department of Emergency Medicine: 3400 Spruce Street Ground Silverstein Bldg Philadelphia PA 19104-4206"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "Penn Observation Medicine Service: 3400 Spruce Street Ground Floor Donner Philadelphia PA 19104-4206"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "UPHS/Presbyterian - Dept. of Emergency Medicine: 51 N. 39th Street . Philadelphia PA 19104-2640"; dt.Rows.Add(dr); Document doc = new Document(); doc.Pages.Add(); //初始化表的新實例 Aspose.Pdf.Table tableProvider = new Aspose.Pdf.Table(); //設置表格的列寬 tableProvider.ColumnWidths = "400 50 "; // 將表格邊框顏色設置為淺灰色 tableProvider.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); //設置表格單元格的邊框 tableProvider.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo(); margin.Top = 2.5F; margin.Left = 2.5F; margin.Bottom = 1.0F; tableProvider.DefaultCellPadding = margin; tableProvider.ImportDataTable(dt, false, 0, 0, 3, 1, true); doc.Pages[1].Paragraphs.Add(tableProvider); doc.Save(dataDir + "HTMLInsideTableCell_out.pdf");
▲第三節:在表格行之間插入分頁符
作為默認行為,在PDF文件中創建表格時,表格到達表格底邊距時,表格會流向后續頁面。但是,當為表添加一定數量的行時,我們可能需要強制插入分頁符。以下代碼段顯示了為表添加10行時插入分頁符的步驟。
//文檔目錄的路徑 string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); //實例化Document實例 Document doc = new Document(); // 將頁面添加到PDF文件的頁面集合 doc.Pages.Add(); //創建表實例 Aspose.Pdf.Table tab = new Aspose.Pdf.Table(); //設置表格的邊框樣式 tab.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red); //將邊框顏色設置為紅色的表格的默認邊框樣式 tab.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red); //指定表columsn widht tab.ColumnWidths = "100 100"; //創建一個循環以為表添加200行 for (int counter = 0; counter <= 200; counter++) { Aspose.Pdf.Row row = new Aspose.Pdf.Row(); tab.Rows.Add(row); Aspose.Pdf.Cell cell1 = new Aspose.Pdf.Cell(); cell1.Paragraphs.Add(new TextFragment("Cell " + counter + ", 0")); row.Cells.Add(cell1); Aspose.Pdf.Cell cell2 = new Aspose.Pdf.Cell(); cell2.Paragraphs.Add(new TextFragment("Cell " + counter + ", 1")); row.Cells.Add(cell2); //添加10行后,在新頁面中呈現新行 if (counter % 10 == 0 && counter != 0) row.IsInNewPage = true; } //將表格添加到PDF文件的段落集合中 doc.Pages[1].Paragraphs.Add(tab); dataDir = dataDir + "InsertPageBreak_out.pdf"; // 保存PDF文檔 doc.Save(dataDir);
ASPOSE技術交流QQ群(642018183)已開通,各類資源及時分享,歡迎交流討論!
掃描關注“慧聚IT”微信公眾號,及時獲取更多產品最新動態及最新資訊