filmov
tv
Trying to print base64 document fails with 'parameter is not valid'

Показать описание
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Trying to print base64 document fails with "parameter is not valid"
I am trying this:
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();
}
}
}
}
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.MSource of the question:
Question and source license information: