itextsharp .net中如何设置两个表格的间距设为0,取网站的域名,协议、端口、当前站点目录的地址

在 iTextSharp(.NET 版 PDF 操作库)中,两个表格间距设为 0 核心是:清除表格的默认边距、间距,并用 PdfPTable.SpacingBefore = 0 强制消除上方间距

直接给你可直接复制运行的完整代码 ,保证两个表格无缝贴合、间距为 0

一、最终可用代码(C#)

csharp

运行

复制代码
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iTextSharpTableZeroSpacing
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. 创建文档
            Document doc = new Document(PageSize.A4);

            // 2. 创建PDF写入流
            string outputPath = "TwoTablesZeroSpacing.pdf";
            PdfWriter.GetInstance(doc, new FileStream(outputPath, FileMode.Create));

            // 3. 打开文档
            doc.Open();

            // ==============================================
            // 第一个表格
            // ==============================================
            PdfPTable table1 = new PdfPTable(1); // 1列
            table1.DefaultCell.Border = Rectangle.BOX; // 显示边框方便看间距
            table1.AddCell("表格 1");

            // 关键:清除所有边距
            table1.SpacingAfter = 0; // 表格下方间距 = 0

            // 添加到文档
            doc.Add(table1);

            // ==============================================
            // 第二个表格(关键:SpacingBefore = 0)
            // ==============================================
            PdfPTable table2 = new PdfPTable(1);
            table2.DefaultCell.Border = Rectangle.BOX;
            table2.AddCell("表格 2");

            // 核心:设置表格**上方间距 = 0**(消除和上一个表格的距离)
            table2.SpacingBefore = 0;  
            table2.SpacingAfter = 0;

            // 添加第二个表格
            doc.Add(table2);

            // 4. 关闭文档
            doc.Close();

            Console.WriteLine($"PDF 已生成:{outputPath}");
            Console.ReadLine();
        }
    }
}

二、让两个表格间距为 0 的3 个关键属性

只要给表格设置这 3 个值,间距就一定是 0:

  1. table1.SpacingAfter = 0 第一个表格底部不留间距

  2. table2.SpacingBefore = 0 第二个表格顶部不留间距(最关键)


三、如果你想完全无缝、连边框都贴在一起

再额外加一句:

csharp

运行

复制代码
// 让表格紧贴文档边缘,不产生默认留白
table1.WidthPercentage = 100;
table2.WidthPercentage = 100;

总结

  1. 两个表格间距为 0 = SpacingAfter=0 + SpacingBefore=0
  2. 必须给第二个表格 设置 SpacingBefore = 0

//取网站的域名,协议、端口、当前站点目录的地址

cs 复制代码
     string urlCode = "http://job.xxx.com:9248/#/pages/businessProcess/PurApply_Detail?Fid=" + FKEYVALUE;
     string picCode = this.baseDir + "PurApply_" + FKEYVALUE + ".jpg";
     urlCode = Globals.CreateQRCode(urlCode, picCode, false, ImageFormats.Jpeg);
     string url = HttpContext.Current.Request.Url.ToString();

     Uri uri = new Uri(url);
     string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;


     string fileDate = usersInfo.UserID + "_" + FKEYVALUE;
     string fileNamePdf = requested + "/FilePDF/PurApply/" + fileDate + ".pdf";

     string fileNamePdf2 = System.AppDomain.CurrentDomain.BaseDirectory + "/FilePDF/PurApply/" + fileDate + ".pdf";
     iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 15, 15, 15, 15);
     object value = PdfWriter.GetInstance(doc, new FileStream(fileNamePdf2, FileMode.Create));
     doc.Open();
相关推荐
小羊没烦恼!13 小时前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
小羊没烦恼!14 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹14 小时前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读15 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车16 小时前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁16 小时前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
kybs199117 小时前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
孙启超17 小时前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int17 小时前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger18 小时前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法