C
C#8mo ago
Laz

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

No description
5 Replies
Angius
Angius8mo 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
Laz8mo 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
Bailey8mo 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
Accord8mo 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
Laz8mo ago
/close
Want results from more Discord servers?
Add your server
More Posts
❔ How to call static virtual interface method without completely reimplementing in derived classThis mainly pertains to generic math in C# 11 / .NET 7, but say I have a minimum of something like t❔ [Avalonia] Basic question regarding subclassingLet's say I have some types like so: ```cs public abstract class Foo {} public class FooA: Foo {} ✅ How to display an image avalonia```xml <Image Source="{Binding ImagePath}" /> ``````cs public static string? _imagePath; public View❔ xUnit tests```csharp using System; using System.IO; namespace TestWordCount; public class WordCounterTests { ✅ Using a scoped service within an IHostedServiceI have a IHostedService implementation that sends a Mediatr request every hour. The handler uses a D❔ Please point me in the right direction. WPF + Windows Service app.I'm quite new to C# but I already have a good bit of experience programming Python, JavaScript and a❔ I now writing auth for my api and I want to know can I make it better```cs using System.Security.Claims; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Auth❔ Design questionHi! I'm a beginner and I'm having trouble finding a decent design for this toy application I'm makin❔ Deploying an MVC projectI know this is a C# server but it's related so I hope it's okay... I've published a standard MVC pr❔ Blazor - MarkupString rendering <img> tag w/ base64 image encoding as textAs can be seen in the dev tools, there are quotation marks around the <img> tag when rendered in-bro