如何使iTextSharp图像“向右浮动”?

3
我正在向PDF文件中添加图片,方法如下:
string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
    png.ScaleToFitLineWhenOverflow = true;
    doc.Add(png);
}

目前它可以工作,但图像并未正确添加到PDF文件中:

[enter image description here

我需要将图片(“仅限办公室使用”矩形)吸附到右侧,并与左侧显示的彩色文本在同一“行”上,如下所示:

[enter image description here

我该怎么做?将“ScaleToFitLineWhenOverflow”设置为true无效。我还减小了图像本身的尺寸,但这没有任何区别-它只是将较小的文件膨胀回与以前相同(屏幕)大小。

更新

一旦我记得实际加载较小的图像(而不仅仅是检查其是否存在),图像就会被缩小:

enter image description here

…但它仍然将自己藏在文本块的下方,而不是右侧。

更新2

我试图实现nelek的想法:

string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImgSmaller.png");
    png.ScaleToFitLineWhenOverflow = true; // this doesn't do what I hoped it would do (keep the img on the same line)
    PdfPTable tblImg = new PdfPTable(1);
    tblImg.WidthPercentage = 35;
    tblImg.HorizontalAlignment = Element.ALIGN_RIGHT;
    var parImg = new Paragraph();
    parImg.Add(png);
    PdfPCell imgCell = new PdfPCell();
    imgCell.AddElement(parImg);
    imgCell.BorderWidth = PdfPCell.NO_BORDER;
    tblImg.AddCell(imgCell);
    doc.Add(tblImg);
}

但是现在图片根本不显示。这是什么情况?这是什么问题?

更新3

好吧,这有点起作用了(仍需要调整):

if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImgSmaller.png"))
{
    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImgSmaller.png");
    PdfPTable tblImg = new PdfPTable(1);
    tblImg.WidthPercentage = 35;
    tblImg.HorizontalAlignment = Element.ALIGN_RIGHT;
    PdfPCell imgCell = new PdfPCell();
    imgCell.AddElement(png); //parImg);
    imgCell.BorderWidth = PdfPCell.NO_BORDER;
    tblImg.AddCell(imgCell);
    doc.Add(tblImg);
}

(我们不需要任何臭段落!)
抱歉,这句话是个玩笑话,它并没有实际意义。
for life's not a paragraph
And death i think is no parenthesis

更新4

通过一点代码调整(将图片大小恢复到原始尺寸,并将表格的WidthPercentage从35改为45),我现在在生成的PDF文件中看到了这个:

enter image description here

...那么我如何将此图片与左边的文本更好地对齐呢?我需要在两个表格中再包装一个双单元格的表格吗?

  • 考虑到情况,反思一下,使用一个表格并将其变成两列/单元格表格(而不是将两个单元/表格套入另一个表格)可能更有意义。

更新5

最新代码让我离目标更近了一步:

PdfPTable tbl = new PdfPTable(2);
tbl.WidthPercentage = 55;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.SetLeading(0, 1.2f);
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell();
chunky.AddElement(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);

string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImg.png"))
{
    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
    PdfPCell imgCell = new PdfPCell();
    imgCell.AddElement(png);
    imgCell.BorderWidth = PdfPCell.NO_BORDER;
    tbl.AddCell(imgCell);
    doc.Add(tbl);
}

...但现在的图片很小,挤压了表格第一个单元格中的文本:

输入图像描述

...也许我需要添加一个空单元格或其他什么东西...


2
创建表格并将图像放入单元格中是不可接受的吗? - nelek
这个表会不会也掉下去呢? - B. Clay Shannon-B. Crow Raven
1
不要生气,但是nelek在之前的评论中提到的是单元格而不是单个单元格。就像你在问题的最后一段所写的那样,一个表格有两个单元格 - 是的,这就是他的想法。干杯。 - nelek
有两个neleks吗? - B. Clay Shannon-B. Crow Raven
nelek:把它作为答案,我会标记它。 - B. Clay Shannon-B. Crow Raven
1
不,只有一个……我在谈论我自己……没有必要这样做……那只是建议而非答案,如果能帮到你,太好了。 - nelek
1个回答

3

做法基本上就是在更新5中的代码(制作一个具有两个单元格/行的表格,并将图片放在第二个存储块中),但将百分比从55改为100(这可能是默认值,因此多余)。

PdfPTable tbl = new PdfPTable(2);
tbl.WidthPercentage = 100;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.SetLeading(0, 1.2f);
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);
par.Add(middlePart);
par.Add(anchorCCO);
PdfPCell chunky = new PdfPCell();
chunky.AddElement(par);
chunky.BorderWidth = PdfPCell.NO_BORDER;
tbl.AddCell(chunky);

string imagepath = @"C:\Users\Default\";
if (System.IO.File.Exists(imagepath + "OfficeUseOnlyImg.png"))
{
    iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imagepath + "OfficeUseOnlyImg.png");
    PdfPCell imgCell = new PdfPCell();
    imgCell.AddElement(png);
    imgCell.BorderWidth = PdfPCell.NO_BORDER;
    tbl.AddCell(imgCell);
    doc.Add(tbl);
}

使用这段代码,图像基本上显示如预期:

enter image description here

注意:工作代码是基于我在这里找到的在线示例。


2
@BClayShannon... 你好... 要注意 PdfPTableWidthPercentage,默认值不是100%,而是80%... 如果你需要定义每一列的宽度,可以这样做(以下是vb示例,抱歉):Dim cp() As Single = {50, 50} : tbl.SetWidths(cp) ... 不再打扰你了。 - nelek
1
谢谢,这让我免去了注释掉我认为是多余的百分比赋值和由此导致的自动拔发的麻烦! - B. Clay Shannon-B. Crow Raven

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接