WPF printing of multiple pages with preview - wpf

The more I read about the subject the less I understand so apologies in advance if the below seems completely off the wall.
I have a usercontrol that contains a flowdocument - a view with a corresponding viewmodel. The aim is to send this to a preview window where the user can view the document and also print it.
I lifted some code from an example at http://www.eggheadcafe.com/tutorials/aspnet/9cbb4841-8677-49e9-a3a8-46031e699b2e/wpf-printing-and-print-pr.aspx
When the below is called
Public Shared Sub PrintPreview(owner As Window, data As FormData)
Using xpsStream As New MemoryStream()
Using package__1 As Package = Package.Open(xpsStream, FileMode.Create, FileAccess.ReadWrite)
Dim packageUriString As String = "memorystream://data.xps"
Dim packageUri As New Uri(packageUriString)
PackageStore.AddPackage(packageUri, package__1)
Dim xpsDocument__2 As New XpsDocument(package__1, CompressionOption.Maximum, packageUriString)
Dim writer As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument__2)
Dim visual As New Form(data)
Dim printTicket As New PrintTicket()
printTicket.PageMediaSize = A4PaperSize
writer.Write(visual, printTicket)
Dim document As FixedDocumentSequence = xpsDocument__2.GetFixedDocumentSequence()
xpsDocument__2.Close()
Dim printPreviewWnd As New PrintPreviewWindow(document)
printPreviewWnd.Owner = owner
printPreviewWnd.ShowDialog()
PackageStore.RemovePackage(packageUri)
End Using
End Using
This brings up the print preview window and shows the user control that holds the flowdocument. However, it only shows the first of what should be multiple pages. I was under the assumption the whole point of writing the xps and then reading it back again in this window was to work around the problem of printing a visual but I'm obviously misunderstanding the whole process. Any help in getting through my thick head what I need to do to be able to view all of the pages in the document would be much appreciated.
Cheers
I thought the above with using xpsdocument and getfixeddocumentsequence would convert the flowdocument to a fixeddocument but seeing it doesn't, am I perhaps writing it wrongly??

You can print a visual to an XPS. However, as I understand it, it is your job to manage pages. If your visual is too big to fit on a page, you need to find a way to split it onto multiple pages.
The source code I posted here prints a list of items over many pages:
https://bitbucket.org/paulstovell/samples/src/a323f0c65ea4/XPS%20Report%20Generator/
If you can find a way to split your visuals (perhaps create 3 forms, with 15 items per form) into pages, you can then use this:
using (var doc = new XpsDocument("P:\\Test2.xps", FileAccess.Write))
{
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
var collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
collator.Write(form1);
collator.Write(form2);
collator.Write(form3);
collator.EndBatchWrite();
}
var doc2 = new XpsDocument("P:\\Test2.xps", FileAccess.Read);
var seq = doc2.GetFixedDocumentSequence();
var window = new Window();
window.Content = new DocumentViewer {Document = seq};
window.ShowDialog();
Also, note that if you're newing up a visual and printing it, you'll need to size it first, otherwise you may get an empty screen. Here's an example of generating a page of data (of course you'd change the sizes to fit an A4 sheet).
private StackPanel CreatePage()
{
var panel = new StackPanel();
panel.Width = 1000;
panel.Height = 1000;
for (var i = 0; i < 10; i++)
{
panel.Children.Add(new TextBlock() {Text = "Item " + i});
}
panel.Measure(new Size(1000, 1000));
panel.Arrange(new Rect(0, 0, 1000, 1000));
return panel;
}

Related

How to remove/disable the print button from the DocumentViewer in code (when on its own thread)?

I am running the following code in a background thread as an STA apartment to provide a Print Preview in a document viewer:
// Print Preview
public static void PrintPreview(FixedDocument fixeddocument)
{
MemoryStream ms = new MemoryStream();
using (Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite))
{
Uri u = new Uri("pack://TemporaryPackageUri.xps");
PackageStore.AddPackage(u, p);
XpsDocument doc = new XpsDocument(p, CompressionOption.Maximum, u.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(fixeddocument.DocumentPaginator);
var previewWindow = new Window();
var docViewer = new DocumentViewer();
previewWindow.Content = docViewer;
THIS FAILS ---> docViewer.CommandBindings.Remove(???Print Button???);
FixedDocumentSequence fixedDocumentSequence = doc.GetFixedDocumentSequence();
docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
previewWindow.ShowDialog();
PackageStore.RemovePackage(u);
doc.Close();
}
}
All works well. However, since this is running in its own thread--not the main thread--the print dialogue on the Document Viewer crashes.
In code, how can I remove and/or disable the Print button from the DocumentViewer?? (I have read everything I could find in Google, and it all is in XAML, not very helpful).
Any help is much appreciated. TIA
Update#1: The only way I can see to do this, is to drop the Print Button from the control template and use a custom Document Viewer. A workable style is given at Document Viewer Style.
It still would be nice if I could simply remove the button from the system Document viewer?
Using the method from this answer, you can alter the visibility of the PrintButton programmatically like this. Let's say I put the method in a class called UIElementHelper:
var button = UIElementHelper.FindChild<Button>(docViewer, "PrintButton");
button.Visibility = Visibility.Collapsed;

WPF Dynamic HyperLinks RichTextbox

I have seen several posts on various forms varying in times from 2006 to now about how to add hyperlinks to RichTextBox, but they all seem overly complex for what I want. I am creating a desktop chat client, and I receive input as strings, now among these strings may be some urls, I need those urls to be clickable. Which from what I gather means they need to be HyperLink objects.
Navigating through the RichTextBox and replacing the urls with HyperLinks seems to be no small feat. Does anyone have a relatively simple solution for this?
In my web client it's a simple one liner
value = value.replace(/(http:\/\/[^\s]+)/gi, '$1');
Never thought I'd see the day where C# actually makes it harder.
If you want to do an equivalent of value.replace(/(http:\/\/[^\s]+)/gi, '$1') in WPF:
<RichTextBox x:Name="MyRichTextBox" IsDocumentEnabled="True" IsReadOnly="True" />
And the code that converts the string is the following:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var htmlText = "Google's website is http://www.google.com";
MyRichTextBox.Document = ConvertToFlowDocument(htmlText);
}
private FlowDocument ConvertToFlowDocument(string text)
{
var flowDocument = new FlowDocument();
var regex = new Regex(#"(http:\/\/[^\s]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var matches = regex.Matches(text).Cast<Match>().Select(m => m.Value).ToList();
var paragraph = new Paragraph();
flowDocument.Blocks.Add(paragraph);
foreach (var segment in regex.Split(text))
{
if (matches.Contains(segment))
{
var hyperlink = new Hyperlink(new Run(segment))
{
NavigateUri = new Uri(segment),
};
hyperlink.RequestNavigate += (sender, args) => Process.Start(segment);
paragraph.Inlines.Add(hyperlink);
}
else
{
paragraph.Inlines.Add(new Run(segment));
}
}
return flowDocument;
}
}
It uses the same regular expression you provided, which is lacking if you properly want to recognize URLs with a regular expression. This one doesn't recognize https ones and the last dot in the following sentence would be a part of the URL: "This is a URL: http://www.google.com/."
What the code does is to split the text based on the regular expression, iterate it and adds the correct elements to the FlowDocument constructed on the fly.
Clicking the Hyperlink should open your default browser.
Result:
That said, this is only good for read only usage of the RichTextBox (as indicated by the question in the comment).

Deserializing byte to image from sql through memory stream

I have convert an image to be saved in SQL Server Database as Binary with column name as "img". I have PictureBox1 ready to show the image.
Now I want to import the binary data back into image, and I'm trying this code in VB.net for example:
Dim queries As String
queries = "SELECT * from StudentData where Std_fname='" & ComboBox1.Text & "'"
Dim com As New SqlCommand(queries, sqlconn)
sqlconn.Open()
Dim ds As New SqlDataAdapter(queries, sqlconn)
Dim dr As SqlDataReader
dr = com.ExecuteReader()
While dr.Read
Std_fnameTextBox.Text = dr("Std_fname")
Std_lnameTextBox.Text = dr("Std_lname")
AgeTextBox.Text = dr("age")
AddressTextBox.Text = dr("address")
StateTextBox.Text = dr("state")
CityTextBox.Text = dr("city")
CountryTextBox.Text = dr("country")
Ic_passportTextBox.Text = dr("ic_passport")
DobDateTimePicker.Text = dr("dob")
PictureBox1.Image = dr("img") 'Here is the problem. If I run it, it ask me to convert Binary to Image first.
End While
sqlconn.Close()
The problem is, I don't know how to convert binary to image in this situation. And yes, I've been googling for it, but can't seem to get the right answer.
dr("img") returns either DBNull.Value, or a byte[]. You can use the stream overload of the Bitmap constructor to load this. In C# (should be easy to translate to VB), you can do it like this:
var imageData = (byte[])dr["img"];
using (var ms = new MemoryStream(imageData))
{
var bmp = new Bitmap(ms);
// Work with bmp
}
As Mark correctly noted, you're supposed to keep the stream open for the whole life-time of the Bitmap - this is actually a bit trickier than it seems, because the Bitmap doesn't even keep a reference to the stream.
The easiest way to handle this is to clone the bitmap after you create it, to remove the dependency on the stream. Unless you can do whatever work you need to do within the using - unlikely if you want to display it in a PictureBox.
You can also use ImageConverter.ConvertFrom directly, but all it does is create the MemoryStream if used on raw byte[] data :)

WPF: Convert Datagrid/DataTable to Table for printing

Im trying to 'print' the contents of my datagrid / datatable.
I read something about converting DATAGRID To TABLE To FLOWDOCUMENT but I can't find a source how to do it.
I just need some sample codes on how to convert datagrid/dataTable to Table .
then I will just apply this solution : Printing a WPF FlowDocument
any idea?
Thanks and Sorry about for my noob question.
As far as my research went when I printed something I found that converting UI elements to pages is a wrong thing to attempt. Printing is a lot different then creating UI and there are no scrolls or draggable columns on paper :) so your data for printing will differ form data displayed on your forms.
As far as DataTable goes this is only an representation of data so you can easily produce a table that can be printed it already has rows and columns so it should be straight forward to begin: (note code not tested)
System.Data.DataTable dataTable = GetDataTable();
var table = new Table();
var rowGroup = new TableRowGroup();
table.RowGroups.Add(rowGroup);
var header = new TableRow();
rowGroup.Rows.Add(header);
foreach (DataColumn column in dataTable.Columns)
{
var tableColumn = new TableColumn();
//configure width and such
table.Columns.Add(tableColumn);
var cell = new TableCell(new Paragraph(new Run("column Header")));
header.Cells.Add(cell);
}
foreach (DataRow row in dataTable.Rows)
{
var tableRow = new TableRow();
rowGroup.Rows.Add(tableRow);
foreach (DataColumn column in dataTable.Columns)
{
var value = row[column].ToString();//mayby some formatting is in order
var cell = new TableCell(new Paragraph(new Run(value)));
tableRow.Cells.Add(cell);
}
}
//and print your table
If you ask me I find this API a little bit verbose but for complex documents with proper understanding it is very powerful.

Print FixedDocument programmatically

I am using a WPF FixedDocument with databinding for a simple invoice report. Works perfect when viewed inside the sofware itsself.
But i want to print a series of invoices in one click. The following code works perfect (quick 'n dirty, just loads an invoice one by one directly inside the viewmodel, for testing purposes) when I choose the XPS writer, bu fails to print correctly when printing to a real printer. I can see nothing of the data bound to the report. All the graphical elements such as lines are there, but no data. (When i print, with the same button, to de xps writer printer, all data is present, and correct...)
Any Ideas?
private void ExecutePrintCommand(object sender, ExecutedRoutedEventArgs args)
{
var invs = args.Parameter as IList<object>;
using (CompuDataContext db = new CompuDataContext())
{
DataLoadOptions dl = new DataLoadOptions();
dl.LoadWith<Invoice>(f => f.Invoicelines);
db.LoadOptions = dl;
ReportViewer viewer = new ReportViewer();
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
PrintQueue q = dlg.PrintQueue;
foreach (var o in invs)
{
InvoiceListDisplay inv = o as InvoiceListDisplay;
Invoice invoice = db.Invoices.Single(f => f.Id == inv.Id);
viewer.DataContext = new InvoicePrintViewModel(invoice);
XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(q);
xpsdw.Write(viewer.Document);
}
}
}
}
mmkay, so I found the answer myself here :)
This helped me (Anybody an idea what the 'reason' behind is? Bug?)
PS: In a flowdocument, i experience the same issue, and have not been able to resolve it there. Any ideas?

Resources