Pregunta

Estoy tratando de imprimir con WPF's PrintDialog Clase (Namespace System.Windows.Controls en PresentationFramework.dll, v4.0.30319). Este es el código que uso:

private void PrintMe()
{
    var dlg = new PrintDialog();

    if (dlg.ShowDialog() == true)
    {
        dlg.PrintVisual(new System.Windows.Shapes.Rectangle
        {
            Width = 100,
            Height = 100,
            Fill = System.Windows.Media.Brushes.Red
        }, "test");
    }
}

El problema es que no importa qué tamaño de papel seleccione para "escritor de documentos de Microsoft XPS", el XPS generado, siempre tiene el ancho y la altura de "Carta" tipo de papel:

Este es el código XAML que puedo encontrar dentro del paquete XPS:

<FixedPage ... Width="816" Height="1056">

¿Fue útil?

Solución

Cambiar el tamaño del papel en el cuadro de diálogo de impresión solo afecta el Ticket PRIMPRESE, no el contenido de Page FIJE. El método PrintVisual produce páginas de tamaño de letra, por lo que para tener un tamaño de página diferente, debe usar el método PrintDocument, así:

private void PrintMe()
{
    var dlg = new PrintDialog();
    FixedPage fp = new FixedPage();
    fp.Height = 100;
    fp.Width = 100;
    fp.Children.Add(new System.Windows.Shapes.Rectangle
        {
            Width = 100,
            Height = 100,
            Fill = System.Windows.Media.Brushes.Red
        });
    PageContent pc = new PageContent();
    pc.Child = fp;
    FixedDocument fd = new FixedDocument();
    fd.Pages.Add(pc);
    DocumentReference dr = new DocumentReference();
    dr.SetDocument(fd);
    FixedDocumentSequence fds = new FixedDocumentSequence();
    fds.References.Add(dr);            

    if (dlg.ShowDialog() == true)
    {
        dlg.PrintDocument(fds.DocumentPaginator, "test");
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top