PrintTaskOptions throws Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

ManojKumar 0 Reputation points
2024-02-15T13:13:31.89+00:00

Hi Team, We are trying to invoke a print preview from the stream of PDF documents. While invoking the print preview we are receiving Catastrophic failure from the PrintTaskOptions while creating print preview pages.
We have tried the following methods to print from the application
https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/print-from-your-app Tools used VisualStudio 17 (v15.9.59) SDK version: Windows Creators Update (10.0; Build 15063) Windows version: 22H2(OS Build 19045.3930)

While using the print task options on CreatePrintPreview Pages we are receiving the PrintTaskOptions belowUser's image

private PdfDocument pdfDocument;
private int m_pageCount;
private Windows.UI.Xaml.Media.Imaging.BitmapImage pdfImage;
private PrintDocument printDocument;
private IPrintDocumentSource printDocumentSource;
private PrintManager printMan;
double marginWidth;
double marginHeight;
private PrintTask printTask;
public MainPage()
{
    this.InitializeComponent();
    Stream stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("PrintPriview.Assets.78b8fafd-48ab-4443-bce9-22803aa094bd_2.pdf");
    LoadPdfStream(stream);
}
private async void LoadPdfStream(Stream stream)
{
    pdfDocument = await PdfDocument.LoadFromStreamAsync(stream.AsRandomAccessStream());
    m_pageCount = ((int)pdfDocument.PageCount);
    pdfImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    Windows.Storage.Streams.InMemoryRandomAccessStream randomAccessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
    await pdfDocument.GetPage(0).RenderToStreamAsync(randomAccessStream);
    randomAccessStream.Seek(0);
    pdfImage.SetSource(randomAccessStream);
    ImageCanvas.Source = pdfImage;
    RegisterForPrint();
}
private void RegisterForPrint()
{
    printDocument = new PrintDocument();
    printDocumentSource = printDocument.DocumentSource;
    printDocument.Paginate += new PaginateEventHandler(CreatePrintPreviewPages);
    printDocument.GetPreviewPage += new GetPreviewPageEventHandler(GetPrintPreviewPage);
     printDocument.AddPages += new AddPagesEventHandler(AddPrintPages);
    printMan = PrintManager.GetForCurrentView();
    printMan.PrintTaskRequested -= new TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs>(PrintTaskRequested);
    printMan.PrintTaskRequested += new TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs>(PrintTaskRequested);
}
private void AddPrintPages(object sender, AddPagesEventArgs e)
{
    printDocument.AddPage(ImageCanvas);
    ApplicationLanguages.PrimaryLanguageOverride = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
}
private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
{
    PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions);
    PrintPageDescription pageDescription = printingOptions.GetPageDescription((uint)e.CurrentPreviewPageNumber);
    marginWidth = pageDescription.PageSize.Width;
    marginHeight = pageDescription.PageSize.Height;
    PrintDocument printDoc = (PrintDocument)sender;
    printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
{
    ApplicationLanguages.PrimaryLanguageOverride = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
    PrintDocument printDoc = (PrintDocument)sender;
    Image m_image = ImageCanvas;
    if (m_image != null)
        printDoc.SetPreviewPage(1, m_image);
}
private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
   string fileName = "PrintPreview.pdf";    
    printTask = e.Request.CreatePrintTask(fileName, sourceRequested => sourceRequested.SetSource(printDocumentSource));
    printMan.PrintTaskRequested -= new TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs>(PrintTaskRequested);
}
private async void PrintBtn_Click(object sender, RoutedEventArgs e)
{
    await PrintManager.ShowPrintUIAsync();
}


Could you please check and update the response? You can find the file that we used here: 78b8fafd-48ab-4443-bce9-22803aa094bd_2.pdf Regards,
Manoj

Universal Windows Platform (UWP)
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
764 questions
{count} votes