print pdf in csharp

You can use the iTextSharp library to generate a PDF and the PrintDocument class from System.Drawing.Printing to print it. Here's an example:

main.cs
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Printing;

// create a PDF document using iTextSharp
var document = new Document();
var writer = PdfWriter.GetInstance(document, new FileStream("myPdf.pdf", FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello, world!"));
document.Close();

// create a PrintDocument and specify the printer name
var printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = "Adobe PDF";

// handle the PrintPage event and draw the PDF onto the page
printDoc.PrintPage += (sender, args) =>
{
    var pdfReader = new PdfReader("myPdf.pdf");
    var pdfPage = pdfReader.GetPageN(1);
    var pdfSize = pdfReader.GetPageSizeWithRotation(1);
    args.Graphics.DrawImage(pdfPage, args.MarginBounds);
};

// print the document
printDoc.Print();
822 chars
27 lines

gistlibby LogSnag