如何在 itextsharp 中的 2 个文本字符串之间放置图像
How to place an Image between 2 text string in itextsharp
我需要在 2 个文本字符串之间放置一个图像。请看下面的代码。正如我所尝试的,但第一个文本与图像重叠。请帮我解决这个问题。
代码
// Create a new Phrase and add the image to it
var cellContent = new Phrase();
var fontHeader = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Paragraph("\n" + "Default Header", fontHeader));
Code39BarcodeDraw barcode39 = BarcodeDrawFactory.Code39WithoutChecksum;
System.Drawing.Image img = barcode39.Draw(val[cnt], 25);
var pdfImg = iTextSharp.text.Image.GetInstance(ReadImage(img));
var width = pdfImg.PlainWidth;
if (width > colWidths.ToArray()[0])
pdfImg.ScaleAbsoluteWidth(width - 20);
cellContent.Add(new Chunk(pdfImg, 0, 0));
var font = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Chunk("\n" + val[cnt], font));
cnt += 1;
//Create a new cell specifying the content
var cell = new PdfPCell(cellContent);
//Ensure our label height is adhered to
cell.FixedHeight = _label.Height;
//Centre align the content
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Border = IncludeLabelBorders ? Rectangle.BOX : Rectangle.NO_BORDER;
//Add to the row
rowCells.Add(cell);
更改此行:
cellContent.Add(new Chunk(pdfImg, 0, 0));
收件人:
cellContent.Add(new Chunk(pdfImg, 0, 0, true));
这样,Phrase
的前导会适应Image
的高度。
我需要在 2 个文本字符串之间放置一个图像。请看下面的代码。正如我所尝试的,但第一个文本与图像重叠。请帮我解决这个问题。
代码
// Create a new Phrase and add the image to it
var cellContent = new Phrase();
var fontHeader = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Paragraph("\n" + "Default Header", fontHeader));
Code39BarcodeDraw barcode39 = BarcodeDrawFactory.Code39WithoutChecksum;
System.Drawing.Image img = barcode39.Draw(val[cnt], 25);
var pdfImg = iTextSharp.text.Image.GetInstance(ReadImage(img));
var width = pdfImg.PlainWidth;
if (width > colWidths.ToArray()[0])
pdfImg.ScaleAbsoluteWidth(width - 20);
cellContent.Add(new Chunk(pdfImg, 0, 0));
var font = FontFactory.GetFont("Verdana", BaseFont.CP1250, true, 12, 0);
cellContent.Add(new Chunk("\n" + val[cnt], font));
cnt += 1;
//Create a new cell specifying the content
var cell = new PdfPCell(cellContent);
//Ensure our label height is adhered to
cell.FixedHeight = _label.Height;
//Centre align the content
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Border = IncludeLabelBorders ? Rectangle.BOX : Rectangle.NO_BORDER;
//Add to the row
rowCells.Add(cell);
更改此行:
cellContent.Add(new Chunk(pdfImg, 0, 0));
收件人:
cellContent.Add(new Chunk(pdfImg, 0, 0, true));
这样,Phrase
的前导会适应Image
的高度。