欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#/VB.NET實(shí)現(xiàn)在PDF文檔中創(chuàng)建表格

 更新時(shí)間:2023年12月17日 09:21:31   作者:E-iceblue  
表格是一種直觀高效的數(shù)據(jù)展示方式,可以按行和列的形式呈現(xiàn)數(shù)據(jù),從而更容易吸引讀者的注意,本文將介紹如何使用 Spire.PDF for .NET 通過(guò) .NET 程序在 PDF 文檔中創(chuàng)建表格,需要的可以參考下

表格是一種直觀高效的數(shù)據(jù)展示方式,可以按行和列的形式呈現(xiàn)數(shù)據(jù),從而更容易吸引讀者的注意。相比于普通文本內(nèi)容,表格能夠更清晰地反映數(shù)據(jù)之間的關(guān)系,降低閱讀難度,加深讀者對(duì)數(shù)據(jù)的理解。本文將介紹如何使用 Spire.PDF for .NET 通過(guò) .NET 程序在 PDF 文檔中創(chuàng)建表格。

Spire.PDF for .NET 提供了 PdfTable 和 PdfGrid 類來(lái)處理 PDF 文檔中的表格。PdfTable 類用于快速創(chuàng)建簡(jiǎn)單常規(guī)而沒有太多格式的表格。而 PdfGrid 類則用于創(chuàng)建更復(fù)雜的表格。

下表列出了這兩個(gè)類之間的區(qū)別。

PdfTablePdfGrid
格式設(shè)置
可通過(guò)事件設(shè)置, 無(wú) API 支持。可通過(guò) API 設(shè)置。
可通過(guò) API 設(shè)置(StringFormat)。可通過(guò) API 設(shè)置(StringFormat)。
單元格可通過(guò)事件設(shè)置 ,無(wú) API 支持。可通過(guò) API 設(shè)置。
其他
跨列合并不支持。可通過(guò) API 設(shè)置。
跨行合并可通過(guò)事件設(shè)置 ,無(wú) API 支持。可通過(guò) API 設(shè)置。
嵌套表格可通過(guò)事件設(shè)置 ,無(wú) API 支持。可通過(guò) API 設(shè)置。
事件BeginCellLayout, EndCellLayout, BeginRowLayout, EndRowLayout, BeginPageLayout, EndPageLayout.BeginPageLayout, EndPageLayout.

以下兩部分分別介紹如何使用 PdfTable 類和 PdfGrid 類在 PDF 文檔中創(chuàng)建表格:

  • 使用 PdfTable 類在 PDF 文檔中創(chuàng)建表格
  • 使用 PdfGrid 類在 PDF 文檔中創(chuàng)建表格

安裝 Spire.PDF for .NET

首先,您需要添加 Spire.PDF for .NET 包中包含的 DLL 文件作為 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò) NuGet 安裝。

使用 PdfTable 類在 PDF 文檔中創(chuàng)建表格

以下是使用 PdfTable 類在 PDF 文檔中創(chuàng)建表格的詳細(xì)操作步驟:

  • 創(chuàng)建一個(gè) PdfDocument 的對(duì)象。
  • 使用 PdfDocument.Pages.Add() 方法在 PDF 文檔中添加一個(gè)頁(yè)面。
  • 創(chuàng)建一個(gè) Pdftable 的對(duì)象。
  • 通過(guò) PdfTable.Style 屬性設(shè)置表格樣式。
  • 通過(guò) PdfTable.DataSource 屬性插入數(shù)據(jù)到表格中。
  • 通過(guò) BeginRowLayout 事件設(shè)置行高和行的背景色。
  • 使用 PdfTable.Draw() 方法在 PDF 頁(yè)面上繪制表格。
  • 使用 PdfDocument.SaveToFile() 方法保存 PDF 文檔。

C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
using System;
using System.Data;
using System.Drawing;

namespace CreatePDFTable
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //創(chuàng)建一個(gè)PdfDocument的對(duì)象
            PdfDocument doc = new PdfDocument();

            //添加一個(gè)頁(yè)面
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(40));

            //創(chuàng)建一個(gè)PdfTable的對(duì)象
            PdfTable table = new PdfTable();

            //設(shè)置表頭以及其他單元格的字體
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 12f, FontStyle.Regular), true);
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC Medium", 12f, FontStyle.Bold), true);

            //創(chuàng)建一個(gè)DataTable的對(duì)象
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("編號(hào)");
            dataTable.Columns.Add("姓名");
            dataTable.Columns.Add("部門");
            dataTable.Columns.Add("職位");
            dataTable.Columns.Add("等級(jí)");
            dataTable.Rows.Add(new string[] { "1", "大衛(wèi)", "信息部", "經(jīng)理", "1" });
            dataTable.Rows.Add(new string[] { "3", "朱穎", "人事部", "經(jīng)理", "1" });
            dataTable.Rows.Add(new string[] { "4", "蘇菲", "市場(chǎng)部", "經(jīng)理", "1" });
            dataTable.Rows.Add(new string[] { "7", "維奇", "市場(chǎng)部", "銷售代表", "2" });
            dataTable.Rows.Add(new string[] { "9", "韋恩", "人事部", "人力資源主管", "2" });
            dataTable.Rows.Add(new string[] { "11", "米雅", "開發(fā)部", "開發(fā)人員", "2" });

            //將數(shù)據(jù)表設(shè)置為表格的數(shù)據(jù)源
            table.DataSource = dataTable;

            //顯示表頭(表頭默認(rèn)隱藏)
            table.Style.ShowHeader = true;

            //設(shè)置表頭的字體顏色和背景色
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray;
            table.Style.HeaderStyle.TextBrush = PdfBrushes.White;

            //設(shè)置表頭的文本對(duì)齊方式
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            //設(shè)置其他單元格的文本對(duì)齊方式
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }

            //訂閱事件
            table.BeginRowLayout += Table_BeginRowLayout;

            //將表格繪制在頁(yè)面上
            table.Draw(page, new PointF(0, 30));

            //保存PDF文檔
            doc.SaveToFile("PdfTable.pdf");
            doc.Dispose();
        }

        //事件處理器
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            //設(shè)置行高
            args.MinimalHeight = 20f;

            //交替行的背景色
            if (args.RowIndex < 0)
            {
                return;
            }
            if (args.RowIndex % 2 == 1)
            {
                args.CellStyle.BackgroundBrush = PdfBrushes.LightGray;
            }
            else
            {
                args.CellStyle.BackgroundBrush = PdfBrushes.White;
            }
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Tables
Imports System
Imports System.Data
Imports System.Drawing

Namespace CreatePDFTable
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            '創(chuàng)建一個(gè)PdfDocument的對(duì)象
            Dim doc As PdfDocument = New PdfDocument()

            '添加一個(gè)頁(yè)面
            Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, New PdfMargins(40))

            '創(chuàng)建一個(gè)PdfTable的對(duì)象
            Dim table As PdfTable = New PdfTable()

            '設(shè)置表頭以及其他單元格的字體
            table.Style.DefaultStyle.Font = New PdfTrueTypeFont(New Font("HarmonyOS Sans SC", 12.0F, FontStyle.Regular), True)
            table.Style.HeaderStyle.Font = New PdfTrueTypeFont(New Font("HarmonyOS Sans SC Medium", 12.0F, FontStyle.Bold), True)

            '創(chuàng)建一個(gè)DataTable的對(duì)象
            Dim dataTable As DataTable = New DataTable()
            dataTable.Columns.Add("編號(hào)")
            dataTable.Columns.Add("姓名")
            dataTable.Columns.Add("部門")
            dataTable.Columns.Add("職位")
            dataTable.Columns.Add("等級(jí)")
            Dim String() As DataTable.Rows.Add(New
            {
            	 "1", "大衛(wèi)", "信息部", "經(jīng)理", "1" 
            }
)
            Dim String() As DataTable.Rows.Add(New
            {
            	 "3", "朱穎", "人事部", "經(jīng)理", "1" 
            }
)
            Dim String() As DataTable.Rows.Add(New
            {
            	 "4", "蘇菲", "市場(chǎng)部", "經(jīng)理", "1" 
            }
)
            Dim String() As DataTable.Rows.Add(New
            {
            	 "7", "維奇", "市場(chǎng)部", "銷售代表", "2" 
            }
)
            Dim String() As DataTable.Rows.Add(New
            {
            	 "9", "韋恩", "人事部", "人力資源主管", "2" 
            }
)
            Dim String() As DataTable.Rows.Add(New
            {
            	 "11", "米雅", "開發(fā)部", "開發(fā)人員", "2" 
            }
)
 
            '將數(shù)據(jù)表設(shè)置為表格的數(shù)據(jù)源
            table.DataSource = dataTable

            '顯示表頭(表頭默認(rèn)隱藏)
            table.Style.ShowHeader = True

            '設(shè)置表頭的字體顏色和背景色
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray
            table.Style.HeaderStyle.TextBrush = PdfBrushes.White

            '設(shè)置表頭的文本對(duì)齊方式
            table.Style.HeaderStyle.StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

            '設(shè)置其他單元格的文本對(duì)齊方式
            Dim i As Integer
            For i = 0 To table.Columns.Count - 1 Step i + 1
                table.Columns(i).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)
            Next

            '訂閱事件
            table.BeginRowLayout += Table_BeginRowLayout()

            '將表格繪制在頁(yè)面上
            table.Draw(page, New PointF(0, 30))

            '保存PDF文檔
            doc.SaveToFile("PdfTable.pdf")
            doc.Dispose()
        End Sub

        '事件處理器
        Private Shared Sub Table_BeginRowLayout(ByVal sender As Object, ByVal args As BeginRowLayoutEventArgs)
            '設(shè)置行高
            args.MinimalHeight = 20.0F

            '交替行的背景色
            If args.RowIndex < 0 Then
                Return
            End If
            If args.RowIndex % 2 = 1 Then
                args.CellStyle.BackgroundBrush = PdfBrushes.LightGray
            Else
                args.CellStyle.BackgroundBrush = PdfBrushes.White
            End If
        End Sub
    End Class
End Namespace

效果圖

使用 PdfGrid 類在 PDF 文檔中創(chuàng)建表格

下面是使用 PdfGrid 類在 PDF 文檔中創(chuàng)建表格的詳細(xì)操作步驟:

  • 創(chuàng)建一個(gè) PdfDocument 的對(duì)象。
  • 使用 PdfDocument.Pages.Add() 方法在 PDF 文檔中添加一個(gè)頁(yè)面。
  • 創(chuàng)建一個(gè) PdfGrid 對(duì)象。
  • 通過(guò) PdfGrid.Style 屬性設(shè)置表格樣式。
  • 使用 PdfGrid.Rows.Add() 方法為表格添加行。
  • 通過(guò) PdfGridRow.Cells[index].Value 屬性插入數(shù)據(jù)到指定單元格。
  • 通過(guò) PdfGridRow.RowSpan 或 PdfGridRow.ColumnSpan 屬性跨列或跨行合并單元格。
  • 通過(guò) PdfGridRow.Cells[index].StringFormat 和 PdfGridRow.Cells[index].Style 屬性設(shè)置指定單元格的格式。
  • 使用 PdfGrid.Draw() 方法在 PDF 頁(yè)面上繪制表格。
  • 使用 PdfDocument.SaveToFile() 方法保存 PDF 文檔。

C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Drawing;

namespace CreatePDFGrid
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //創(chuàng)建一個(gè)PdfDocument的對(duì)象
            PdfDocument doc = new PdfDocument();

            //添加一個(gè)頁(yè)面
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(40));

            //創(chuàng)建一個(gè)PdfGrid的對(duì)象
            PdfGrid grid = new PdfGrid();

            //設(shè)置單元格填充
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);

            //設(shè)置字體
            grid.Style.Font = new PdfTrueTypeFont(new Font("HarmonyOS Sans SC", 13f, FontStyle.Regular), true);

            //添加行
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();
            PdfGridRow row3 = grid.Rows.Add();
            PdfGridRow row4 = grid.Rows.Add();
            grid.Columns.Add(4);

            //獲取列寬
            foreach (PdfGridColumn col in grid.Columns)
            {
                col.Width = 110f;
            }

            //寫入數(shù)據(jù)到指定單元格
            row1.Cells[0].Value = "訂單及支付狀態(tài)";
            row2.Cells[0].Value = "訂單號(hào)";
            row2.Cells[1].Value = "日期";
            row2.Cells[2].Value = "顧客姓名";
            row2.Cells[3].Value = "是否已支付";
            row3.Cells[0].Value = "00223";
            row3.Cells[1].Value = "2022年06月02日";
            row3.Cells[2].Value = "專相地產(chǎn)";
            row3.Cells[3].Value = "已支付";
            row4.Cells[0].Value = "00224";
            row4.Cells[1].Value = "2022年06月03日";
            row4.Cells[3].Value = "未支付";

            //跨列合并單元格
            row1.Cells[0].ColumnSpan = 4;

            //跨行合并單元格
            row3.Cells[2].RowSpan = 2;

            //設(shè)置指定單元格的文本對(duì)齊方式
            row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            //設(shè)置指定單元格的背景色
            row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Orange;
            row4.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGray;

            //設(shè)置邊框格式
            PdfBorders borders = new PdfBorders();
            borders.All = new PdfPen(Color.Orange, 0.8f);
            foreach (PdfGridRow pgr in grid.Rows)
            {
                foreach (PdfGridCell pgc in pgr.Cells)
                {
                    pgc.Style.Borders = borders;
                }
            }

            //將表格繪制在頁(yè)面上
            grid.Draw(page, new PointF(0, 30));

            //保存PDF文檔
            doc.SaveToFile("PdfGrid.pdf");
            doc.Dispose();
        }
    }
}

VB.NET

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Grid
Imports System
Imports System.Drawing

Namespace CreatePDFGrid
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            '創(chuàng)建一個(gè)PdfDocument的對(duì)象
            Dim doc As PdfDocument = New PdfDocument()

            '添加一個(gè)頁(yè)面
            Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, New PdfMargins(40))

            '創(chuàng)建一個(gè)PdfGrid的對(duì)象
            Dim grid As PdfGrid = New PdfGrid()

            '設(shè)置單元格填充
            grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)

            '設(shè)置字體
            grid.Style.Font = New PdfTrueTypeFont(New Font("HarmonyOS Sans SC", 13.0F, FontStyle.Regular), True)

            '添加行
            Dim row1 As PdfGridRow = grid.Rows.Add()
            Dim row2 As PdfGridRow = grid.Rows.Add()
            Dim row3 As PdfGridRow = grid.Rows.Add()
            Dim row4 As PdfGridRow = grid.Rows.Add()
            grid.Columns.Add(4)

            '獲取列寬
            Dim col As PdfGridColumn
            For Each col In grid.Columns
                col.Width = 110.0F
            Next

            '寫入數(shù)據(jù)到指定單元格
            row1.Cells(0).Value = "訂單及支付狀態(tài)"
            row2.Cells(0).Value = "訂單號(hào)"
            row2.Cells(1).Value = "日期"
            row2.Cells(2).Value = "顧客姓名"
            row2.Cells(3).Value = "是否已支付"
            row3.Cells(0).Value = "00223"
            row3.Cells(1).Value = "2022年06月02日"
            row3.Cells(2).Value = "專相地產(chǎn)"
            row3.Cells(3).Value = "已支付"
            row4.Cells(0).Value = "00224"
            row4.Cells(1).Value = "2022年06月03日"
            row4.Cells(3).Value = "未支付"

            '跨列合并單元格
            row1.Cells(0).ColumnSpan = 4

            '跨行合并單元格
            row3.Cells(2).RowSpan = 2

            '設(shè)置指定單元格的文本對(duì)齊方式
            row1.Cells(0).StringFormat = New PdfStringFormat(PdfTextAlignment.Center)
            row3.Cells(2).StringFormat = New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)

            '設(shè)置指定單元格的背景色
            row1.Cells(0).Style.BackgroundBrush = PdfBrushes.Orange
            row4.Cells(3).Style.BackgroundBrush = PdfBrushes.LightGray

            '設(shè)置邊框格式
            Dim borders As PdfBorders = New PdfBorders()
            borders.All = New PdfPen(Color.Orange, 0.8F)
            Dim pgr As PdfGridRow
            For Each pgr In grid.Rows
                Dim pgc As PdfGridCell
                For Each pgc In pgr.Cells
                    pgc.Style.Borders = borders
                Next
            Next

            '將表格繪制在頁(yè)面上
            grid.Draw(page, New PointF(0, 30))

            '保存PDF文檔
            doc.SaveToFile("PdfGrid.pdf")
            doc.Dispose()
        End Sub
    End Class
End Namespace

效果圖

以上就是C#/VB.NET實(shí)現(xiàn)在PDF文檔中創(chuàng)建表格的詳細(xì)內(nèi)容,更多關(guān)于C# PDF創(chuàng)建表格的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論