首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向PdfStamper.AcroFields iTextSharp添加表

向PdfStamper.AcroFields iTextSharp添加表
EN

Stack Overflow用户
提问于 2016-08-15 22:31:22
回答 1查看 3.1K关注 0票数 1

我刚开始使用iTextSharp,但在做一些非常简单的事情时遇到了麻烦。我有一个用PdfStamper填充的模板,如下所示

代码语言:javascript
复制
 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();     

我希望能够有一个名为“PdfPTable”或类似的字段,并用一个完整格式的PdfPTable填充它(我知道我可以在字符串中完成所有这些操作,但我不想这样做)。有没有更好的方法来做这件事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-16 01:59:00

PDFStamper文档说它接受所有允许的对象,所以我将使用对象(或对象列表)和标志的组合。这样你就可以传递你需要的所有信息,并根据你需要的任何逻辑对其进行操作。

下面的代码将使用PdfPTable将图像均匀地放置在页面顶部的网格中。

然后,您可以使用PdfTemplate并使用WriteSelectedRows方法将PdfPTable的内容写入其中。

代码语言:javascript
复制
        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();
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38957109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档