添加 Table 到 PdfStamper.AcroFields iTextSharp
Add Table to PdfStamper.AcroFields iTextSharp
我开始使用 iTextSharp,但在做一些非常简单的事情时遇到了麻烦。我有一个用 PdfStamper 填充的模板,就像这样
PdfReader rdr = new PdfReader(@"C:\temp\Template.pdf");
PdfStamper stamper = new PdfStamper(rdr, new System.IO.FileStream(path = @"C:\temp\Created.pdf", System.IO.FileMode.Create));
stamper.AcroFields.SetField("softwarename", "Software");
stamper.FormFlattening = true;
AcroFields form = stamper.AcroFields;
form.GenerateAppearances = true;
stamper.Close();
rdr.Close();
我希望能够有一个名为 "grid" 或类似名称的字段,并用一个格式完整的 PdfPTable 填充它(我知道我可以用字符串完成所有操作,但我不'想要那个)。有更好的方法吗?
PDFStamper 文档说它接受所有允许的对象,所以我会结合使用对象(或对象列表)和标志。这样你就可以传递你需要的所有信息,并根据你需要的任何逻辑对其进行操作。
以下代码将使用 PdfPTable 将图像均匀地放置在页面顶部的网格中。
然后就可以使用PdfTemplate,使用WriteSelectedRows方法将PdfPTable的内容写入其中
PdfPTable table;
string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input.pdf");
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.pdf");
List<ImageTextSharpModel> images
PdfPTable table = new PdfPTable(images.Count);
table.WidthPercentage = 100;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
for (var i = 0; i < images.Count; i++)
{
var image = images[i];
try
{
PdfPCell cell = new PdfPCell();
cell.BorderWidth = 0;
cell.FixedHeight = image.Height;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
Paragraph p = new Paragraph();
float offset = 0;
var img = iTextSharp.text.Image.GetInstance(image.AbsolutePath);
img.ScaleToFit(image.Width, image.Height);
//Manually setting the location
if (image.Alignment == iTextSharp.text.Image.RIGHT_ALIGN)
{
offset = i == 0
? (((doc.PageSize.Width/images.Count) - doc.LeftMargin) - img.ScaledWidth)
: (((doc.PageSize.Width/images.Count) - doc.LeftMargin) - img.ScaledWidth) - cell.Width;
}
else if (image.Alignment == iTextSharp.text.Image.ALIGN_CENTER)
{
if (images.Count == 1)
{
offset = ((doc.PageSize.Width - img.ScaledWidth)/2) - doc.LeftMargin;
}
else
{
offset = (((doc.PageSize.Width/images.Count) - img.ScaledWidth)/2);
}
}
p.Add(new Chunk(img, offset, 0));
cell.AddElement(p);
table.AddCell(cell);
}
catch (Exception ex)
{
//Ignore
}
}
;
//add table to stamper
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(inputFile);
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(pdfReader, fs))
{
int PageCount = pdfReader.NumberOfPages;
for (int x = 1; x <= PageCount; x++)
{
PdfContentByte canvas = stamper.GetOverContent(x);
PdfTemplate tableTemplate = canvas.CreateTemplate(1500, 1300);
table.WriteSelectedRows(0, -1, 0, 1300, tableTemplate);
}
stamper.Close();
}
}
我开始使用 iTextSharp,但在做一些非常简单的事情时遇到了麻烦。我有一个用 PdfStamper 填充的模板,就像这样
PdfReader rdr = new PdfReader(@"C:\temp\Template.pdf");
PdfStamper stamper = new PdfStamper(rdr, new System.IO.FileStream(path = @"C:\temp\Created.pdf", System.IO.FileMode.Create));
stamper.AcroFields.SetField("softwarename", "Software");
stamper.FormFlattening = true;
AcroFields form = stamper.AcroFields;
form.GenerateAppearances = true;
stamper.Close();
rdr.Close();
我希望能够有一个名为 "grid" 或类似名称的字段,并用一个格式完整的 PdfPTable 填充它(我知道我可以用字符串完成所有操作,但我不'想要那个)。有更好的方法吗?
PDFStamper 文档说它接受所有允许的对象,所以我会结合使用对象(或对象列表)和标志。这样你就可以传递你需要的所有信息,并根据你需要的任何逻辑对其进行操作。
以下代码将使用 PdfPTable 将图像均匀地放置在页面顶部的网格中。
然后就可以使用PdfTemplate,使用WriteSelectedRows方法将PdfPTable的内容写入其中
PdfPTable table;
string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input.pdf");
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.pdf");
List<ImageTextSharpModel> images
PdfPTable table = new PdfPTable(images.Count);
table.WidthPercentage = 100;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
for (var i = 0; i < images.Count; i++)
{
var image = images[i];
try
{
PdfPCell cell = new PdfPCell();
cell.BorderWidth = 0;
cell.FixedHeight = image.Height;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
Paragraph p = new Paragraph();
float offset = 0;
var img = iTextSharp.text.Image.GetInstance(image.AbsolutePath);
img.ScaleToFit(image.Width, image.Height);
//Manually setting the location
if (image.Alignment == iTextSharp.text.Image.RIGHT_ALIGN)
{
offset = i == 0
? (((doc.PageSize.Width/images.Count) - doc.LeftMargin) - img.ScaledWidth)
: (((doc.PageSize.Width/images.Count) - doc.LeftMargin) - img.ScaledWidth) - cell.Width;
}
else if (image.Alignment == iTextSharp.text.Image.ALIGN_CENTER)
{
if (images.Count == 1)
{
offset = ((doc.PageSize.Width - img.ScaledWidth)/2) - doc.LeftMargin;
}
else
{
offset = (((doc.PageSize.Width/images.Count) - img.ScaledWidth)/2);
}
}
p.Add(new Chunk(img, offset, 0));
cell.AddElement(p);
table.AddCell(cell);
}
catch (Exception ex)
{
//Ignore
}
}
;
//add table to stamper
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(inputFile);
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(pdfReader, fs))
{
int PageCount = pdfReader.NumberOfPages;
for (int x = 1; x <= PageCount; x++)
{
PdfContentByte canvas = stamper.GetOverContent(x);
PdfTemplate tableTemplate = canvas.CreateTemplate(1500, 1300);
table.WriteSelectedRows(0, -1, 0, 1300, tableTemplate);
}
stamper.Close();
}
}