Question Create PDF document from data

Manie Verster

Member
Joined
Nov 22, 2023
Messages
22
Programming Experience
10+
Create PDF Document:
    string pdfReport = "";
    DateTime now = DateTime.Now;
    if (RecipeID == "0")
    {
        pdfReport += "Recipe_All_" + now.ToString("yyyyMMddHHmmss") + ".pdf";
    }
    else
    {
        pdfReport += "Recipe_" + RecipeName + "_" + now.ToString("yyyyMMddHHmmss") + ".pdf";
    }
    string pdfFilePath = "C:\\Projects\\RecipeManager\\PDF Reports\\" + pdfReport;
    if (File.Exists(pdfFilePath));
    {
        File.Delete(pdfFilePath);
    }
    PdfWriter writer = new PdfWriter(pdfFilePath);
    PdfDocument pdf = new PdfDocument(writer);
    DataTable dtRecipeHead = DataHelper.RecipeHeader_Report(RecipeID);
    if (dtRecipeHead.Rows.Count > 0)
    {
        Document document = new Document(pdf);

        Table table = new Table(6);
        //myImage = System.Drawing.Image.FromFile(photoPath);

        table.AddHeaderCell(new Cell().Add(new Paragraph("Description")));
        table.AddHeaderCell(new Cell().Add(new Paragraph("Category")));
        table.AddHeaderCell(new Cell().Add(new Paragraph("Serving Size")));
        table.AddHeaderCell(new Cell().Add(new Paragraph("Source")));
        table.AddHeaderCell(new Cell().Add(new Paragraph("Prep Time")));
        table.AddHeaderCell(new Cell().Add(new Paragraph("Cooking Time")));

        foreach (DataRow row in dtRecipeHead.Rows)
        {
            table.AddCell(new Cell().Add(new Paragraph(row["Description"].ToString())));
            table.AddCell(new Cell().Add(new Paragraph(row["RecipeTypeName"].ToString())));
            table.AddCell(new Cell().Add(new Paragraph(row["ServingSize"].ToString())));
            table.AddCell(new Cell().Add(new Paragraph(row["RecipeSource"].ToString())));
            table.AddCell(new Cell().Add(new Paragraph(row["PrepTime"].ToString())));
            table.AddCell(new Cell().Add(new Paragraph(row["CookingTime"].ToString())));
        }

        document.Add(table);
    }
    writer.Close();
    pdf.Close();
}

I am trying to create a PDF document but because I know very little about this subject I am willing to learn from someone who knows better and can tell me what I am doing wrong. When I open the document after creating it, I get an error message stating the file is corrupt. Can someone help me, please?

I Googled this intensively but my code looks exactly the same as theirs.
 
Solution
Sorry, that was a typo. Here is the current path that I am using:

I do not see anything wrong with that file path but I might miss something. Below is the code that I am using to create the pdf document.

C#:
        private void Export_PDF(string RecipeID, string RecipeName)
        {
            string pdfReport = "";
            string RName = "";
            DateTime now = DateTime.Now;
            if (RecipeID == "0")
            {
                pdfReport += "Recipe_All_" + now.ToString("yyyyMMddHHmmss") + ".pdf";
            }
            else
            {
                pdfReport += "Recipe_" + RecipeName + "_" + now.ToString("yyyyMMddHHmmss") + ".pdf";
            }
            string pdfFilePath =...
I recommend starting small and just try to create a PDF that says "Hello, World" in it. If that succeeds then that suggest that the issue is with your table in PDF creation. If that fails, then that suggests you have some other configuration issue in your use of iText7.

Moving this thread out the WinForms forum since there is nothing WinForms specific about this question.
 
Line 12 if statement should remove ;
Line 45 should be removed, closing the document on line 46 also closes the writer it uses (unless SetCloseWriter(false) was called). Throws exception otherwise.
 
I recommend starting small and just try to create a PDF that says "Hello, World" in it. If that succeeds then that suggest that the issue is with your table in PDF creation. If that fails, then that suggests you have some other configuration issue in your use of iText7.

Moving this thread out the WinForms forum since there is nothing WinForms specific about this question.

Sorry, I am using this code in a Winforms application.
 
Your pdf question/problem is not related to it being winform project or any other project type.
 
Line 12 if statement should remove ;
Line 45 should be removed, closing the document on line 46 also closes the writer it uses (unless SetCloseWriter(false) was called). Throws exception otherwise.

Hi JohnH,

Your suggestions helped me out. Thank you!!

I have another problem with the same code but something I added after my question. Googled it a lot and got either the same code or code that would not work.

C#:
document.Add([B]Layout[/B].Element.Image, myImage)

I will show the error I got and I just know too little to know what to do.

1700737099565.png


The potential fixes also did not work.
 
I found an example on the internet to create image element:
C#:
var el = New Element.Image(ImageDataFactory.Create("file path"));
 
Not working yet.

Describe "not working". Is there a compiler error? Is there a time error? Are you getting unexpected output? No output? An employee that is just coffee-badging or quiet quitting?
 
Describe "not working". Is there a compiler error? Is there a time error? Are you getting unexpected output? No output? An employee that is just coffee-badging or quiet quitting?

C#:
var el = New Element.Image(ImageDataFactory.Create(myImage));

1700808289519.png


This is the error I get. The reason I said what I said was that I decided to leave this issue for now and finish the rest of my report. Sorry again, I should have said that. No employees or employers here. I am self-employed and in the process of learning new things. I recently finished a C# course and trying to develop a desktop application that I hope to sell and I do this on the side of my other work. I have, since I started on the C# route only had issues and tried to deal with it on my own because when you learn something like that it sticks.

I appreciate all the help I get and thank you for your help in this matter. My personal opinion is that I most probably need to install another Nuget package but have no idea what.
 
C#:
var el = New Element.Image(ImageDataFactory.Create(myImage));

View attachment 3046

This is the error I get. The reason I said what I said was that I decided to leave this issue for now and finish the rest of my report. Sorry again, I should have said that. No employees or employers here. I am self-employed and in the process of learning new things. I recently finished a C# course and trying to develop a desktop application that I hope to sell and I do this on the side of my other work. I have, since I started on the C# route only had issues and tried to deal with it on my own because when you learn something like that it sticks.

I appreciate all the help I get and thank you for your help in this matter. My personal opinion is that I most probably need to install another Nuget package but have no idea what.

C#:
var el = [B]Element.Image[/B](ImageDataFactory.Create("file path"));

I removed the new keyword from the line of code and now got a new error which says:
'Element' does not contain a definition for 'image'.
 
I removed the new keyword from the line of code and now got a new error which says:
Why on earth would you do that? Just remove the whole line and problem be gone, yeah?
 
Why on earth would you do that? Just remove the whole line and problem be gone, yeah?

No need to be sarcastic now. I have tried everything already and just wanted to see what happens if I do that. I WILL remove the whole image code and give it up for a bad idea!!!
 
Hi there,
I am back on this issue and solved part of my problem. For anyone battling with the same issue, here is my solution:

C#:
string photoPath = row["RecipePhoto"].ToString();
ImageData data = ImageDataFactory.Create(photoPath);
iText.Layout.Element.Image img = new iText.Layout.Element.Image(data);
document.Add(img);

I have a new issue with this same problem. my photoPath variable looks like this: C\\Images\\image1.jpg and I get a path format is invalid error. Any suggestions?
 

Latest posts

Back
Top Bottom