iTextSharp PDF使用表格嵌入条形码。无法格式化为一页

时间:2016-03-22 13:14:17

标签: c# itextsharp

我正在创建一个条形码页面以适应onlinelabels.com (ol2050LP)

我已经成功制作了条形码,并且还可以像我期望的那样在桌子上很好地展示它们。

我遇到的问题是,页面上有一些幻影页眉和页脚推送页面的最后两行条形码。

我的结果:

enter image description here

以下是我的方法:

 public void CreatePDF(List<BarcodeLib.Barcode.DataMatrix> listOfBarcodes)
        {
            if (listOfBarcodes.Count >= 1)
            {
                Document document = new Document();
                PdfPTable table = new PdfPTable(13);
                document.SetPageSize(PageSize.A4);
                document.SetMargins(0.25f, 0.25f, 0.2505f, 0.2505f);

                try
                {
                    PdfWriter.GetInstance(document, new FileStream(@"test.pdf", FileMode.CreateNew));
                    document.Open();
                    foreach (BarcodeLib.Barcode.DataMatrix image in listOfBarcodes)
                    {
                        iTextSharp.text.Image dmatrix = iTextSharp.text.Image.GetInstance(image.drawBarcodeAsBytes(), true);
                        dmatrix.SetDpi(300, 300);
                        dmatrix.ScaleAbsolute(37, 37);
                        table.AddCell(dmatrix);
                    }
                    document.Add(table);
                    document.Close();
                }
                catch (DocumentException de)
                {
                    Console.Error.WriteLine(de.Message);
                }
                catch (IOException ioe)
                {
                    Console.Error.WriteLine(ioe.Message);
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

在打开文档之前,您应该这样做:

Document document = new Document();
document.SetMargins(0f, 0f, 0f, 0f);

<强>更新<!/强>

比:

document.Open();

以下是它在我的程序中的工作原理:

without SetMargins with SetMargins

(左)没有SetMargins,(右)有SetMargins

相关问题