简体   繁体   中英

C# - Adding multiple pages to PDF file using iTextSharp

i'm having troubles with my code. I've been looking in StackOverflow here, but it seems the examples doesn't apply to my code.

I've been trying to generate a pdf file with multiple pages but I can't find a way to make it work.

I mean, with the code as is it now, it generates multiple pdf files with the correct data.

Could you help me?

 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(); }

First of all change Document Document = new Document(PageSize.A4, 0f, 0f, 15f, 0f); to Document document = new Document(PageSize.A4, 0f, 0f, 15f, 0f); .

Let's figure it out step by step.

You can't set an instance name as it's class name. Like this:

 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();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM