c# - iTextSharp 在单个单元格中添加文本和条形码?

标签 c# itextsharp

基本上,我想使用 iTextSharp 将字符串和条形码组合到一个单元格中。 从下面的代码来看,它是以下两行:

 table.AddCell(tempstring);
 table.AddCell(new text.Phrase(new text.Chunk(image39, 0, 0)));

下面列出了完整的代码

using text = iTextSharp.text;
using pdf = iTextSharp.text.pdf;

text.Document doc = new text.Document();

            pdf.PdfWriter writer = pdf.PdfWriter.GetInstance(doc, new FileStream(pdfgen.sWorkPath + "\\OrderNumber" + txtSellerNumber.Text.ToString() + ".pdf", FileMode.Create));
            doc.Open();
            pdf.PdfContentByte cb = writer.DirectContent;

            pdf.Barcode39 code39 = new pdf.Barcode39();
            code39.Code = txtSellerNumber.Text.ToString();
            code39.StartStopText = false;
            text.Image image39 = code39.CreateImageWithBarcode(cb, null, null);

            iTextSharp.text.Table table = new iTextSharp.text.Table(3);
            table.BorderWidth = 2;
            table.BorderColor = new text.Color(0, 0, 255);
            table.Padding = 3;
            table.Spacing = 1;
            text.Cell cell = new text.Cell("CHS");
            cell.Header = true;
            cell.Colspan = 3;


            StringBuilder sb = new StringBuilder();
            sb.Append("NAME" + Environment.NewLine);
            sb.Append("Seller #" + txtSellerNumber.Text.ToString() + Environment.NewLine);
            sb.Append("Size #" + txtSize1.Text.ToString() + Environment.NewLine);
            sb.Append("Price #" + txtPrice1.Text.ToString() + Environment.NewLine);
            sb.Append("Description : " + txtDescription1.Text.ToString() + Environment.NewLine);

            string tempstring = sb.ToString();
            //Wanting to combine the following two cells into 1
            table.AddCell(tempstring);
            table.AddCell(new text.Phrase(new text.Chunk(image39, 0, 0)));
            //End
            doc.Add(table);

            doc.Close();
            HttpContext.Current.Response.Redirect("~/OrderNumber" + txtSellerNumber.Text.ToString() + ".pdf", false);

最佳答案

其实很简单...

PdfPCell cell = new PdfPCell( table.DefaultCell /* optional */ );
cell.AddElement( tempString );
cell.AddElement(new text.Phrase(new text.Chunk(image39, 0, 0)));
table.AddCell(cell);

尝试一些变化……你可能需要做一些实验,比如这个

PdfPCell cell = new PdfPCell( table.DefaultCell /* optional */ );
Phrase phrase = new Phrase();
phrase.Add(new Chunk(tempString));
phrase.Add(new Chunk(image39, 0, 0)));
cell.AddElement( phrase );
table.AddCell(cell);

关于c# - iTextSharp 在单个单元格中添加文本和条形码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215522/

相关文章:

C#逻辑内存配置 View

c# - Google Vision API指定JSON文件

Kotlin 内联类的 C# 等价物

pdf - 使用 iTextSharp 自定义 PDFStamper

itextsharp - 如何在Itextsharp中添加当前页码和总页数

C# CURL POST(内容类型,哈希键)

c# - 不能隐式转换类型 System.DateTime?到 System.DateTime

c# - iTextsharp) 边距宽度

在 saas 项目中使用 itextsharp 的许可考虑

c# - 在pdf c#中的另一个图像上添加图像水印