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 =...
The path is missing colon after drive letter. C:\Images...

(double backslashes is normally how VS shows strings escaped in debugger)
 
The path is missing colon after drive letter. C:\Images...

(double backslashes is normally how VS shows strings escaped in debugger)

Sorry, that was a typo. Here is the current path that I am using:
C:\Projects\RecipeManager\RecipeImages\Beef stew.png
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 = "C:\\Projects\\RecipeManager\\PDF Reports\\" + pdfReport;

            PdfWriter writer = new PdfWriter(pdfFilePath);
            PdfDocument pdf = new PdfDocument(writer);
            DataTable dtRecipeHead = DataHelper.RecipeHeader_Report(RecipeID);

            iText.Layout.Document document = new iText.Layout.Document(pdf);

            Paragraph header = new Paragraph("Recipe Book")
                .SetTextAlignment(TextAlignment.CENTER)
                .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
                .SetFontSize(20)
                .SetBold();

            string today = now.ToString("dd/MMMM/yyyy HH:mm");
            Paragraph subheader = new Paragraph(today)
                .SetTextAlignment(TextAlignment.CENTER)
                .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
                .SetFontSize(20)
                .SetBold();

            AreaBreak aBFirst = new AreaBreak();
            document.Add(aBFirst);

            foreach (DataRow row in dtRecipeHead.Rows)
            {

                iText.Layout.Element.Table IngrTable = new iText.Layout.Element.Table(3);
                iText.Layout.Element.Table methodTable = new iText.Layout.Element.Table(1);
               
                [I][B]string photoPath = row["RecipePhoto"].ToString();
                ImageData data = ImageDataFactory.Create(photoPath);
                // the full path from row["RecipePhoto"]  quoted above
                iText.Layout.Element.Image img = new iText.Layout.Element.Image(data);[/B][/I]
[B][I]                document.Add(img);[/I][/B]

                Paragraph parRecipe = new Paragraph(row["Description"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(20)
                    .SetBold();
                document.Add(parRecipe);
                Paragraph parRecipeType = new Paragraph(row["RecipeTypeName"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(16)
                    .SetBold();
                document.Add(parRecipeType);
                Paragraph parRecipeSource = new Paragraph(row["RecipeSource"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(14)
                    .SetBold();
                document.Add(parRecipeSource);
                Paragraph parRecipeTime = new Paragraph(row["PrepTime"].ToString() + " | " + row["CookingTime"])
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(10)
                    .SetBold();
                document.Add(parRecipeTime);
                string ID = row["RecipeID"].ToString();
                RName = row["Description"].ToString();
                //document.Add(table);
                DataTable dtRecipeIngedients = DataHelper.RecipeIngredient_Report(ID);
                //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                if (dtRecipeIngedients.Rows.Count > 0 )
                {
                    Paragraph ingheader = new Paragraph("Ingredients")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("Ingredients")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("Quantity")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("UOM")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(ingheader);
                }
                else
                {
                    Paragraph ingheader = new Paragraph("Ingredients - No data returned")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(ingheader);
                }
                foreach (DataRow ro in dtRecipeIngedients.Rows)
                {
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["Ingredients"].ToString())))
                        .SetFontSize(15);
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["Quantity"].ToString())))
                        .SetFontSize(15);
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["UOM"].ToString())))
                        .SetFontSize(15);
                }
                document.Add(IngrTable);
                DataTable dtMethods = DataHelper.RecipeMethod_Report(ID);
                if (dtMethods.Rows.Count > 0)
                {
                    Paragraph methHeader = new Paragraph("Methods")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    methodTable.AddHeaderCell(new Cell().Add(new Paragraph("Methods")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add (methHeader);
                }
                else
                {
                    Paragraph methHeader = new Paragraph("Methods - No data returned")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(methHeader);
                }
                foreach (DataRow rw in dtMethods.Rows)
                {
                    methodTable.AddCell(new Cell().Add(new Paragraph(rw["RecipeMethod"].ToString())))
                        .SetFontSize(15);
                }
                document.Add(methodTable);
                AreaBreak aB = new AreaBreak();
                document.Add(aB);
            }
            Process.Start(pdfFilePath);
            pdf.Close();
        }
 
What error are you getting?
 
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 = "C:\\Projects\\RecipeManager\\PDF Reports\\" + pdfReport;

            PdfWriter writer = new PdfWriter(pdfFilePath);
            PdfDocument pdf = new PdfDocument(writer);
            DataTable dtRecipeHead = DataHelper.RecipeHeader_Report(RecipeID);

            iText.Layout.Document document = new iText.Layout.Document(pdf);

            Paragraph header = new Paragraph("Recipe Book")
                .SetTextAlignment(TextAlignment.CENTER)
                .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
                .SetFontSize(20)
                .SetBold();

            string today = now.ToString("dd/MMMM/yyyy HH:mm");
            Paragraph subheader = new Paragraph(today)
                .SetTextAlignment(TextAlignment.CENTER)
                .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
                .SetFontSize(20)
                .SetBold();

            AreaBreak aBFirst = new AreaBreak();
            document.Add(aBFirst);

            foreach (DataRow row in dtRecipeHead.Rows)
            {

                iText.Layout.Element.Table IngrTable = new iText.Layout.Element.Table(3);
                iText.Layout.Element.Table methodTable = new iText.Layout.Element.Table(1);
              
                [I][B]string photoPath = row["RecipePhoto"].ToString();
                ImageData data = ImageDataFactory.Create(photoPath);
                // the full path from row["RecipePhoto"]  quoted above
                iText.Layout.Element.Image img = new iText.Layout.Element.Image(data);[/B][/I]
[B][I]                document.Add(img);[/I][/B]

                Paragraph parRecipe = new Paragraph(row["Description"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(20)
                    .SetBold();
                document.Add(parRecipe);
                Paragraph parRecipeType = new Paragraph(row["RecipeTypeName"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(16)
                    .SetBold();
                document.Add(parRecipeType);
                Paragraph parRecipeSource = new Paragraph(row["RecipeSource"].ToString())
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(14)
                    .SetBold();
                document.Add(parRecipeSource);
                Paragraph parRecipeTime = new Paragraph(row["PrepTime"].ToString() + " | " + row["CookingTime"])
                    .SetTextAlignment(TextAlignment.CENTER)
                    .SetFontSize(10)
                    .SetBold();
                document.Add(parRecipeTime);
                string ID = row["RecipeID"].ToString();
                RName = row["Description"].ToString();
                //document.Add(table);
                DataTable dtRecipeIngedients = DataHelper.RecipeIngredient_Report(ID);
                //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                if (dtRecipeIngedients.Rows.Count > 0 )
                {
                    Paragraph ingheader = new Paragraph("Ingredients")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("Ingredients")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("Quantity")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    IngrTable.AddHeaderCell(new Cell().Add(new Paragraph("UOM")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(ingheader);
                }
                else
                {
                    Paragraph ingheader = new Paragraph("Ingredients - No data returned")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(ingheader);
                }
                foreach (DataRow ro in dtRecipeIngedients.Rows)
                {
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["Ingredients"].ToString())))
                        .SetFontSize(15);
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["Quantity"].ToString())))
                        .SetFontSize(15);
                    IngrTable.AddCell(new Cell().Add(new Paragraph(ro["UOM"].ToString())))
                        .SetFontSize(15);
                }
                document.Add(IngrTable);
                DataTable dtMethods = DataHelper.RecipeMethod_Report(ID);
                if (dtMethods.Rows.Count > 0)
                {
                    Paragraph methHeader = new Paragraph("Methods")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    methodTable.AddHeaderCell(new Cell().Add(new Paragraph("Methods")))
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add (methHeader);
                }
                else
                {
                    Paragraph methHeader = new Paragraph("Methods - No data returned")
                        .SetTextAlignment(TextAlignment.LEFT)
                        .SetFontSize(15)
                        .SetBold();
                    document.Add(methHeader);
                }
                foreach (DataRow rw in dtMethods.Rows)
                {
                    methodTable.AddCell(new Cell().Add(new Paragraph(rw["RecipeMethod"].ToString())))
                        .SetFontSize(15);
                }
                document.Add(methodTable);
                AreaBreak aB = new AreaBreak();
                document.Add(aB);
            }
            Process.Start(pdfFilePath);
            pdf.Close();
        }

Problem solved! The problem was that row["RecipePhoto"].ToString() was empty on some rows. I added an if block to check for empty strings and then to skip the image portion.
 
Solution

Latest posts

Back
Top Bottom