Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Printing XPS document from DocumentViewer

$
0
0

My current problem is either a wierd bug or I have no idea how to make documents.

I'm trying to print some reports from WPF, and I'm initially generating them as Flow Documents and than converting them to XPS documents, so I can display them in DocumentViewer. That part works perfectly.

But when I print the document by clicking on Print icon in DocumentViewer, contents of ALL inline elements that do not contain letters default to being displayed in a font other than the one specified.

So if this is the code that is used to make and display a simple table:

FlowDocument document = new FlowDocument();
FontFamily font = new System.Windows.Media.FontFamily("Times New Roman");
document.FontFamily = font;
Table table = new Table();
document.Blocks.Add(table);
TableRowGroup rowGroup = new TableRowGroup();
table.RowGroups.Add(rowGroup);
TableRow row = new TableRow();
rowGroup.Rows.Add(row);

TableCell tempCell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
tempCell.Blocks.Add(new Paragraph(new Run(txt1.Text)) { FontFamily = font });
row.Cells.Add(tempCell);

tempCell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
tempCell.Blocks.Add(new Paragraph(new Run(txt2.Text)) { FontFamily = font });
row.Cells.Add(tempCell);

tempCell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
tempCell.Blocks.Add(new Paragraph(new Run(txt3.Text)) { FontFamily = font });
row.Cells.Add(tempCell);

tempCell = new TableCell() { BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
tempCell.Blocks.Add(new Paragraph(new Run(txt4.Text)) { FontFamily = font });
row.Cells.Add(tempCell);

DocumentPaginator _paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
CleanTempFile();
fileName = System.IO.Path.GetRandomFileName();
try
{
    // write the XPS document
    using (XpsDocument doc = new XpsDocument(fileName, FileAccess.ReadWrite))
    {
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
        writer.Write(_paginator);
    }

    // Read the XPS document into a dynamically generated
    // preview Window 
    using (XpsDocument doc = new XpsDocument(fileName, FileAccess.Read))
    {
        FixedDocumentSequence fds = doc.GetFixedDocumentSequence();

        docView.Document = fds as IDocumentPaginatorSource;


    }
}
catch
{
    CleanTempFile();
}

and this is how DocumentViewer displays the document

and this is how the document looks like after it's printed:

Notice how all cells that do not contain letters are in a font that is not Times New Roman allthough I have explicitly set it as Times New Roman.

You can also check out the WPF project that I used to test this "bug" since my actual project is kind of large: Wpf project DocumentViewerTest

So is this a BUG or what am I doing wrong?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>