为了账号安全,请及时绑定邮箱和手机立即绑定

C# - 使用 iTextSharp 将多个页面添加到 PDF 文件

C# - 使用 iTextSharp 将多个页面添加到 PDF 文件

C#
婷婷同学_ 2022-09-04 16:37:04
我的代码有问题。我一直在这里寻找StackOverflow,但似乎这些例子不适用于我的代码。我一直在尝试生成一个包含多个页面的pdf文件,但我找不到使其工作的方法。我的意思是,使用现在的代码,它会生成多个具有正确数据的pdf文件。你能帮我吗? foreach (var item in ListCars.OrderBy(x => x.Destiny))                {                    Document Document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);                    Image Img = null;                    FileStream fsData = null;                    Img = Image.GetInstance(Properties.Resources.CMODEL, System.Drawing.Imaging.ImageFormat.Png);                    Img.ScaleToFit(PageSize.A4);                    Img.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER;                    string DataForTest = "";                    string PDFName = "TEST - " + item.Vin + ".PDF";                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\");                    fsData = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName, FileMode.Create);                    PdfWriter writer = PdfWriter.GetInstance(Document, fsData);                    Document.Open();                    PdfContentByte cb = writer.DirectContent;                    ColumnText ct = new ColumnText(cb);                    Phrase DataForTestT = new Phrase(DataForTest, FontFactory.GetFont("IMPACT", 8));                    ct.SetSimpleColumn(DataForTestT, 115, 824, 561, 307, 8, Element.ALIGN_LEFT);                    ct.Go();                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName);                    Document.Add(Img);                    Document.AddCreationDate();                    Document.Close();                }
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

首先更改为 .Document Document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);Document document = new Document(PageSize.A4, 0f, 0f, 15f, 0f);


让我们一步一步弄清楚。


不能将实例名称设置为其类名。喜欢这个:


 foreach (var item in ListCars.OrderBy(x => x.Destiny))

                {

                    Document document= new Document(PageSize.A4, 0f, 0f, 15f, 0f);

                    Image Img = null;

                    FileStream fsData = null;

                    Img = Image.GetInstance(Properties.Resources.CMODEL, System.Drawing.Imaging.ImageFormat.Png);


                    Img.ScaleToFit(PageSize.A4);

                    Img.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER;

                    string DataForTest = "";

                    string PDFName = "TEST - " + item.Vin + ".PDF";


                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\");

                    fsData = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName, FileMode.Create);

                    PdfWriter writer = PdfWriter.GetInstance(document, fsData);


                    document.Open();


                    PdfContentByte cb = writer.DirectContent;

                    ColumnText ct = new ColumnText(cb);


                    Phrase DataForTestT = new Phrase(DataForTest, FontFactory.GetFont("IMPACT", 8));

                    ct.SetSimpleColumn(DataForTestT, 115, 824, 561, 307, 8, Element.ALIGN_LEFT);

                    ct.Go();


                    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Rems\Pages\" + PDFName);

                    document.Add(Img);

                    document.AddCreationDate();

                    document.Close();

                }


查看完整回答
反对 回复 2022-09-04
  • 1 回答
  • 0 关注
  • 158 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信