0

此片段是用于生成 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);
4

1 回答 1

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();
    }
于 2013-10-09T13:36:59.030 回答