This is a migrated thread and some comments may be shown as answers.

How to load and print a pdf File?

1 Answer 2462 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Leigh
Top achievements
Rank 1
Leigh asked on 23 Jul 2020, 11:55 AM

I have a .net core 3.1 service application.

My company has bought the DevCraft subscription so I believe this gives me access to the Telerik document Process module.

Which nuget packages do I need to install to be able to load and print a PDF document?

I have found a few posts suggesting 'RadPdfViewer' is the class I need to get access to, but I cannot find a nuget package that allows this to be used in my project.

I found this post:- https://feedback.telerik.com/document-processing/1356074-documentprocessing-printing

But it doesnt compile and I cannot seem to be able to use this code snippet:-

            RadPdfViewer pdfViewer = new RadPdfViewer();
            PdfFormatProvider provider;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (Stream stream = File.OpenRead(fileLocation))
                {
                    stream.CopyTo(memoryStream);
                }
                
                provider = new PdfFormatProvider(memoryStream, FormatProviderSettings.ReadOnDemand);
                pdfViewer.Document = provider.Import();

                string fileName = new FileInfo(fileLocation).Name;
                PrintSettings printSettings = new PrintSettings() { DocumentName = fileName };

                pdfViewer.Print(dialog, printSettings);
            }

Would love some assistance on this please, thank you.

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 24 Jul 2020, 10:19 AM

Hi Leigh,

Yes, the document processing library is included. 

Please note that RadPdfViewer is UI component used with the WPF suite. When using .Net Core you are not allowed to add references to PesentationFramework assembly when the project type differs from a desktop app. The possible solution is to manually edit the project file to change the used framework. I have attached a small sample that shows this along with which Telerik assemblies are required. 

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Scott
Top achievements
Rank 1
Iron
commented on 11 Oct 2022, 08:17 PM

Dimitar,

I have a similar situation with printing.  I tried to load your example, but it will not compile.  I get a bunch of errors saying "The type or namespace name 'Telerik' could not be found".  Seeing as how this is from 2020, have things changed, is there a more current example?

Thanks,
- Scott

Dimitar
Telerik team
commented on 12 Oct 2022, 09:19 AM

Hi Scott, 

Nothing has changed and the example is still valid. We cannot attach assemblies here and they are removed from the project. You need to manually add the missing references. You can use assemblies from the product installation folder or NuGet packages

I hope this helps. Should you have any other questions do not hesitate to ask.
Scott
Top achievements
Rank 1
Iron
commented on 31 Oct 2022, 09:33 PM

Dimitar,

In my situation I have a web API that needs to send documents to the printer.  I was able to get this working in WinForms using this PDF Silent Print - Telerik UI for WinForms example.  However, I require being able to do this from an API.  Is this possible?

Thanks,
- Scott

Tanya
Telerik team
commented on 03 Nov 2022, 11:28 AM

Hello Scott,

You can create a separate service to print the document. Using the example you have found, you will be able to print the document without showing the control in the UI.

Hope this is helpful.

Scott
Top achievements
Rank 1
Iron
commented on 03 Nov 2022, 04:04 PM

Tanya,
As I mentioned in my previous post, this is a WebAPI project...there is NO UI. 

Using the PDF Silent Print example I mentioned previously, I get the following error at the line "rViewer.LoadElementTree();".

System.InvalidOperationException
  HResult=0x80131509
  Message=An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
  Source=Telerik.WinControls.PdfViewer

Please advise.

Thanks,
-Scott

Dess | Tech Support Engineer, Principal
Telerik team
commented on 04 Nov 2022, 09:34 AM

Hi Scott, your question has already been answered in the other thread (1585873) you have opened on the same topic. However, I am posting the answer here as well in order the community to benefit from it.

We kindly ask you to use just one thread for a specific problem to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.Thank you for your understanding.

The referred KB article is the suitable approach to print the document loaded in RadPdfViewer without any UI. However, RadPdfViewer loads the document asynchronously when using the LoadDocument method. It is not possible to print the document before it is loaded. That is why the DocumentLoaded event is used in the example which is expected to be fired when the BackgroundWorker used by RadPdfViewer is completed with the document loading. Then, you can execute the desired action with the exact document and print it. 

I am not familiar with the complete scenario and setup that you have. But according to the provided information, it seems that the asynchronous pdf loading can't be executed in the specific environment that you are using.

Even though it is possible to set the PdfViewerElement.AsyncRender property to false and this will disable the async rendering of pages, the LoadDocument method still runs a BackgroundWorker asynchronously. However, it is possible to import the file by using the Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider and the produced RadFixedDocument can be set to the RadPdfViewer.Document property. Thus, the asynchronous loading will be eliminated. However, according to the document, the import action may require some time to import. Please give this approach a try and see how it works on your end.

            var provider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
            string filePath = @"..\..\Sample.pdf";
            Telerik.Windows.Documents.Fixed.Model.RadFixedDocument doc = provider.Import(File.ReadAllBytes(filePath));
            var radPdfViewer = new RadPdfViewer(); 
            radPdfViewer.Document = doc; 
            radPdfViewer.PrintOrientation = PrintOrientation.Landscape; 
            radPdfViewer.PdfViewerElement.AsyncRender = false;
            using (var document = new RadPrintDocument { Landscape = false, DocumentName = Path.GetFileName(filePath) })
            {
                document.PrintController = new StandardPrintController();  

                document.AssociatedObject = radPdfViewer.PdfViewerElement;

                var settings = new PrinterSettings
                {
                    // PrinterName = printerName,
                    Copies = 1,
                    Duplex = Duplex.Simplex,
                    PrintRange = PrintRange.AllPages
                };

                document.DefaultPageSettings = new PageSettings
                {
                    PrinterSettings = settings,
                    Margins = new Margins(0, 0, 0, 0)
                };
                document.PrinterSettings = settings;

                document.Print();
            }

 

Scott
Top achievements
Rank 1
Iron
commented on 04 Nov 2022, 04:13 PM | edited

Dess,
My apologies.  Yes, I had posted this here first but Tanya seemed to miss the point so I posted the error message I was receiving so that others might benefit from any answers.   I later posted a more complete and specific question to support.  It's my understanding that support tickets are not visible to the Community so thank you for posting that response back here so the community could benefit.

Tags
PdfProcessing
Asked by
Leigh
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or