如何繪製PDF嵌套表格?本篇文章就給大家詳細介紹繪製PDF嵌套表格的步驟。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。
巢狀表格,也就是在一張表格中的特定儲存格中再插入一個或多個表格,使用巢狀表格的優點在於能夠讓內容的佈局更加合理,同時也方便程式套用。在下面的範例中,將介紹如何透過C#程式設計來示範如何插入巢狀表格到PDF文件。
重點概括:
1. 插入巢狀表格
#2. 插入文字到巢狀表格
3. 插入圖片到嵌套表格
使用工具
#Spire.PDF 4.9.7
##註:
1.這裡使用的版本為4.9.7,經過測試,對於程式碼中涉及的PdfGridCellContentList類別和PdfGridCellContent類別僅在使用該版本或以上版本可用。使用時,請注意版本資訊。 2.下載安裝後,編輯程式碼時,請注意新增引用Spire.Pdf.dll(dll檔案可在安裝路徑下的Bin資料夾下取得)步驟1 :建立文件
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
步驟2 :新增字型、畫筆,寫入文字到PDF文檔
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true); PdfPen pen = new PdfPen(Color.Gray);string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking"; page.Canvas.DrawString(text, font, pen, 100, 50);
步驟3 :建立第一個表格
//创建一个PDF表格,并添加两行 PdfGrid grid = new PdfGrid(); PdfGridRow row1 = grid.Rows.Add(); PdfGridRow row2 = grid.Rows.Add(); //设置表格的单元格内容和边框之间的上、下边距 grid.Style.CellPadding.Top = 5f; grid.Style.CellPadding.Bottom = 5f; //添加三列,并设置列宽grid.Columns.Add(3); grid.Columns[0].Width = 120f; grid.Columns[1].Width = 150f; grid.Columns[2].Width = 120f;
步驟4 :建立一個巢狀表格
//创建一个一行两列的嵌套表格 PdfGrid embedGrid1 = new PdfGrid(); PdfGridRow newRow = embedGrid1.Rows.Add(); embedGrid1.Columns.Add(2); //设置嵌套表格的列宽 embedGrid1.Columns[0].Width = 50f; embedGrid1.Columns[1].Width = 60f;
步驟5 :新增文字、圖片到巢狀表格
//初始化SizeF类,设置图片大小 SizeF imageSize = new SizeF(45, 35); //实例化PdfGridCellContentList、PdfGridCellContent类,加载需要添加到嵌套表格的图片 PdfGridCellContentList contentList = new PdfGridCellContentList(); PdfGridCellContent content = new PdfGridCellContent(); content.Image = PdfImage.FromFile("1.png"); content.ImageSize = imageSize; contentList.List.Add(content); //实例化PdfStringFormat、PdfTrueTypeFont类,设置单元格文字对齐方式 PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); //添加文本内容及图片到嵌套表格 newRow.Cells[0].Value = "Norway"; newRow.Cells[0].StringFormat = stringFormat; newRow.Cells[1].Value = contentList; //将图片添加到嵌套表格的第二个单元格 newRow.Cells[1].StringFormat = stringFormat;
步驟6 :新增資料到第一個表格
//设置第一个表格的单元格的值和格式row1.Cells[0].Value = "Rank"; row1.Cells[0].StringFormat = stringFormat; row1.Cells[0].Style.Font = font; row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon; row1.Cells[1].Value = "Country"; row1.Cells[1].StringFormat = stringFormat; row1.Cells[1].Style.Font = font; row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon; row1.Cells[2].Value = "Total"; row1.Cells[2].StringFormat = stringFormat; row1.Cells[2].Style.Font = font; row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon; row2.Cells[0].Value = "1"; row2.Cells[0].StringFormat = stringFormat; row2.Cells[0].Style.Font = font; row2.Cells[1].Value = embedGrid1; //将嵌套表格添加到第一个表格的第二行第二个单元格 row2.Cells[1].StringFormat = stringFormat; row2.Cells[2].Value = "39"; row2.Cells[2].StringFormat = stringFormat; row2.Cells[2].Style.Font = font;
步驟7:將表格繪製到頁面指定位置
grid.Draw(page, new PointF(30f, 90f));
步驟8 :儲存文件
pdf.SaveToFile("result.pdf");
完成程式碼後,偵錯程序,產生文件。繪製的表格如下:
全部程式碼:using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
using System.Windows.Forms;
using System;
namespace NestedTable_PDF
{
class Program
{
static void Main(string[] args)
{
//实例化PdfDocument类,并添加页面到新建的文档
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//添加字体、画笔,写入文本到PDF文档
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 11f), true);
PdfPen pen = new PdfPen(Color.Gray);
string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
page.Canvas.DrawString(text, font, pen, 100, 50);
//创建一个PDF表格,并添加两行
PdfGrid grid = new PdfGrid();
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
//设置表格的单元格内容和边框之间的上、下边距
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;
//添加三列,并设置列宽
grid.Columns.Add(3);
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 150f;
grid.Columns[2].Width = 120f;
//创建一个一行两列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(2);
//设置嵌套表格的列宽
embedGrid1.Columns[0].Width = 50f;
embedGrid1.Columns[1].Width = 60f;
//初始化SizeF类,设置图片大小
SizeF imageSize = new SizeF(45, 35);
//实例化PdfGridCellContentList、PdfGridCellContent类,加载需要添加到嵌套表格的图片
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("1.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//实例化PdfStringFormat、PdfTrueTypeFont类,设置单元格文字对齐方式
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//添加文本内容及图片到嵌套表格
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //将图片添加到嵌套表格的第二个单元格
newRow.Cells[1].StringFormat = stringFormat;
//设置第一个表格的单元格的值和格式
row1.Cells[0].Value = "Rank";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.Font = font;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[1].Value = "Country";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.Font = font;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row1.Cells[2].Value = "Total";
row1.Cells[2].StringFormat = stringFormat;
row1.Cells[2].Style.Font = font;
row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;
row2.Cells[0].Value = "1";
row2.Cells[0].StringFormat = stringFormat;
row2.Cells[0].Style.Font = font;
row2.Cells[1].Value = embedGrid1; //将嵌套表格添加到第一个表格的第二行第二个单元格
row2.Cells[1].StringFormat = stringFormat;
row2.Cells[2].Value = "39";
row2.Cells[2].StringFormat = stringFormat;
row2.Cells[2].Style.Font = font;
//将表格绘制到页面指定位置
grid.Draw(page, new PointF(30f, 90f));
//保存文档并打开
pdf.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}
以上是本次C#在PDF中繪製嵌套表格的全部內容。
更多相關教程,請造訪:
以上是C#如何繪製PDF嵌套表格?繪製PDF嵌套表格的步驟的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

C#.NET是構建微服務的熱門選擇,因為其生態系統強大且支持豐富。 1)使用ASP.NETCore創建RESTfulAPI,處理訂單創建和查詢。 2)利用gRPC實現微服務間的高效通信,定義和實現訂單服務。 3)通過Docker容器化微服務,簡化部署和管理。

C#和.NET的安全最佳實踐包括輸入驗證、輸出編碼、異常處理、以及身份驗證和授權。 1)使用正則表達式或內置方法驗證輸入,防止惡意數據進入系統。 2)輸出編碼防止XSS攻擊,使用HttpUtility.HtmlEncode方法。 3)異常處理避免信息洩露,記錄錯誤但不返回詳細信息給用戶。 4)使用ASP.NETIdentity和Claims-based授權保護應用免受未授權訪問。

C 語言中冒號 (':') 的含義:條件語句:分隔條件表達式和語句塊循環語句:分隔初始化、條件和增量表達式宏定義:分隔宏名和宏值單行註釋:表示從冒號到行尾的內容為註釋數組維數:指定數組的維數


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境