簡體   English   中英

如何讓這個iTextSharp圖像“向右浮動”?

[英]How can I get this iTextSharp Image to “float right”?

我正在將圖像添加到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文件中:

[ 在此輸入圖像描述

但是,我需要將圖像(“僅限Office使用”矩形)磁化到右側,與左側顯示的雜色文本位於同一“行”,如下所示:

[ 在此輸入圖像描述

我怎樣才能做到這一點? 將“ScaleToFitLineWhenOverflow”設置為true無濟於事。 我也縮小了圖像本身的大小,但沒有區別 - 它只是采用較小的文件並將其膨脹回原來的相同(屏幕)大小。

UPDATE

一旦我記得實際加載較小的圖像(而不僅僅是檢查它的存在),圖像已被懲罰:

在此輸入圖像描述

......但它仍然在文本塊之下,而不是在右邊。

更新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中看到了這一點:

在此輸入圖像描述

...那么如何通過Twitter之前的引導將img拉出來以更好地與左邊的文本對齊? 我是否必須將兩張桌子包在另一張雙人桌子里?

  • 我認為,在反思中,只使用一個表,並使其成為一個兩列/單元格表(而不是將兩個單細胞變形蟲包裹,而不是將表格包裝到另一個表中)可能更有意義。

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

...但是現在圖像很小並且在桌子的第一個單元格中壓縮文本:

在此輸入圖像描述

...也許我需要添加一個空單元格或其他東西......

這樣做的方法基本上是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);
}

使用此代碼,圖像顯示幾乎與預想的一樣:

在此輸入圖像描述

注意:工作代碼基於我在此處找到的在線示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM