[英]How can I get the resolution of an embeded image in a PDF using 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無濟於事。 我也縮小了圖像本身的大小,但沒有區別 - 它只是采用較小的文件並將其膨脹回原來的相同(屏幕)大小。
一旦我記得實際加載較小的圖像(而不僅僅是檢查它的存在),圖像已被懲罰:
......但它仍然在文本塊之下,而不是在右邊。
我試圖實現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);
}
...但現在圖像根本不顯示。 錘子是什么? 什么鏈?
好的,這種作品(仍然需要調整):
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
通過一些代碼調整(恢復到原始的全尺寸圖像,並將表格的WidthPercentage從35更改為45),我現在在生成的PDF中看到了這一點:
...那么如何通過Twitter之前的引導將img拉出來以更好地與左邊的文本對齊? 我是否必須將兩張桌子包在另一張雙人桌子里?
距離最新代碼更近一步:
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.