John Malkovich
John Malkovich

Reputation: 55

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

Upvotes: 1

Views: 1415

Answers (1)

MOckjer
MOckjer

Reputation: 621

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

Upvotes: 1

Related Questions