欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在C#里繪制PDF嵌套表格

今天就跟大家聊聊有關(guān)怎么在C#里繪制PDF嵌套表格,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、固陽(yáng)網(wǎng)站維護(hù)、網(wǎng)站推廣。

要點(diǎn)概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入圖片到嵌套表格

使用工具

  • Spire.PDF 4.9.7

注:

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

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

怎么在C#里繪制PDF嵌套表格

示例代碼(供參考)

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

PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();

步驟 2 :添加字體、畫(huà)筆,寫(xiě)入文本到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è)置單元格文字對(duì)齊方式
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:將表格繪制到頁(yè)面指定位置

grid.Draw(page, new PointF(30f, 90f));

步驟 8 :保存文檔

pdf.SaveToFile("result.pdf");

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

怎么在C#里繪制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)
        {
            //實(shí)例化PdfDocument類,并添加頁(yè)面到新建的文檔
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //添加字體、畫(huà)筆,寫(xiě)入文本到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è)置單元格文字對(duì)齊方式
            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;

            //將表格繪制到頁(yè)面指定位置
            grid.Draw(page, new PointF(30f, 90f));

            //保存文檔并打開(kāi)
            pdf.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

以上是本次C#在PDF中繪制嵌套表格的全部?jī)?nèi)容。

看完上述內(nèi)容,你們對(duì)怎么在C#里繪制PDF嵌套表格有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

標(biāo)題名稱:怎么在C#里繪制PDF嵌套表格
本文鏈接:http://chinadenli.net/article30/ihegso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)服務(wù)器托管全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作