PDF管理控件Aspose.PDF for .Net使用教程(三十六):在表格內(nèi)添加HTML標(biāo)簽和分頁符
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成、修改、轉(zhuǎn)換、渲染、保護(hù)和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項(xiàng),表格創(chuàng)建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務(wù),擴(kuò)展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發(fā)者帶來Aspose.PDF for .NET的一系列使用教程,例如進(jìn)行文檔間的轉(zhuǎn)換,如何標(biāo)記PDF文件,如何使用表單和圖表等等。本文將在表格內(nèi)添加HTML標(biāo)簽、在表格行之間插入分頁符、將表格邊框提取為圖像。
>>Aspose.PDF for .NET更新至最新版v20.5,歡迎下載體驗(yàn)。
使用PDF文檔時(shí),表格很重要。它們提供了用于以系統(tǒng)方式顯示信息的強(qiáng)大功能。該Aspose.PDF.Generator命名空間包含同名的類Table,Cell并且Row它從頭開始生成PDF文檔時(shí)創(chuàng)建表提供的功能。
在表格內(nèi)添加HTML標(biāo)簽
有時(shí),可能會(huì)提出一個(gè)要求,即導(dǎo)入具有一些HTML標(biāo)記的數(shù)據(jù)庫(kù)內(nèi)容,然后將其導(dǎo)入Table對(duì)象。導(dǎo)入內(nèi)容時(shí),應(yīng)在PDF文檔中相應(yīng)地呈現(xiàn)HTML標(biāo)記。
// The path to the documents directory. 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] = "<li>Department of Emergency Medicine: 3400 Spruce Street Ground Silverstein Bldg Philadelphia PA 19104-4206</li>"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "<li>Penn Observation Medicine Service: 3400 Spruce Street Ground Floor Donner Philadelphia PA 19104-4206</li>"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "<li>UPHS/Presbyterian - Dept. of Emergency Medicine: 51 N. 39th Street . Philadelphia PA 19104-2640</li>"; dt.Rows.Add(dr); Document doc = new Document(); doc.Pages.Add(); // Initializes a new instance of the Table Aspose.Pdf.Table tableProvider = new Aspose.Pdf.Table(); //Set column widths of the table tableProvider.ColumnWidths = "400 50 "; // Set the table border color as LightGray tableProvider.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray)); // Set the border for table cells 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");
在表格行之間插入分頁符
作為默認(rèn)行為,當(dāng)在PDF文件中創(chuàng)建表格時(shí),表格到達(dá)表格底邊距時(shí),表格會(huì)流向后續(xù)頁面。但是,當(dāng)為表添加一定數(shù)量的行時(shí),我們可能需要強(qiáng)制插入分頁符。以下代碼段顯示了為表添加10行時(shí)插入分頁符的步驟。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); // Instantiate Document instance Document doc = new Document(); // Add page to pages collection of PDF file doc.Pages.Add(); // Create table instance Aspose.Pdf.Table tab = new Aspose.Pdf.Table(); // Set border style for table tab.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red); // Set default border style for table with border color as Red tab.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red); // Specify table columsn widht tab.ColumnWidths = "100 100"; // Create a loop to add 200 rows for table 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); // When 10 rows are added, render new row in new page if (counter % 10 == 0 && counter != 0) row.IsInNewPage = true; } // Add table to paragraphs collection of PDF file doc.Pages[1].Paragraphs.Add(tab); dataDir = dataDir + "InsertPageBreak_out.pdf"; // Save the PDF document doc.Save(dataDir);
將表格邊框提取為圖像
頁面邊框是路徑繪制操作。因此,Pdf-> Html處理邏輯僅執(zhí)行繪圖指令并將背景放在文本后面。因此,要重復(fù)此邏輯,您必須手動(dòng)處理內(nèi)容運(yùn)算符并自己繪制圖形。另外請(qǐng)注意,以下代碼段可能不適用于各種PDF文件,但是如果您遇到任何問題,請(qǐng)隨時(shí)與我們聯(lián)系。此代碼是為特定的PDF文件開發(fā)的。以下代碼段顯示了從PDF文檔中提取表格邊框作為Image的步驟。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); Document doc = new Document(dataDir + "input.pdf"); Stack graphicsState = new Stack(); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)doc.Pages[1].PageInfo.Width, (int)doc.Pages[1].PageInfo.Height); System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); // Default ctm matrix value is 1,0,0,1,0,0 System.Drawing.Drawing2D.Matrix lastCTM = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, 0); // System.Drawing coordinate system is top left based, while pdf coordinate system is low left based, so we have to apply the inversion matrix System.Drawing.Drawing2D.Matrix inversionMatrix = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, (float)doc.Pages[1].PageInfo.Height); System.Drawing.PointF lastPoint = new System.Drawing.PointF(0, 0); System.Drawing.Color fillColor = System.Drawing.Color.FromArgb(0, 0, 0); System.Drawing.Color strokeColor = System.Drawing.Color.FromArgb(0, 0, 0); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bitmap)) { gr.SmoothingMode = SmoothingMode.HighQuality; graphicsState.Push(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0)); // Process all the contents commands foreach (Operator op in doc.Pages[1].Contents) { Aspose.Pdf.Operators.GSave opSaveState = op as Aspose.Pdf.Operators.GSave; Aspose.Pdf.Operators.GRestore opRestoreState = op as Aspose.Pdf.Operators.GRestore; Aspose.Pdf.Operators.ConcatenateMatrix opCtm = op as Aspose.Pdf.Operators.ConcatenateMatrix; Aspose.Pdf.Operators.MoveTo opMoveTo = op as Aspose.Pdf.Operators.MoveTo; Aspose.Pdf.Operators.LineTo opLineTo = op as Aspose.Pdf.Operators.LineTo; Aspose.Pdf.Operators.Re opRe = op as Aspose.Pdf.Operators.Re; Aspose.Pdf.Operators.EndPath opEndPath = op as Aspose.Pdf.Operators.EndPath; Aspose.Pdf.Operators.Stroke opStroke = op as Aspose.Pdf.Operators.Stroke; Aspose.Pdf.Operators.Fill opFill = op as Aspose.Pdf.Operators.Fill; Aspose.Pdf.Operators.EOFill opEOFill = op as Aspose.Pdf.Operators.EOFill; Aspose.Pdf.Operators.SetRGBColor opRGBFillColor = op as Aspose.Pdf.Operators.SetRGBColor; Aspose.Pdf.Operators.SetRGBColorStroke opRGBStrokeColor = op as Aspose.Pdf.Operators.SetRGBColorStroke; if (opSaveState != null) { // Save previous state and push current state to the top of the stack graphicsState.Push(((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Clone()); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opRestoreState != null) { // Throw away current state and restore previous one graphicsState.Pop(); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opCtm != null) { System.Drawing.Drawing2D.Matrix cm = new System.Drawing.Drawing2D.Matrix( (float)opCtm.Matrix.A, (float)opCtm.Matrix.B, (float)opCtm.Matrix.C, (float)opCtm.Matrix.D, (float)opCtm.Matrix.E, (float)opCtm.Matrix.F); // Multiply current matrix with the state matrix ((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Multiply(cm); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opMoveTo != null) { lastPoint = new System.Drawing.PointF((float)opMoveTo.X, (float)opMoveTo.Y); } else if (opLineTo != null) { System.Drawing.PointF linePoint = new System.Drawing.PointF((float)opLineTo.X, (float)opLineTo.Y); graphicsPath.AddLine(lastPoint, linePoint); lastPoint = linePoint; } else if (opRe != null) { System.Drawing.RectangleF re = new System.Drawing.RectangleF((float)opRe.X, (float)opRe.Y, (float)opRe.Width, (float)opRe.Height); graphicsPath.AddRectangle(re); } else if (opEndPath != null) { graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opRGBFillColor != null) { fillColor = opRGBFillColor.getColor(); } else if (opRGBStrokeColor != null) { strokeColor = opRGBStrokeColor.getColor(); } else if (opStroke != null) { graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.DrawPath(new System.Drawing.Pen(strokeColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opFill != null) { graphicsPath.FillMode = FillMode.Winding; graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.FillPath(new System.Drawing.SolidBrush(fillColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opEOFill != null) { graphicsPath.FillMode = FillMode.Alternate; graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.FillPath(new System.Drawing.SolidBrush(fillColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } } } dataDir = dataDir + "ExtractBorder_out.png"; bitmap.Save(dataDir, ImageFormat.Png);
還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。