我将图像添加到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 Use Only"矩形)磁化到右侧,同一行"行"如左边显示的杂色文字,如:
[
我该怎么做?设置" ScaleToFitLineWhenOverflow"真的没有帮助。我也缩小了图像本身的大小,但没有区别 - 它只是采用较小的文件并将其膨胀回原来的相同(屏幕)大小。
一旦我记得实际加载较小的图像(而不仅仅是检查它的存在),图像就会受到惩罚:
......但它仍然在文本块之下,而不是在右边。
我试图实现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);
}
...但现在图片很小,并且在桌子的第一个单元格中压缩文字:
......也许我需要添加一个空单元格......
答案 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);
}
使用此代码,图像显示的几乎与想象的一样:
注意:工作代码基于我找到here的在线示例。