PDF管理控件Aspose.PDF for .Net使用教程(三十六):在表格内添加HTML标签和分页符

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

>>Aspose.PDF for .NET更新至最新版v20.5,欢迎下载体验。


使用PDF文档时,表格很重要。它们提供了用于以系统方式显示信息的强大功能。该Aspose.PDF.Generator命名空间包含同名的类Table,Cell并且Row它从头开始生成PDF文档时创建表提供的功能。

在表格内添加HTML标签

有时,可能会提出一个要求,即导入具有一些HTML标记的数据库内容,然后将其导入Table对象。导入内容时,应在PDF文档中相应地呈现HTML标记。

// 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 TableAspose.Pdf.Table tableProvider = new Aspose.Pdf.Table();//Set column widths of the tabletableProvider.ColumnWidths = "400 50 ";// Set the table border color as LightGraytableProvider.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));// Set the border for table cellstableProvider.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行时插入分页符的步骤。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Tables();// Instantiate Document instanceDocument doc = new Document();// Add page to pages collection of PDF filedoc.Pages.Add();// Create table instanceAspose.Pdf.Table tab = new Aspose.Pdf.Table();// Set border style for tabletab.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red);// Set default border style for table with border color as Redtab.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Red);// Specify table columsn widhttab.ColumnWidths = "100 100";// Create a loop to add 200 rows for tablefor (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处理逻辑仅执行绘图指令并将背景放在文本后面。因此,要重复此逻辑,您必须手动处理内容运算符并自己绘制图形。另外请注意,以下代码段可能不适用于各种PDF文件,但是如果您遇到任何问题,请随时与我们联系。此代码是为特定的PDF文件开发的。以下代码段显示了从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,0System.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 matrixSystem.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);


还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年4月11日
下一篇 2020年4月11日

相关推荐

发表回复

登录后才能评论