Image is not imported/exported from PDF file

4 posts, 0 answers
  1. Maurício
    Maurício avatar
    41 posts
    Member since:
    Jan 2011

    Posted 01 Feb Link to this post

    Hello 

     The following method is supposed to import a PDF file and export it. It works as expected, except that the image is lost. 

    private void Test1()
          {
              var provider = new PdfFormatProvider();
     
              var stream = File.OpenRead(@"C:\inputFile.pdf");
     
              var document = provider.Import(stream);
     
              using (var output = new FileStream(@"C:\outputFile.pdf", FileMode.OpenOrCreate))
              {
                  provider.Export(document, output);
              }
          }

     

     You might say that the file is in bad format or something, but why would this work?

    private void Test2()
        {
            var stream = new FileStream(@"C:\inputFile.pdf", FileMode.Open, FileAccess.Read);
     
            var source = new PdfDocumentSource(stream, FormatProviderSettings.ReadOnDemand);
     
            ViewModel.DocumentSource = source;
        }

    DocumentSource is binded to the PdfViewer.DocumentSource.

     

    Here is the link to the PDF inputFile: https://goo.gl/3lpe9O

     Thank you

  2. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 04 Feb Link to this post

    Hi Maurício,

    The PdfFormatProvider class is shared between RadPdfProcessing and RadPdfViewer and it is important which of the provider's constructor overloads you are using. If I correctly understand, you are trying to use RadPdfViewer. The corresponding PdfFormatProvider could be created using the constructor that takes two parameters:
    PdfFormatProvider provider = new PdfFormatProvider(stream, settings);

    For more information on this topic, you could refer to the Showing a File article in our documentation.

    However, RadPdfViewer is intended only to display PDF files and doesn't support exporting them.

    Hope this is helpful.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Maurício
    Maurício avatar
    41 posts
    Member since:
    Jan 2011

    Posted 04 Feb in reply to Tanya Link to this post

    Hello Tanya!

     I think I wasn't clear

    I will explain directly what I am trying to do:

    I have two different PDF files in my hard drive. I want them to become a single RadFixedDocument so I can set it to the PdfViewer.DocumentSource.

    Here's what I am doing:

        private static RadFixedDocument LoadDocument(string file)
           {
               var provider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
               using (Stream stream = File.OpenRead(file))
               {
                   return provider.Import(stream);
               }
           }
     
           private void LoadPdf()
           {
               var mainDocument = new RadFixedDocument();
     
               var document1 = LoadDocument(@"C:\inputFile.pdf");
     
               var document2 = LoadDocument(@"C:\inputFile1.pdf");
     
               foreach (var page in document1.Pages.ToList())
               {
                   document1.Pages.Remove(page);
                   mainDocument.Pages.Add(page);
               }
     
               foreach (var page in document2.Pages.ToList())
               {
                   document2.Pages.Remove(page);
                   mainDocument.Pages.Add(page);
               }
     
               pdfViewer.DocumentSource = GetPdfDocumentSource(mainDocument);
           }
     
    private PdfDocumentSource GetPdfDocumentSource(RadFixedDocument document)
           {
               var formatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider
               {
                   ExportSettings =
                   {
                       ImageQuality = ImageQuality.High
                   }
               };
     
               var pdfMemoryStream = new MemoryStream(formatProvider.Export(document));
     
               var pdfSource = new PdfDocumentSource(pdfMemoryStream, pdfViewer.DefaultFormatProviderSettings);
     
               pdfSource.Loaded += (sender, args) =>
               {
                   if (pdfMemoryStream != null)
                   {
                       pdfMemoryStream.Dispose();
                       pdfMemoryStream = null;
                   }
               };
     
               return pdfSource;
           }

     

    Here are the pdf files you can try to join (must be them because that's the point of the post, one of it doesn't show the image): https://goo.gl/Wjwk2i

     Thank you for your reply!!

  4. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 08 Feb Link to this post

    Hi Maurício,

    I was able to reproduce the behavior you are experiencing and it is caused by a missing feature in the RadPdfProcessing library. The image in question is actually preserved as a Form XObject. We have already logged such a request and you could track its progress through this public item. However, I am afraid that I cannot suggest you a suitable approach to work around this missing functionality.

    Regards,
    Tanya
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top