Image is not imported/exported from PDF file

8 posts, 0 answers
  1. Maurício
    Maurício avatar
    42 posts
    Member since:
    Jan 2011

    Posted 01 Feb 2016 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
    558 posts

    Posted 04 Feb 2016 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
    42 posts
    Member since:
    Jan 2011

    Posted 04 Feb 2016 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
    558 posts

    Posted 08 Feb 2016 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
  5. Vo
    Vo avatar
    4 posts
    Member since:
    Apr 2011

    Posted 08 Sep Link to this post

    Hi,

    i am using pdfviewer to show pdf content from url.

     

    My problem is some content is not showed.

     

    I tried open my pdf file in latest demo version and this error is still appeared. (attach file)

     

    Can anybody help me?

  6. Tanya
    Admin
    Tanya avatar
    558 posts

    Posted 12 Sep Link to this post

    Hello,

    I am afraid I cannot tell why the content is missing before testing the case. Is it possible to share a sample document so we can check what is causing you issues?

    Regards,
    Tanya
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  7. Vo
    Vo avatar
    4 posts
    Member since:
    Apr 2011

    Posted 13 Sep in reply to Tanya Link to this post

    my test case is very simple. I open a pdf by a edit pdf tool and insert some new content, then save it.

    When i open edited file by pdfviewer (direct on telerik demo) --> pdf is showed but my new content is lost.

  8. Maurício
    Maurício avatar
    42 posts
    Member since:
    Jan 2011

    Posted 13 Sep in reply to Vo Link to this post

    Vo said:

    my test case is very simple. I open a pdf by a edit pdf tool and insert some new content, then save it.

    When i open edited file by pdfviewer (direct on telerik demo) --> pdf is showed but my new content is lost.

    I just did what you said and it worked fine.

    I've created a new PDF using this online edit tool: http://www.pdfescape.com/open/

    Then I drew a line, downloaded the file and opened with the PdfViewer from the demos.

Back to Top