Categories: WPF
Posted by
toddpi314 on
3/14/2009 12:36 PM |
Comments (0)
Read this article in your language IT | EN | DE | ES
The XPS FixedDocument could become the new standard in portable documents. We just need few developer’s to build a strong API and get Microsoft to ramp up!
In the meantime, here is a little help on Printing your [Fixed/Flow]Document:
Creating a Document
First of all, you must create a Package before a document can come into play. This is not clear, since the XPS API has been updated between the Beta, 3.0, and 3.5 release.
Here is one of the most common examples online (http://www.ericsink.com/wpf3d/B_Printing.html):
1: public static void PrintXPS(FixedDocument doc)
2: {
3: string tempPath = "";
4: try
5: {
6: tempPath = System.IO.Path.GetTempFileName();
7: Package package = Package.Open(tempPath);
8: XpsDocument xpsd = new XpsDocument(package);
9: XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
10: xw.Write(doc);
11: xpsd.Close();
12:
13: PrintXPS(tempPath);
14: }
15: finally
16: {
17: if (tempPath.Length > 0)
18: System.IO.File.Delete(tempPath);
19: }
20: }
Of course, this now throws: Archive file cannot be size 0.
Digging through the XPSCreate Sample shows a quick workaround:
Excert from XpsCreate.cs in XpsCreate Sample Provided by MS:
1: if (File.Exists(packageName))
2: File.Delete(packageName);
3:
4: // Create an XpsDocument package (without PrintTicket).
5: using (Package package = Package.Open(packageName))
6: {
7: XpsDocument xpsDocument = new XpsDocument(package);
8:
9: // Add the package content (false=without PrintTicket).
10: AddPackageContent(xpsDocument, false);
11:
12: // Close the package.
13: xpsDocument.Close();
14: }
Integrating this back into our code, we have:
1: tempPath = System.IO.Path.GetTempFileName() + ".xps";
2: Package package = Package.Open(tempPath);
3: XpsDocument xpsd = new XpsDocument(package);
4: XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
5: xw.Write(doc);
6: xpsd.Close();
7:
8: PrintXPS(tempPath);
Its that simple!
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5