PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Everything is offset by 1.333 when not doing pop
http://forum.pdfsharp.de/viewtopic.php?f=2&t=3941
Page 1 of 1

Author:  karl.wolf [ Fri Apr 05, 2019 6:20 am ]
Post subject:  Everything is offset by 1.333 when not doing pop

Code for the mainwindow:
Code:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Render();
        }

        void Render()
        {
            double A4Width = XUnit.FromCentimeter(21).Point;
            double A4Height = XUnit.FromCentimeter(29.7).Point;

            FixedDocument fixedDocument = new FixedDocument();
            var size = new Size(A4Height, A4Width);
            fixedDocument.DocumentPaginator.PageSize = size;

            DrawingVisual dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();
            XGraphics gfx = XGraphics.FromDrawingContext(dc, new XSize(size.Width, size.Height), XGraphicsUnit.Point);
            // FIXME: Seems everything is off by factor 1.333 if I do not pop here (or scale by 1.0/1.33), which is exactly what is in the DefaultViewMatrix of the gfx
            //dc.Pop();
            RenderDocument(gfx, gfx.PageSize);
            dc.Close();

            // Create page content
            UIElement visual = new DrawingVisualPresenter(dv);

            FixedPage fixedPage = new FixedPage
            {
                Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0xFE, 0xFE, 0xFE))
            };


            fixedPage.Children.Add(visual);

            PageContent pageContent = new PageContent
            {
                Child = fixedPage,
            };

            fixedDocument.Pages.Add(pageContent);
            Viewer.Document = fixedDocument;
        }

        void RenderDocument(XGraphics gfx, XSize size)
        {
            //Rectangle for presentation
            var container = gfx.BeginContainer();

            //Would solve the problem, but why is there a 1.33 factor???
            //gfx.ScaleTransform(1.0 / 1.33, 1 / 1.33);
            XRect rect = new XRect(new XPoint(), gfx.PageSize);
            rect.Inflate(-50, -50);
            gfx.DrawRectangle(XPens.Blue, rect);
        }
    }


Xaml window code:
Code:
<Window x:Class="WPF_Projekt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_Projekt"
        mc:Ignorable="d"
        Title="MainWindow" Height="1080" Width="1920">
    <Grid>
        <DocumentViewer Panel.ZIndex="0" Grid.Row="1" Grid.Column="0" x:Name="Viewer"/>
    </Grid>
</Window>


Ass mentioned in the commented code. There is a 1.333 factor offset on everthing, when I am not doing dc.pop(). Why is that the case?

Attachments:
File comment: This is how it should look like
wpf_offset_correct.PNG
wpf_offset_correct.PNG [ 4.76 KiB | Viewed 21926 times ]
File comment: Here you can see the offset.
wpf_offset.PNG
wpf_offset.PNG [ 4.13 KiB | Viewed 21926 times ]

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