如何将此 iTextSharp 图像获取到 "float right"?
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 Use Only" 矩形)向右磁化,与左侧显示的杂色文本在相同 "row" 上,如下所示:
[
我该怎么做?将 "ScaleToFitLineWhenOverflow" 设置为 true 没有帮助。我还减小了图像本身的大小,但这没有任何区别 - 它只是将较小的文件放大并恢复到与之前相同的(屏幕)大小。
更新
一旦我记得实际上也 加载 较小的图像(而不是仅仅检查它的存在),图像就被惩罚了:
...但它仍然隐藏在文本块下方,而不是右侧。
更新 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
稍微调整一下代码(恢复为原始全尺寸图像,并将 table 的 WidthPercentage 从 35 更改为 45),我现在在生成的 PDF 中看到了这个:
...那么我如何通过它的 Twitter 之前的引导程序将 img 拉起来以更好地与左侧的文本对齐?我是否必须将两个 table 都包裹在另一个双单元 table 中?
- 经过深思熟虑,我认为只使用一个 table 并将其变成两个 column/cell table 可能更有意义(而不是包裹两个单细胞变形虫,我的意思是 tables,进入另一个 table)。
更新 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);
}
...但现在图像很小,并且在 table 的第一个单元格中挤压文本:
...也许我需要添加一个空单元格或其他内容...
方法基本上是更新 5 中的代码(用两个 cells/rows 制作一个 table,并将图像放在第二个存储罐中),但是百分比从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。
我正在向 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" 矩形)向右磁化,与左侧显示的杂色文本在相同 "row" 上,如下所示:
[
我该怎么做?将 "ScaleToFitLineWhenOverflow" 设置为 true 没有帮助。我还减小了图像本身的大小,但这没有任何区别 - 它只是将较小的文件放大并恢复到与之前相同的(屏幕)大小。
更新
一旦我记得实际上也 加载 较小的图像(而不是仅仅检查它的存在),图像就被惩罚了:
...但它仍然隐藏在文本块下方,而不是右侧。
更新 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
稍微调整一下代码(恢复为原始全尺寸图像,并将 table 的 WidthPercentage 从 35 更改为 45),我现在在生成的 PDF 中看到了这个:
...那么我如何通过它的 Twitter 之前的引导程序将 img 拉起来以更好地与左侧的文本对齐?我是否必须将两个 table 都包裹在另一个双单元 table 中?
- 经过深思熟虑,我认为只使用一个 table 并将其变成两个 column/cell table 可能更有意义(而不是包裹两个单细胞变形虫,我的意思是 tables,进入另一个 table)。
更新 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);
}
...但现在图像很小,并且在 table 的第一个单元格中挤压文本:
...也许我需要添加一个空单元格或其他内容...
方法基本上是更新 5 中的代码(用两个 cells/rows 制作一个 table,并将图像放在第二个存储罐中),但是百分比从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。