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

Error when converting from Word to PDF

4 Answers 674 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 05 Aug 2020, 07:07 PM

Not sure if this is were I should post this question.  I have created a dotnet standard assembly to use with my controller with the PDFViewer that will translate a Word document (docx) to a pdf.  I have tried several different methods and get the same error.  The error I receive is:  "Can not export other than Jpeg and Jpeg2000 and ImageQuality different than High".  The document is pretty straight forward with text a barcpde and a table if that helps (a small image of the doc attached).  Does anyone have an idea how to fix this?  I also created a "this is a test" document and when it is displayed it shows: "PDF file fails to process."  I have attached the document.

 

System.NotSupportedException
  HResult=0x80131515
  Message=Can not export other than Jpeg and Jpeg2000 and ImageQuality different than High
  Source=Telerik.Documents.Fixed
  StackTrace:
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.DoOnUnknownData(Byte[] unknownData, ImageQuality imageQuality, Action`1 doOnEncodedData)
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.InitializeImageInfoFromUnknownData(Byte[] unknownData, ImageQuality imageQuality)
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.EnsureImageInfo()
   at Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.get_Width()
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.Objects.ImageXObject.CopyPropertiesFrom(IPdfExportContext context, ImageSource imageSource)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfExportContext.GetResource(ImageSource resource)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfContentExportContext.GetResource(ImageSource imageSource)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ImageWriter.WriteOverride(PdfWriter writer, IPdfContentExportContext context, Image element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.MarkableContentElementWriter`1.Write(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentElementWriterBase.WriteElement(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentRootWriter.WriteOverride(PdfWriter writer, IPdfContentExportContext context, IContentRootElement element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentElementWriter`1.Write(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ContentElementWriters.ContentElementWriterBase.WriteElement(PdfWriter writer, IPdfContentExportContext context, Object element)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.ContentStream.BuildContentData(IPdfExportContext context, IResourceHolder resourceHolder, IContentRootElement contentRootElement)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.Page.CopyPropertiesFrom(IPdfExportContext context, RadFixedPage fixedPage)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.DocumentCatalog.CopyRadFixedPageProperties(RadFixedPage source, Page destination, IRadFixedDocumentExportContext context)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.DocumentCatalog.CopyPagePropertiesFrom(IRadFixedDocumentExportContext context)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Model.Elements.DocumentStructure.DocumentCatalog.CopyPropertiesFrom(IRadFixedDocumentExportContext context)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.PdfExporter.Export(IRadFixedDocumentExportContext context, Stream output)
   at Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider.ExportOverride(RadFixedDocument document, Stream output)
   at Telerik.Windows.Documents.Common.FormatProviders.FormatProviderBase`1.Export(T document, Stream output)
   at Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider.ExportOverride(RadFlowDocument document, Stream output)
   at Telerik.Windows.Documents.Common.FormatProviders.FormatProviderBase`1.Export(T document, Stream output)
   at TexasProtax.Document.Pdf.ConvertToPdf.FromWord(String filename) in D:\#bitbucket\TexasProtax\texasprotax.appraisalportal\TexasProtax.Document\Pdf\ConvertToPdf.cs:line 36
   at TexasProTax.AppraisalPortal.Web.Areas.Document.Controller.ViewerController.GetDocument(String location) in D:\#bitbucket\TexasProtax\texasprotax.appraisalportal\TexasProtax.AppraisalPortal.Web\Areas\Document\Controller\ViewerController.cs:line 43
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

Here is the method:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Telerik.Windows.Documents.Common.FormatProviders;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
using Telerik.Windows.Documents.Flow.Model;
 
...
 
        public static Stream FromWord(string filename)
        {
            using (Stream inStream = File.OpenRead(filename))
            using (Stream outStream = new MemoryStream())
            {
                IFormatProvider<RadFlowDocument> fileFormatProvider = new DocxFormatProvider();
 
                RadFlowDocument document = null;
                using (Stream input = File.OpenRead(filename))
                {
                    document = fileFormatProvider.Import(input);
                }
 
                //byte[] renderedBytes = null;
                IFormatProvider<RadFlowDocument> formatProvider = new PdfFormatProvider();
                using (MemoryStream ms = new MemoryStream())
                {
                    formatProvider.Export(document, ms);
                    //renderedBytes = ms.ToArray();
                    return ms;
                }
 
            }
        }

4 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 10 Aug 2020, 09:33 AM

Hello Carlos,

The described behavior with the NotSupportedException seems to be related to an already fixed issue, related to the previously not supported image formats (e.g. WMF, EMF, GIF, PNG, and other). We have provided an additional assembly (Telerik.Documents.ImageUtils) with our Release R2 2020 that handle these image formats. For more information, you can check the WordsProcessing`s Cross-Platform Support help article. 

I would like to suggest (if you aren't until now) updating your Telerik Document Processing binaries/NuGets to a current version. If this doesn't help I would like to ask you to send us the document causing this behavior. I must assure you we treat all client files in a strictly confidential manner and for testing purposes only. You can do this in a separate private ticket selecting WordsProcessing as a Product.

As for the other document (attached to this ticket thread), I tried to convert it to PDF document using the WordsProcessing library and it works as expected. Such behavior is often observed when rewriting an existing PDF file. To make sure this doesn't happen you can check if a document with such name already exists and if so to delete it before you export it again. Check the following code snippet: 

string exportedDocumentName = "Sample.pdf";
if (File.Exists(exportedDocumentName))
{
	File.Delete(exportedDocumentName);
}

Looking forward to hearing from you.

Regards,
Martin
Progress Telerik

0
Carlos
Top achievements
Rank 1
answered on 11 Aug 2020, 11:35 AM

Thank you for your reply.  I have upgraded to 2020.2.615 and I was able to use the samples "ConvertDocuments_NetStandard" to create an class that saves a pdf to a temp file and pass to the PdfViewer in Asp.net Core.  Is it possible to instead of passing a file name, pass a memory stream to the PdfViewer which is expecting a FileStreamResult?  This is working with the TestDocument but the production document has an embedded barcode. 

 

I think it is complaining about with "fixedextensibilitymanager.jpegimageconverter cannot be null. the .net standard ..."  I am working through that if I can't figure out I will reach out again.  Howerver right now I need a to resolve not using a temporary file but using a memory stream.

0
Carlos
Top achievements
Rank 1
answered on 11 Aug 2020, 12:01 PM
Also when I pass a MemoryStream from the code below the PdfViewer spins and never displays the pdf. I have attached the full class that converts the docx to pdf.
IFormatProvider<RadFlowDocument> formatProvider = new PdfFormatProvider();
MemoryStream ms = new MemoryStream();
formatProvider.Export(this.document, ms);
return ms;

 

Here is the controller action

        [HttpGet]
        [Route("getdocument")]
        public FileStreamResult GetDocument(string location)
        {
            Stream stream = null;

            // test if location is docx
            if (location.ToLower().EndsWith("docx"))
            {
                DocumentConverter converter = new DocumentConverter();
                stream = converter.ConvertToPdf(location);
            }


            // Test if location is a pdf
            if (location.ToLower().EndsWith("pdf"))
                stream = new MemoryStream(System.IO.File.ReadAllBytes(location));

            if (stream == null)
                stream = new MemoryStream();

            return new FileStreamResult(stream, "application/pdf");
        }

0
Martin
Telerik team
answered on 12 Aug 2020, 10:12 AM

Hello Carlos,

First things first, if I understand you right you need to store the document after the conversion to memory stream instead of a PDF file. If so,  please check the following code snippet:

Stream memoryStream = new MemoryStream();
var flowPdfFormatProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
flowPdfFormatProvider.Export(radFlowDocument, memoryStream);

Then (when needed) you can import this memory stream into a RadFixedDocument in order to pass it to the PdfViewer:

var fixedPdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
RadFixedDocument radFixedDocument = fixedPdfFormatProvider.Import(memoryStream);

As for the JpegImageConverter, I would suggest checking the information and examples in the PdfProcessing`s Cross-Platform Support help article.

I am attaching a sample project demonstrating these functionalities. Please, feel free to modify it in a way closer to your scenario.

As for your last question about that the PdfViwer never displays the imported PDF document, we will need the document causing this behavior in order to investigate the issue, so I would like to suggest to open a new support ticket, describe this behavior, and attach the document. I must assure you we treat all client files in a strictly confidential manner and for testing purposes only.

Regards,
Martin
Progress Telerik

Tags
PDFViewer
Asked by
Carlos
Top achievements
Rank 1
Answers by
Martin
Telerik team
Carlos
Top achievements
Rank 1
Share this question
or