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.