1

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

3
  • 1
    stackoverflow.com/questions/11307749/… Commented Feb 11, 2019 at 15:47
  • Could you elaborate a little more? I'm kinda new with all this stuff. Commented Feb 11, 2019 at 15:48
  • Please post the error. What happens? Crashes? Doesn't add pages? Commented Feb 11, 2019 at 15:50

1 Answer 1

1

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();
                }
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't crash, just ignore the code completely. I'll do now what you told me there. Ah i know what you mean there, that was a typo when I was writing the code here.
Also don't forget to switch the other Documents in the necessary places. Paste what i posted.
Nono, that was just a typo when I was writing the code here. Don't mind it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.