如何才能将此iTextSharp图像移至"向右浮动"?

时间:2015-07-23 20:48:26

标签: c# image itextsharp

我将图像添加到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

但是,我需要将图像(" Office Use Only"矩形)磁化到右侧,同一行"行"如左边显示的杂色文字,如:

[enter image description here

我该怎么做?设置" ScaleToFitLineWhenOverflow"真的没有帮助。我也缩小了图像本身的大小,但没有区别 - 它只是采用较小的文件并将其膨胀回原来的相同(屏幕)大小。

更新

一旦我记得实际加载较小的图像(而不仅仅是检查它的存在),图像就会受到惩罚:

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);
}

(我们不需要stinkin'段落!)

for life's not a paragraph
And death i think is no parenthesis

更新4

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

enter image description here

...那么如何通过Twitter之前的引导将img拉出来以更好地与左边的文本对齐?我是否必须将两张桌子包在另一张双人桌子里?

  • 我认为,在反思时,只使用一个表,并使其成为一个两列/单元格表(而不是将两个单细胞变形虫包裹在另一个表中,而不是将表包裹在另一个表中)可能更有意义。 / LI>

更新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);
}

...但现在图片很小,并且在桌子的第一个单元格中压缩文字:

enter image description here

......也许我需要添加一个空单元格......

1 个答案:

答案 0 :(得分:2)

这样做的方法基本上是Update 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

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