PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

RTF renderer doesn't resize images properly [Fixed?]
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3532
Page 1 of 1

Author:  MariusIonut [ Wed Jan 18, 2017 1:32 pm ]
Post subject:  RTF renderer doesn't resize images properly [Fixed?]

Hello, I've been using MigraDoc for a while now and I encountered two problems in version 1.32 ( http://pdfsharp.codeplex.com/releases/view/83749 ).
1. There is a casting problem in image rendering from double to float when using GetValueAsIntended. (ImageRenderer.cs attached) also was reported before on forum

2. The second problem is that in PDF the image is resized as requested but in RTF it is not. I can't find the problem so maybe I can get some help
I have attached image and the dlls.

Update:
It seems that its not actually a problem from migradoc.
\picscalex \picscaley
\picw \pich
Image resize arguments are written as is supposed to be but when opening the RTF in word document every image that doesn't have square format is altered by dividing picscaley by 4

ex: if we have an image with the arguments:
\picscalex22\picscaley22\pichgoal66150\picwgoal27285\pich116681\picw48127
it will actually be read by Word as
\picscalex22\picscaley8\pichgoal66150\picwgoal27285\pich116681\picw48127

but if we have a square image:
\picscalex22\picscaley22\pichgoal960\picwgoal960\pich1693\picw1693
the reading is done properly

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using MigraDoc.RtfRendering;

namespace ConsoleApplication4 {
    class Program {
        static void Main(string[] args) {
            Document document = new Document();
            // Add a section to the document
            Section section = document.AddSection();
            // Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();
            // Add some text to the paragraph
            var image = section.AddImage(@"C:\template_figure.png");
            image.LockAspectRatio = true;
            image.Height = Unit.FromMillimeter(243.3611);
            image.ScaleHeight = 0.95;
           
            //PDF
            const bool unicode = true;

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode);
            pdfRenderer.Document = document.Clone();
            pdfRenderer.RenderDocument();

            pdfRenderer.PdfDocument.Save(@"C:\template_figure.pdf");

            //RTF
            RtfDocumentRenderer renderer = new RtfDocumentRenderer();
            renderer.Render(document.Clone(), @"C:\template_figure.rtf", null);
        }
    }
}



Just change C:\template_figure.png to where the image is and update the dlls location.

Attachments:
MigraDocLib.part2.rar [184.8 KiB]
Downloaded 656 times
MigraDocLib.part1.rar [200 KiB]
Downloaded 662 times
ImageRenderer.rar [3.19 KiB]
Downloaded 661 times

Author:  MariusIonut [ Wed Jan 18, 2017 1:34 pm ]
Post subject:  Re: RTF renderer doesn't resize images properly

Reply to add the image, sorry, there is a maximum of 3 files per post and 256 kb per file.

Attachments:
template_figure.png
template_figure.png [ 66.85 KiB | Viewed 11338 times ]

Author:  Thomas Hoevel [ Thu Jan 19, 2017 2:32 pm ]
Post subject:  Re: RTF renderer doesn't resize images properly [Updated]

Hi!
MariusIonut wrote:
Image resize arguments are written as is supposed to be but when opening the RTF in word document every image that doesn't have square format is altered by dividing picscaley by 4
Thanks for the update.
Have you tried several Word versions?

Which value does \picscaley get when you fix the image in Word and save the document?

It seems that \pichgoal and \picwgoal are optional. Maybe you get better results if you leave \picwgoal or both out.

Author:  MariusIonut [ Thu Jan 19, 2017 4:58 pm ]
Post subject:  Re: RTF renderer doesn't resize images properly [Updated]

\picscalex30\picscaley30\pichgoal44640\picwgoal22320\pich78740\picw39370\pngblip

when opened in Word the sizes are Width: 30% and Height:15%, I've tried several Word versions.

Correct one is:

\picscalex30\picscaley60\pichgoal44640\picwgoal22320\pich78740\picw39370\pngblip

in Word is displayed as Width 30% and Height 30%

Author:  MariusIonut [ Fri Jan 20, 2017 11:27 am ]
Post subject:  Re: RTF renderer doesn't resize images properly [Updated]

After some digging and some blind conversions I came up with the solution(more of a hacky workaround?)

Dividing the pichgoal,picwgoal,pich,picw by 4 and multiplying picscalex,picscaley by 4 results in the perfect resolution.


Also I've tried other way to generate RTF(like DOCBOOK) files and somehow they all seem to result in wrong resized images.

fix in ImageRenderer.cs
Code:
        /// <summary>
        ///   Renders scaling, width and height for the image.
        /// </summary>
        private void RenderDimensionSettings()
        {
            float scaleX = (GetShapeWidth() / _originalWidth);
            float scaleY = (GetShapeHeight() / _originalHeight);

            rtfWriter.WriteControl("picscalex", (int)(scaleX * 100 * 4));
            rtfWriter.WriteControl("picscaley", (int)(scaleY * 100 * 4));

            RenderUnit("pichgoal", GetShapeHeight() / scaleY / 4);
            RenderUnit("picwgoal", GetShapeWidth() / scaleX / 4);

            //A bit obscure, but necessary for Word 2000:
            rtfWriter.WriteControl("pich", (int)(_originalHeight.Millimeter * 100 / 4));
            rtfWriter.WriteControl("picw", (int)(_originalWidth.Millimeter * 100 / 4));
        }

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/