PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Oct 22, 2017 3:21 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Jan 18, 2017 1:32 pm 
Offline

Joined: Tue Apr 26, 2016 8:24 pm
Posts: 20
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 30 times
MigraDocLib.part1.rar [200 KiB]
Downloaded 37 times
ImageRenderer.rar [3.19 KiB]
Downloaded 34 times


Last edited by MariusIonut on Fri Jan 20, 2017 11:27 am, edited 3 times in total.
Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 18, 2017 1:34 pm 
Offline

Joined: Tue Apr 26, 2016 8:24 pm
Posts: 20
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 899 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 19, 2017 2:32 pm 
Offline
empira Employee
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2720
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 19, 2017 4:58 pm 
Offline

Joined: Tue Apr 26, 2016 8:24 pm
Posts: 20
\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%


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 20, 2017 11:27 am 
Offline

Joined: Tue Apr 26, 2016 8:24 pm
Posts: 20
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));
        }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group