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();
}
}
PDF는 표 양식 요소를 알지 못합니다. 대신 각 셀에 양식 요소가 포함 된 표를 작성할 수 있습니다. 또는 XFA 양식을 살펴보십시오. 그들은 당신의 기대를 충족시키지 못하지만 그렇게하지는 못합니다. – mkl