如何计算FixedPage维度

时间:2013-10-03 20:45:16

标签: c# wpf xps

此代码段是用于生成XPS文档的一些代码的一部分。 XPS文档生成不是开玩笑。如果可能的话,我希望避免在这里粘贴任何XPS代码。相反,此代码侧重于问题的WPF部分。

我要求你帮助的问题就在这里。我已经对尺寸进行了硬编码以适用于测试图像:

double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
fixedPage.Arrange(new Rect(new Point(magicNumber_X, 0), size));

相反,如何修复此代码以计算坐标?

完整方法:

    private PageContent AddContentFromImage()
    {
        var pageContent = new PageContent();
        var fixedPage = new FixedPage();            

        var bitmapImage = new BitmapImage(new Uri(hardCodedImageSampleFilePath, UriKind.RelativeOrAbsolute));

        var image = new Image();
        image.Source = bitmapImage;            
        fixedPage.Children.Add(image);

        ((IAddChild)pageContent).AddChild(fixedPage);

        double pageWidth = 96 * 8.5;//XPS documents are 96 units per inch
        double pageHeight = 96 * 11;

        fixedPage.Width = pageWidth;
        fixedPage.Height = pageHeight;

        var size = new Size(8.5 * 96, 11 * 96);

        fixedPage.Measure(size);
        double magicNumber_X = 3.5;//trial and error...3 too small 4 too big
        double magicNumber_Y = 0;
        fixedPage.Arrange(new Rect(new Point(magicNumber_X, magicNumber_Y), size));
        fixedPage.UpdateLayout();

        return pageContent;
    }

我有点惊讶FixedPage.Measure(size)本身并没有纠正这个问题。我试过没有传递任何参数,例如fixedPage.Arrange(new Rect(), size))仍然没有。

FWIW,当我使用PrintDocument时,这个计算工作正常。

private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        RectangleF marginBounds = e.MarginBounds;
        RectangleF printableArea = e.PageSettings.PrintableArea;

        int availableWidth = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width));
        int availableHeight = (int)Math.Floor(printDocument.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height));

        Rectangle rectangle = new Rectangle(0,0, availableWidth -1, availableHeight - 1);        
        g.DrawImage(_image, rectangle);

1 个答案:

答案 0 :(得分:1)

我迷上了FixedPage.Loaded事件,因为为了执行计算需要使用FixedPage.ActualHeight,并且在控件加载之前不会设置。这也意味着使用此机制必须显示FixedPage才能正确执行自动打印。

    void fixedPage_Loaded(object sender, RoutedEventArgs e)
    {
        var fixedDocument = sender as FixedPage;
        CalculateSize(fixedDocument);
    }
    private void CalculateSize(FixedPage fixedPage)
    {
        PrintQueue printQueue = LocalPrintServer.GetDefaultPrintQueue();
        PrintCapabilities capabilities = printQueue.GetPrintCapabilities();

        //get scale of the print wrt to screen of WPF visual
        double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / fixedPage.ActualWidth, capabilities.PageImageableArea.ExtentHeight / fixedPage.ActualHeight);

        //Transform the Visual to scale
        fixedPage.LayoutTransform = new ScaleTransform(scale, scale);

        //get the size of the printer page
        var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

        //update the layout of the visual to the printer page size.
        fixedPage.Measure(sz);
        double x = capabilities.PageImageableArea.OriginWidth;
        double y = capabilities.PageImageableArea.OriginHeight;
        fixedPage.Arrange(new Rect(new Point(x, y), sz));
        fixedPage.UpdateLayout();
    }