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

Export DOCX to PDF with images

11 Answers 319 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
ghini
Top achievements
Rank 2
ghini asked on 17 Jan 2017, 09:37 AM
Hello to all,
I have a DOCX file with a header logo.
I import the file in a RadFlowDocument and edit it with a RadFlowDocumentEditor.
If I save it in a new DOCX everything it is OK.
If I save a PDF (with Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider) the image disappears.

The image in my DOCX is "in line".
So I think I should not convert images to "ImageInline", right?
If so, why have not the class Telerik.Windows.Documents.Model.ImageInline?
My Telerik version is 2016.2.607.

Thank you

11 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 19 Jan 2017, 04:44 PM
Hello,

We are unaware of issues related to the export of inline images to PDF and I am not sure what might be causing the behavior you are experiencing. Is it possible to share a document showing the exact case so we can test it locally?

Regards,
Tanya
Telerik by Progress

0
Frankfrut RSC IM
Top achievements
Rank 1
answered on 20 Jan 2017, 03:11 PM

Hi Ghini,

I have the same problem with 2016.2.607 when saving a RadFlowDocument as PDF.

To solve this, I had to do first create a new image with the existing data and then resave it as a PNG:

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
Paragraph imageParagraph = datacell5.Blocks.AddParagraph();
 
//Create new image as the PDF export won't display
System.Drawing.Image img =  System.Drawing.Image.FromStream(ms);
 
//Must resave the byte[] from ms to ensure that it can be inserted into the PDF properly.
using (var msImg = new MemoryStream())
{
    img.Save(msImg, System.Drawing.Imaging.ImageFormat.Png);
    editor.MoveToParagraphStart(imageParagraph);
    int newHeight = (int)(img.Height / ((double)img.Width / 100));
    editor.InsertImageInline(msImg, "png", new Size(100, newHeight));
}

 

Once I did this step, the images were correctly added to the PDFs.

Hope this helps!

0
Frankfrut RSC IM
Top achievements
Rank 1
answered on 20 Jan 2017, 03:13 PM
    using (var ms = new MemoryStream(row["Picture"].ConvertOrDefault<byte[]>()))
    {
        RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
        Paragraph imageParagraph = datacell5.Blocks.AddParagraph();
         
        //Create new image as the PDF export won't display
        System.Drawing.Image img =  System.Drawing.Image.FromStream(ms);
         
        //Must resave the byte[] from ms to ensure that it can be inserted into the PDF properly.
        using (var msImg = new MemoryStream())
        {
            img.Save(msImg, System.Drawing.Imaging.ImageFormat.Png);
            editor.MoveToParagraphStart(imageParagraph);
            int newHeight = (int)(img.Height / ((double)img.Width / 100));
            editor.InsertImageInline(msImg, "png", new Size(100, newHeight));
        }
    }
}

 

Sorry, forgot the first line of code.
0
Tanya
Telerik team
answered on 25 Jan 2017, 12:32 PM
Hi ,

As I mentioned in my previous post, the inline images are supported and I cannot say what the reason might be before testing a sample document.

Since you haven't specified the type of the image you are having troubles with, I would like to mention that the floating images are not supported by PdfFormatProvider and are skipped when you export PDF documents. If you would like, you can vote for the implementation of that feature and track its status through the related public item.

Regards,
Tanya
Telerik by Progress

0
ghini
Top achievements
Rank 2
answered on 14 Feb 2017, 07:37 AM
Hi Frankfrut, thanks for your help!
I'm sorry but I do not understand the first line ... What is row["Picture"]?

However I solved by modifying the original document and using PNG images.
Now the output in DOCX is changed but well or other problem in the PDF generation. All paragraphs of justified text in DOCX, are left-aligned in the PDF.

thank you so much
0
sharon
Top achievements
Rank 1
answered on 10 Jan 2018, 06:02 PM

Im having same same problem when trying to convert a docx (having logo/clip art images) to pdf. The backcolor of the images are turning black. Is there any solution for this? 

Is there anyway i can convert those problematic images to png during conversion? 

Here is my code,

 var fileFormatProvider = new DocxFormatProvider();
                var document = new RadFlowDocument();

string pdfFilename = string.Empty;

                using (FileStream input = new FileStream(docxReportfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    document = fileFormatProvider.Import(input);
                }

                byte[] renderedBytes = null;
                IFormatProvider<RadFlowDocument> formatProvider = new PdfFormatProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    formatProvider.Export(document, ms);
                    renderedBytes = ms.ToArray();
                    pdfFilename = Path.ChangeExtension(docxReportfile, ".pdf");
                    SysFile.WriteAllBytes(pdfFilename, renderedBytes);

 

0
sharon
Top achievements
Rank 1
answered on 10 Jan 2018, 06:08 PM
Im using the version 2015.3.1322.40
0
Deyan
Telerik team
answered on 15 Jan 2018, 04:40 PM
Hello Sharon,

You may enumerate the images in the RadFlowDocument instance and change their ImageSource properties. The following code snippet shows how to enumerate all ImageInline instances, calculate their image bytes so that they become valid PNG images and then change the ImageSouce.
foreach (ImageInline image in document.EnumerateChildrenOfType<ImageInline>())
{
    byte[] originalImageData = image.Image.ImageSource.Data;
    byte[] pngImageData = CreatePngImageBytes(originalImageData);
    image.Image.ImageSource = new Telerik.Windows.Documents.Media.ImageSource(pngImageData, "png");
}

It would also be helpful if you can share a link for downloading the problematic DOCX file. This way we would be able to reproduce the original issue on our side and try to provide a more concrete solution.

I hope this helps.

Regards,
Deyan
Progress Telerik

0
Simon Woods
Top achievements
Rank 1
answered on 11 Apr 2018, 02:51 PM

Hi Deyan

Is there any chance you could post the CreatePngImageBytes routine?

Thx

s

0
Simon Woods
Top achievements
Rank 1
answered on 11 Apr 2018, 03:34 PM

Actually I may have sorted that already ... but I have a further question

I've been using a RadDocument. My docx loads fine using that. However I don't have access to convert the embedded bitmaps to pngs as far as I can see. It looks like your code is tied to a RadFlowDocument as was Sharon's example code above.

However, if I try and load my docx in to RadFlowDocument, it errors out on the DocxFormatProvider.Import with an error

"Value cannot be null.
Parameter name: relationshipId"

Why would the RadDocument be ok and the RadFlowDocument not?

Further, if I need to use the RadDocument, how would I convert embedded images to pngs.

Many thx again

s

 

 

 

0
Deyan
Telerik team
answered on 16 Apr 2018, 01:14 PM
Hello Simon Woods,

RadWordsProcessing and RadRichTextBox are two separate products and they do not have common codebase. This explains the different support of functionalities provided by the corresponding RadFlowDocument and RadDocument classes.

In order to reproduce the mentioned import exception, we need to test the problematic DOCX file. Could you send us this file either by adding it as an attachment to a newly opened support ticket or by sending a link to the file in this public forum thread? This way we will be able to investigate the concrete cause of the issue and suggest a solution for it.

As for changing the images with RadDocument - it is done in a way similar to the RadFlowDocument approach. There are only two API differences. The first difference is that instead of using Data property you should use InlineImage GetBytes() method. The second difference is that you should change the image source by using InlineImage Init method.

I hope this information is helpful.

Regards,
Deyan
Progress Telerik

Tags
WordsProcessing
Asked by
ghini
Top achievements
Rank 2
Answers by
Tanya
Telerik team
Frankfrut RSC IM
Top achievements
Rank 1
ghini
Top achievements
Rank 2
sharon
Top achievements
Rank 1
Deyan
Telerik team
Simon Woods
Top achievements
Rank 1
Share this question
or