Docx/Xlsx to RadfixedDocument via PdfProcessing

4 posts, 0 answers
  1. Olaf
    Olaf avatar
    3 posts
    Member since:
    Aug 2016

    Posted 09 Sep Link to this post

    Hi, Im trying to convert docx/xlsx documents to jpg images. The only way I know of how to do this is via the WPF telerik. By converting a Docx/XLSX to PDF then PDF to jpg image. If there is any other way please let me know. This is what I'm currently doing:

    XLSX

    Dim stream As FileStream = File.OpenRead(inputPath)
    Dim document As RadFixedDocument
     
    Dim fileFormatProvider As Spreadsheet.FormatProviders.IWorkbookFormatProvider = New Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider()
    Dim workbook As Spreadsheet.Model.Workbook
    Using stream
       workbook = fileFormatProvider.Import(stream)
    End Using
     
    Dim formatProvider As New Spreadsheet.FormatProviders.Pdf.PdfFormatProvider()
    document = formatProvider.ExportToFixedDocument(workbook)
     
    Dim page As RadFixedPage = document.Pages(0)
    Dim factory2 As New ThumbnailFactory
    Dim size2 = New Size(1200, 1800)
     
    'This line throws an error
    Dim imageSrc As ImageSource = factory2.CreateThumbnail(page, size2)

     

    DOCX

    Dim stream As FileStream = File.OpenRead(inputPath)
    Dim document As RadFixedDocument
     
    Dim docxformatprovider As New Flow.FormatProviders.Docx.DocxFormatProvider()
    Dim docxdocument As Flow.Model.RadFlowDocument = docxformatprovider.Import(stream)
    Dim formatProvider As New Flow.FormatProviders.Pdf.PdfFormatProvider()
    document = formatProvider.ExportToFixedDocument(docxdocument)
     
    'To image conversion
    Dim page As RadFixedPage = document.Pages(0)
    Dim factory2 As New ThumbnailFactory
    Dim size2 = New Size(1200, 1800)
     
    'This line throws an error
    Dim imageSrc As ImageSource = factory2.CreateThumbnail(page, size2)

     

    The code is a little bit different, the xlsx code contains Using Stream but makeing no difference. 

    The last line throws an error: An unhandled exception of type 'System.NullReferenceException' occurred in Telerik.Windows.Controls.FixedDocumentViewers.dll

    But if I save the Flowdocument as a PDF, then open it again it wont be a problem to create an imageSrc with the fixedpage, this is of course not a desirable situation:

    'Here I already converted the document to a RadFlowDocument
     
    Dim provider As New Fixed.FormatProviders.Pdf.PdfFormatProvider()
    Using output As Stream = File.OpenWrite(inputPath & ".pdf")
        provider.Export(document, output)
    End Using
     
    Dim stream2 As FileStream = File.OpenRead(inputPath & ".pdf")
    Dim pdf As New Fixed.FormatProviders.Pdf.PdfFormatProvider(stream2, Fixed.FormatProviders.FormatProviderSettings.ReadOnDemand)
    document = pdf.Import()
     
    Dim page As RadFixedPage = document.Pages(0)
    Dim factory2 As New ThumbnailFactory
    Dim size2 = New Size(1200, 1800)
     
    'No problem, image can be created.
    Dim imageSrc As ImageSource = factory2.CreateThumbnail(page, size2)

    This only works for the DOCX file. The XLSX can't be saved from the workbook file. 

    I hope you can help me.

    These are the guides I used:
    http://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/pdf/pdfformatprovider.html
    http://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats-and-conversion/docx/docxformatprovider.html
    http://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/xlsx/xlsxformatprovider.html
    http://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/formats-and-conversion/pdf/pdfformatprovider.html
    http://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdfformatprovider.html
    http://www.telerik.com/forums/convert-pdf-to-jpg

  2. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 13 Sep Link to this post

    Hello Olaf,

    The issue you are observing is related to the fact that the RadFixedDocument class is shared between RadPdfProcessing and RadPdfViewer. Currently, the class is not completely unified to work with the two controls and the way the document is created is substantial for its subsequent use.

    The format provider, created through the empty constructor of the class, creates an instance of RadFixedDocument, that is used in PdfProcessing. However, the API for creating thumbnails from a PDF document is included in PdfViewer and in order to create the corresponding document, you should use the overload of PdfFormatProvider's constructor that accepts two parameters. Here is how I modified your code to achieve the desired goal:
    Dim fileFormatProvider As Spreadsheet.FormatProviders.IWorkbookFormatProvider = New Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider()
    Dim workbook As New Spreadsheet.Model.Workbook()
    Using stream
        workbook = fileFormatProvider.Import(stream)
    End Using
     
    Dim formatProvider As New Spreadsheet.FormatProviders.Pdf.PdfFormatProvider()
    Dim documentBytes = formatProvider.Export(workbook)
     
    ' Use the provider that corresponds to PdfViewer
    Dim pdfProvider As New PdfFormatProvider(New MemoryStream(documentBytes), FormatProviderSettings.ReadAllAtOnce)
    Dim pdfDocument As RadFixedDocument = pdfProvider.Import()
     
    Dim page As RadFixedPage = pdfDocument.Pages(0)
    Dim factory2 As New ThumbnailFactory()
    Dim size2 As New Size(1200, 1800)
     
    'This line throws an error
    Dim imageSrc As ImageSource = factory2.CreateThumbnail(page, size2)

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress

  3. Olaf
    Olaf avatar
    3 posts
    Member since:
    Aug 2016

    Posted 16 Sep in reply to Tanya Link to this post

    And currently there is no support for creating images directly form XLSX or DOCX?
  4. Tanya
    Admin
    Tanya avatar
    405 posts

    Posted 20 Sep Link to this post

    Hi Olaf,

    At this point, exporting the documents to images is an unsupported functionality. The libraries are independent of UI and don't have the notion of how the document should look like. Therefore, they cannot directly print a document or export it to an image.

    Regards,
    Tanya
    Telerik by Progress

Back to Top