最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解

 更新時(shí)間:2021年11月26日 10:26:40   作者:返回主頁E-iceblue  
嵌套表格,顧名思義,就是在一張表格中的特定單元格中再插入一個(gè)或者多個(gè)表格,本文將為大家介紹C#繪制PDF嵌套表格的代碼示例,需要的同學(xué)可以參考一下

嵌套表格,即在一張表格中的特定單元格中再插入一個(gè)或者多個(gè)表格,使用嵌套表格的優(yōu)點(diǎn)在于能夠讓內(nèi)容的布局更加合理,同時(shí)也方便程序套用。下面的示例中,將介紹如何通過C#編程來演示如何插入嵌套表格到PDF文檔。

要點(diǎn)概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入圖片到嵌套表格

使用工具

Spire.PDF 4.9.7

注:

1.這里使用的版本為4.9.7,經(jīng)測試,對于代碼中涉及的PdfGridCellContentList類和PdfGridCellContent類僅在使用該版本或者以上版本可用。使用時(shí),請注意版本信息。

2.下載安裝后,在編輯代碼時(shí),請注意添加引用Spire.Pdf.dll(dll文件可在安裝路徑下的Bin文件夾下獲取)

示例代碼(供參考)

步驟 1 :創(chuàng)建文檔

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 :創(chuàng)建第一個(gè)表格

//創(chuàng)建一個(gè)PDF表格,并添加兩行
PdfGrid grid = new PdfGrid(); 
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();

//設(shè)置表格的單元格內(nèi)容和邊框之間的上、下邊距
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;

//添加三列,并設(shè)置列寬
grid.Columns.Add(3);
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 150f;
grid.Columns[2].Width = 120f;

步驟 4 :創(chuàng)建一個(gè)嵌套表格

//創(chuàng)建一個(gè)一行兩列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(2);

//設(shè)置嵌套表格的列寬
embedGrid1.Columns[0].Width = 50f;
embedGrid1.Columns[1].Width = 60f;

步驟 5 :添加文本、圖片到嵌套表格

//初始化SizeF類,設(shè)置圖片大小
SizeF imageSize = new SizeF(45, 35);

//實(shí)例化PdfGridCellContentList、PdfGridCellContent類,加載需要添加到嵌套表格的圖片
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("1.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//實(shí)例化PdfStringFormat、PdfTrueTypeFont類,設(shè)置單元格文字對齊方式
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

//添加文本內(nèi)容及圖片到嵌套表格
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //將圖片添加到嵌套表格的第二個(gè)單元格
newRow.Cells[1].StringFormat = stringFormat;

步驟 6 :添加數(shù)據(jù)到第一個(gè)表格

//設(shè)置第一個(gè)表格的單元格的值和格式
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; //將嵌套表格添加到第一個(gè)表格的第二行第二個(gè)單元格
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");

完成代碼后,調(diào)試程序,生成文檔。繪制的表格如下:

全部代碼:

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)
        {
            //實(shí)例化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);

            //創(chuàng)建一個(gè)PDF表格,并添加兩行
            PdfGrid grid = new PdfGrid(); 
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();

            //設(shè)置表格的單元格內(nèi)容和邊框之間的上、下邊距
            grid.Style.CellPadding.Top = 5f;
            grid.Style.CellPadding.Bottom = 5f;

            //添加三列,并設(shè)置列寬
            grid.Columns.Add(3);
            grid.Columns[0].Width = 120f;
            grid.Columns[1].Width = 150f;
            grid.Columns[2].Width = 120f; 

            //創(chuàng)建一個(gè)一行兩列的嵌套表格
            PdfGrid embedGrid1 = new PdfGrid();
            PdfGridRow newRow = embedGrid1.Rows.Add();
            embedGrid1.Columns.Add(2);

            //設(shè)置嵌套表格的列寬
            embedGrid1.Columns[0].Width = 50f;
            embedGrid1.Columns[1].Width = 60f;

            //初始化SizeF類,設(shè)置圖片大小
            SizeF imageSize = new SizeF(45, 35);

            //實(shí)例化PdfGridCellContentList、PdfGridCellContent類,加載需要添加到嵌套表格的圖片
            PdfGridCellContentList contentList = new PdfGridCellContentList();
            PdfGridCellContent content = new PdfGridCellContent();
            content.Image = PdfImage.FromFile("1.png");
            content.ImageSize = imageSize;
            contentList.List.Add(content);
            //實(shí)例化PdfStringFormat、PdfTrueTypeFont類,設(shè)置單元格文字對齊方式
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);         

            //添加文本內(nèi)容及圖片到嵌套表格
            newRow.Cells[0].Value = "Norway";
            newRow.Cells[0].StringFormat = stringFormat;
            newRow.Cells[1].Value = contentList; //將圖片添加到嵌套表格的第二個(gè)單元格
            newRow.Cells[1].StringFormat = stringFormat;           

            //設(shè)置第一個(gè)表格的單元格的值和格式
            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; //將嵌套表格添加到第一個(gè)表格的第二行第二個(gè)單元格
            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# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解的詳細(xì)內(nèi)容,更多關(guān)于C# 的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#實(shí)現(xiàn)五子棋游戲

    C#實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C#中的委托Delegate

    C#中的委托Delegate

    這篇文章介紹了C#中的委托Delegate,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)

    C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)

    本文主要介紹了C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實(shí)例

    c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實(shí)例

    這篇文章主要介紹了c#只讀字段和常量的區(qū)別,以及靜態(tài)構(gòu)造函數(shù)的使用實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • C#使用windows服務(wù)發(fā)送郵件

    C#使用windows服務(wù)發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了C#使用windows服務(wù)發(fā)送郵件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#創(chuàng)建、部署、調(diào)用WebService圖文實(shí)例詳解

    C#創(chuàng)建、部署、調(diào)用WebService圖文實(shí)例詳解

    本文主要用詳細(xì)的圖文給大家介紹C#創(chuàng)建、部署、調(diào)用WebService的全部過程以及中間需要避免的問題。
    2017-11-11
  • C#合并多種格式文件為PDF的方法

    C#合并多種格式文件為PDF的方法

    這篇文章主要為大家詳細(xì)介紹了C#合并多種格式文件為PDF的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 在C#語言里對NULL的技術(shù)處理小結(jié)

    在C#語言里對NULL的技術(shù)處理小結(jié)

    在 C# 中處理 null 值是編寫可靠且可靠的代碼的一個(gè)重要方面,在本文中,我將討論一些在 C# 中處理 null 值的最常用技術(shù),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Unity C#打包AssetBundle與場景詳解

    Unity C#打包AssetBundle與場景詳解

    這篇文章主要給大家介紹了關(guān)于Unity C#打包AssetBundle與場景的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • C#操作SQLite實(shí)現(xiàn)數(shù)據(jù)的增刪改查

    C#操作SQLite實(shí)現(xiàn)數(shù)據(jù)的增刪改查

    SQLite是一個(gè)輕量級、跨平臺的關(guān)系型數(shù)據(jù)庫,在小型項(xiàng)目中,方便,易用,同時(shí)支持多種開發(fā)語言。本文將用C#語言對SQLite 的一個(gè)封裝,實(shí)現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下
    2022-01-01

最新評論

合阳县| 运城市| 海口市| 辽阳县| 崇义县| 扶绥县| 金平| 江西省| 银川市| 淅川县| 华宁县| 定结县| 彭水| 鹿邑县| 民丰县| 额敏县| 张家口市| 平顶山市| 广州市| 万宁市| 昭平县| 南投市| 和田市| 北流市| 泰和县| 土默特左旗| 莱州市| 原阳县| 若尔盖县| 台山市| 陈巴尔虎旗| 荆门市| 阿拉善右旗| 怀来县| 同德县| 福泉市| 柳河县| 永仁县| 许昌市| 特克斯县| 陇南市|