Код: Выделить всё
try
{
using (var memoryStream = new MemoryStream(pdfBytes))
{
// Load the PDF document
PdfSharp.Pdf.PdfDocument pdfDocument = PdfReader.Open(memoryStream, PdfDocumentOpenMode.ReadOnly);
// Print each page
for (int i = 0; i < pdfDocument.PageCount; i++)
{
using (var printDocument = new PrintDocument())
{
printDocument.PrinterSettings.PrinterName = printerName;
// Handle the PrintPage event
printDocument.PrintPage += (sender, e) =>
{
PdfSharp.Events.RenderEvents renderEvents = new PdfSharp.Events.RenderEvents();
XGraphics graphics = XGraphics.FromGraphics(e.Graphics, new XSize(e.MarginBounds.Width, e.MarginBounds.Height), renderEvents);
// Get the current page to print
var page = pdfDocument.Pages[i];
var pageWidth = page.Width;
var pageHeight = page.Height;
// Calculate the scale to fit the page
var scaleX = e.MarginBounds.Width / pageWidth;
var scaleY = e.MarginBounds.Height / pageHeight;
var scale = Math.Min(scaleX, scaleY);
// Center the page on the printer
var xPos = e.MarginBounds.Left + (e.MarginBounds.Width - (pageWidth * scale)) / 2;
var yPos = e.MarginBounds.Top + (e.MarginBounds.Height - (pageHeight * scale)) / 2;
// Draw the content of the PDF page
XImage xImage = XImage.FromStream(memoryStream);
graphics.DrawImage(xImage, xPos, yPos, pageWidth * scale, pageHeight * scale);
// Draw rectangle for the page border
graphics.DrawRectangle(XPens.Black, xPos, yPos, pageWidth * scale, pageHeight * scale);
};
// Trigger the print job
printDocument.Print();
}
}
}
}
Недопустимый параметр
Теперь я предполагаю, что это происходит из-за событий рендеринга, которые я оставил пустыми, но я не смог найти ничего, что хотелось бы вставить сюда; но, возможно, проблема вовсе не в этом.
У кого-нибудь есть какие-нибудь советы?
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/790 ... -not-valid
Мобильная версия