c# - 使用 iText 7 for .NET 时如何定义 Colspan 和 Rowspan

标签 c# .net wpf mvvm itext7

我有 WPF 应用程序,我想在我的代码中实现 colspan 和 rowspan。我使用 iText 版本 7 将数据导出为 PDF。但是我不知道如何为 Cell 对象定义 colspan 或 rowspan。有什么办法可以解决吗?

我的目标实际上是在我的表格中创建主标题和副标题。

我的输出:

enter image description here

目标输出:

enter image description here

我的代码如下:

using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using Microsoft.Win32;
using System.Diagnostics;
using System.Windows;

namespace ITextPdf.UI
{
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Export_Btn(object sender, RoutedEventArgs e)
      {
         SaveFileDialog dlg = new SaveFileDialog
         {
            DefaultExt = ".pdf",
            Filter = "Adobe PDF Files(*.pdf)|*.pdf",
            FilterIndex = 1
         };

         string fileName = string.Empty;

         if (dlg.ShowDialog() == true)
         {
            fileName = dlg.FileName;

            PdfWriter writer = new PdfWriter(fileName);
            PdfDocument pdf = new PdfDocument(writer);

            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            Document document = new Document(pdf, PageSize.LETTER);
            document.SetMargins(20, 20, 20, 20);
            document.Add(new Paragraph("General Ledger")
               .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
               .SetFontSize(18)
               .SetFont(bold)
               .SetMarginBottom(-8));
            document.Add(new Paragraph("Naawan, Misamis Oriental")
               .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
               .SetFontSize(14)
               .SetFont(font)
               .SetMarginBottom(20));

            var table = new Table(new float[] { 80, 120, 80, 80, 80 });
            table.SetMinWidth(100)
               .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
               .SetFontSize(10);
            table.AddHeaderCell("Date");
            table.AddHeaderCell("Particulars");
            table.AddHeaderCell("Debit");
            table.AddHeaderCell("Credit");
            table.AddHeaderCell("Total Amount");
            table.AddCell("1/1/17");
            table.AddCell("Beginning Balance");
            table.AddCell("100.00");
            table.AddCell("");
            table.AddCell("100.00");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("300.00");
            table.AddCell("200.00");

            document.Add(table);
            document.Close();
            Process.Start(fileName);
         }
      }
   }
}

最佳答案

您需要调整这些行:

table.AddHeaderCell("Date");
table.AddHeaderCell("Particulars");
table.AddHeaderCell("Debit");
table.AddHeaderCell("Credit");
table.AddHeaderCell("Total Amount");

然后像这样改变它们:

table.AddHeaderCell(new Cell(2, 1).Add(new Paragraph("Date")));
table.AddHeaderCell(new Cell(2, 1).Add(new Paragraph("Particulars")));
table.AddHeaderCell(new Cell(1, 3).Add(new Paragraph("Amount")));
table.AddHeaderCell("Debit");
table.AddHeaderCell("Credit");
table.AddHeaderCell("Amount");

请注意,您最初提到了 PdfPCell 类,但 iText 7 中不再存在该类。您需要 Cell 类,如 tutorial 中所述。 .

关于c# - 使用 iText 7 for .NET 时如何定义 Colspan 和 Rowspan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49451789/

相关文章:

c# - 如何在 Windows 窗体中打开特定选项卡

c# - 在多人之间分配美元金额的舍入问题

c# - 如何删除使用 string.Replace ("-", ""后剩余的空间)

c# - String.Format() 中的间距

c# - ItemsControl 和 Canvas - 只有第一个项目可见

c# - 如何将常规文本附加到已经具有绑定(bind)到其文本属性的 TextBlock?

c# - Mono.Cecil Exception 在分析.NET 4.5 版本的System.Xml DLL 时抛出,为什么?

javascript - 一键发送两个表单,然后重定向到第三个操作

c# - ServiceStack - 如何在一项服务中托管多个版本端点?

c# - 为代码设置 ASP.NET 结构