C
C#15mo ago
Laz

✅ Need help with school project, premises is to convert and print WinForms into a PDF format

No description
5 Replies
Angius
Angius15mo ago
Anything PDF is a major, major PITA The best thing I found was QuestPDF There are some libraries to convert HTML to PDF as well I'm not aware of any libraries that would allow you to convert your Winform window into a PDF, though
Laz
LazOP15mo ago
private void WinFormToPDF_Click(object sender, EventArgs e)
{
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();

// Iterate through tabs
foreach (TabPage tab in Main_TabPage.TabPages)
{
// Create a new PDF page for each tab
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Add content to the PDF page
XFont font = new XFont("Arial", 12);
XBrush brush = XBrushes.Black;
gfx.DrawString("Data from WinForms:", font, brush, 100, 100);
}

// Save the PDF to a file
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string pdfFileName = Path.Combine(desktopPath, "ETTV CALCULATION PDF.pdf");
document.Save(pdfFileName);


System.Diagnostics.Process.Start(pdfFileName);
TaskDialog.Show("PDF Created", $"The information has been saved to {pdfFileName}");
System.Diagnostics.Process.Start(pdfFileName);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void WinFormToPDF_Click(object sender, EventArgs e)
{
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();

// Iterate through tabs
foreach (TabPage tab in Main_TabPage.TabPages)
{
// Create a new PDF page for each tab
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);

// Add content to the PDF page
XFont font = new XFont("Arial", 12);
XBrush brush = XBrushes.Black;
gfx.DrawString("Data from WinForms:", font, brush, 100, 100);
}

// Save the PDF to a file
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string pdfFileName = Path.Combine(desktopPath, "ETTV CALCULATION PDF.pdf");
document.Save(pdfFileName);


System.Diagnostics.Process.Start(pdfFileName);
TaskDialog.Show("PDF Created", $"The information has been saved to {pdfFileName}");
System.Diagnostics.Process.Start(pdfFileName);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
this is what i have come up with but no progress, the API runs winform pops up, but then unable to be saved and converted to PDF form, as in there is no PDF generated and i don't know whyh
Bailey
Bailey15mo ago
Hi, You are probably using pdf sharp. In this case just use google and search for pdfsharp multipage pdf. There are enough examples. Do it step by step. Adding text first step. Second adding pictures
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Laz
LazOP15mo ago
/close

Did you find this page helpful?