【问题标题】:iTextSharp - How can I iterate tables side by side in C# winforms?iTextSharp - 如何在 C# winforms 中并排迭代表格?
【发布时间】:2017-08-10 04:42:43
【问题描述】:

我有一个图书馆,我想为他们打印图书馆标签。当您从 GridControl 中选择书籍并单击 Print Labels 按钮时,程序会在 pdf 文件上创建标签,如下所示:

private void btnQRPrintLabels_Click(object sender, EventArgs e)
{
    // These are books' ids. Normally, these are retrieved from GridControl element.
    var books_ids = new List<int>() {1, 2, 5, 6, 7, 12};
    // I don't use PanelControl. But in the future, maybe I can use it later.
    CreateLabels(books_ids, panelControl1);
}

这里是CreateLabels方法的内容:

public void CreateLabels(List<int> books_ids, PanelControl p)
{
    var doc = new Document(PageSize.A4, 10, 10, 10, 10);
    var m = new SaveFileDialog
    {
        Filter = @"PDF File Format|*.pdf",
        FileName = "Labels.pdf"
    };

    if (m.ShowDialog() != DialogResult.OK) return;

    var file = new FileStream(m.FileName, FileMode.Create);
    wr = PdfWriter.GetInstance(doc, file);

    doc.Open();
    cb = wr.DirectContent;
    wr.PageEvent = this;
    // Labels are created from this method.
    Labels(doc, books_ids);
    doc.Close();
}

最后是Labels方法的内容:

protected void Labels(Document doc, List<int> books_ids)
{
    var i = 1;
    foreach (var book_id in books_ids)
    {
        var alignment = (i % 2 != 0) ? Element.ALIGN_LEFT : Element.ALIGN_RIGHT;

        // Book's informations are retrieved from Linq Connect Model.
        var _connection = new LinqtoSQLiteDataContext();
        var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id);
        // Outer table to get rounded corner...
        var table = new PdfPTable(1) { WidthPercentage = 48, HorizontalAlignment = alignment };
        // Inner table: First cell for QR code and second cell for book's informations.
        var inner_table = new PdfPTable(2) { WidthPercentage = 100 };
        inner_table.DefaultCell.Border = Rectangle.NO_BORDER;

        var inner_measurements = new[] { 40f, 60f };
        inner_table.SetWidths(inner_measurements);
        // Generate QR code in the `Tools` class, `GenerateQR` method.
        System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn);
        var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
        var qr = iTextSharp.text.Image.GetInstance(pdfImage);

        var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER };
        var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER };

        inner_table.AddCell(a);
        inner_table.AddCell(b);

        var s = new PdfPCell(inner_table)
        {
            CellEvent = new RoundRectangle(),
            Border = Rectangle.NO_BORDER,
            Padding = 2,
            HorizontalAlignment = Element.ALIGN_LEFT
        };
        table.AddCell(s);
        doc.Add(table);
        i++;
    }
}

这些代码给我带来这样的标签:

但我想得到这样的标签:

因为如果我设法并排获取标签,我可以轻松地将其打印到计算机标签上。这是我想在上面打印的计算机标签:

如何并排迭代表?

【问题讨论】:

    标签: c# winforms itext iteration


    【解决方案1】:

    使用一个外部表与

    new PdfPTable(2) { WidthPercentage = 100 }
    

    然后将所有内部表添加到此表中,最后将外部表添加到文档中。这样就不需要使用切换左/右对齐方式了。

    :编辑:

    var table = new PdfPTable(2) { WidthPercentage = 100 };
    
    foreach (var book_id in books_ids)
    {
      // Book's informations are retrieved from Linq Connect Model.
      var _connection = new LinqtoSQLiteDataContext();
      var book = _connection.books.SingleOrDefault(b_no => b_no.id == book_id);
      // Outer table to get rounded corner...
    
      // Inner table: First cell for QR code and second cell for book's informations.
      var inner_table = new PdfPTable(2) { WidthPercentage = 100 };
      inner_table.DefaultCell.Border = Rectangle.NO_BORDER;
    
      var inner_measurements = new[] { 40f, 60f };
      inner_table.SetWidths(inner_measurements);
      // Generate QR code in the `Tools` class, `GenerateQR` method.
      System.Drawing.Image image = Tools.GenerateQR(100, 100, book?.isbn);
      var pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
      var qr = iTextSharp.text.Image.GetInstance(pdfImage);
    
      var a = new PdfPCell(qr) { Border = Rectangle.NO_BORDER };
      var b = new PdfPCell(new Phrase(book?.name, font_normal)) { Border = Rectangle.NO_BORDER };
    
      inner_table.AddCell(a);
      inner_table.AddCell(b);
    
      PdfPCell labelCell = new PdfPCell(inner_table)
      {
          CellEvent = new RoundRectangle(),
          Border = Rectangle.NO_BORDER,
          Padding = 2,
          HorizontalAlignment = Element.ALIGN_LEFT
      };
    
      table.AddCell(labelCell);               
    }
    
    doc.Add(table);
    

    【讨论】:

    • 我不明白在foreach (var book_id in books_ids)循环中创建内表时如何导入外表?
    • 嗯...我编辑了上面的文字并发布了您的代码并进行了一些修改。我在记事本中写了这个,所以它可能无法直接使用 - 但是通过这种结构,您应该了解如何获得所需的输出。
    【解决方案2】:

    只需添加页面级表格(2 列,6 行)并将每个标签放入此页面级表格的单个单元格中。

    【讨论】:

    • 抱歉,我不明白我是怎么做到的?我用谷歌搜索:google.com.tr/?gws_rd=ssl#q=itextsharp+page-level+table&* 但我没有找到任何关于此的信息。
    • 我的意思是表格将在整个页面上,因为每个标签都使用表格,标签布局使用下一个表格。
    • 是的,因为我认为,我必须使用圆角的表。
    猜你喜欢
    • 1970-01-01
    • 2016-06-20
    • 2022-11-10
    • 2014-01-06
    • 1970-01-01
    • 2021-07-22
    • 2017-03-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多