Add the required using statements:
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System.Drawing.Printing;
Create a new PdfDocument object:
var doc = new PdfDocument();
Add a new page to the document:
var page = doc.AddPage();
Create a graphics object to draw on the page:
var gfx = XGraphics.FromPdfPage(page);
Add text or graphics to the page using the graphics object:
gfx.DrawString("Hello, World!", new XFont("Verdana", 20), XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
Create a PrintDocument object:
var printDoc = new PrintDocument();
Set the printer name and page settings:
printDoc.PrinterSettings.PrinterName = "<printer name>";
printDoc.DefaultPageSettings.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom", (int)page.Width.Point, (int)page.Height.Point);
Attach an event handler to the PrintPage event:
printDoc.PrintPage += (sender, args) =>
{
var stream = new MemoryStream();
doc.Save(stream);
var bytes = stream.ToArray();
args.Graphics.DrawImage(Image.FromStream(new MemoryStream(bytes)), args.PageBounds);
};
Print the document:
printDoc.Print();
Dispose of the graphics and document objects:
gfx.Dispose();
doc.Dispose();
Note: You may need to adjust the code to suit your specific use case.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-08-11 11:00:00 +0000
Seen: 11 times
Last updated: Jun 26 '22
What are the steps to restrict the overall file size of uploaded files in NestJS using multer?
How do I delete my Git credentials?
How can one open the "What's New" window in IntelliJ?
What are the steps to complete the merge process once I have resolved conflicts?
What is the method to obtain the public URL of a file in TYPO3?
What are the steps to set up TA-lib on Google Colab?
What are the steps to configure a gstreamer v4l2src stream-mmap?
What are the steps to create a fixed right-hand side bar on a modal using tailwindcss?