What's the point of XPS? - wpf

When I read books about WPF, I saw the authors mention XPS like it was something important. Windows also includes its XPS viewer, and I've seen that listed as a "feature" of Windows.
But why? What's the point? Who the heck uses it? It's my understanding that XPS is, basically, like PDF, xhtml, or ePub (which is just xhtml)...or even Word's docx format. Many of the features are the same among those formats.
It doesn't seem to have any major benefits compared to any of those other formats. It seems to me that xhtml would be so much more useful than XPS as a way to save and load FlowDocuments from the RichTextBox. I've looked at multiple blogs about converting between the two. Most or all of the rich text on the internet is (x)html. Beyond that, I don't think anyone uses it just to publish their docs; PDF is preferred. It seems like XPS is just some random format that MS made and decided to push. I generally love MS, but they do have a habit of that kind of thing. Couldn't MS have made an api using xhtml instead? That would have been more useful in a lot of situations, I'd think.
So, is there a point to using XPS, particularly in comparison to one of the other formats I mentioned (or any I haven't)? Have you ever used XPS in your programs or otherwise?

As U62 already stated, WPF comes with a DocumentViewer control which enables you to view XPS documents. The DocumentViewer also has some useful fonctions like Print, Zoom, FitToPage etc... So you don't need to implement that or use a third party tool.
What I just finished an hour ago using XPS and the DocumentViewer was some kind of "Adress label print preview". Allow the user to select some contacts from a list of contacts, click "Print Preview". This opens a new XAML Window which contains a DocumentViewer control and a ListBox with the choice of different Labels (e.g. 1 sheet with 12 labels [2 columns, 6 rows], 1 sheet with a single label whose width and height can be user defined).
Based on the users selection, I generate an XPS Document in the layout the user selected with the adresses of the selected contacts. If e.g. the user selected 4 contacts and wishes to print them on "SingleLabelSheet"'s, I generate 1 XPS document with 4 pages, each page containing 1 Adress. Then I display the XPS in the DocumentViewer and the user can print the labels on our Label Printer.
Once I understood how the XPS API worked (at least the Basics), it was a matter of 2 hours to get this up and running.
So, basically, I see XPS as an easy to use API to display FixedDocuments which are to be generated on the fly. But I wouldn't personnally go about saving them to my HDD or somehow modify them or whatever you generally do with documents.

The only actual advantage I can think of is that you have a control for viewing XPS documents in WPF applications. The other formats you mention mean you would have to bring in a 3rd party renderer (or write one yourself if you have a year to spare).
btw. I don't know much about ePub, but XPS isn't directly comparable with XHTML, it's more like PDF in that it's designed to have a fixed layout.

XPS to WPF is like WMF to Win32/WinForms, it's a persistent format that let you store and print native WPF graphics.
XPS is used to print from WPF (even when you print directly from the application without saving, the internal printing system is built on XPS) so what should MS do:
Create a new file format that exactly fit with what they are trying to do
Build a 100% perfect translator from WPF to a format they don't control like PDF (and hope Adobe doesn't break all WPF applications out there with the next release of Acrobat Reader).
What would you do?
Saving XPS files is just a nice bonus.

Look, I may be a pessimist on XPS as a report generation solution, but I gave it a go and found the initial documentation to be hard to understand, with less real world samples out there than what I would have liked. When I put it into a real world business application I found it to be frustrating, particularly in LOB apps that require tables that span over multiple pages.
Things may have changed since then but as soon as I started looking at tables that spanned over several pages and I wanted column headers to go to the top, etc. I found that the API required me to do what I would call excessive workarounds with unnecessary complexity.
So, things may have changed since then (about 8 months ago), but I went from XPS to using ITextSharp and that has been a lot less painful.
So the only advantage I would say, like everyone else, is the built in viewer in WPF - but other than that I feel the API may need to "mature" a bit more before I will attempt something again in it.

Actually I found a really nice reason to use XPS. I wanted to print from multiple sources, merge documents and specify duplex and stapled. Finally it should be printed as one document with duplex and stapled. I was having a difficult time doing so but found that by printing to XPS (saved to disc) I could accomplish my goal with minimum fuss. I haven't found any other method that is so easy and straightforward.
Dim PrintServer As New SysPrint.PrintServer("\\" & My.Computer.Name)
Dim PrintQ As New SysPrint.PrintQueue(PrintServer, "Ricoh Main")
Dim Jobs As SysPrint.PrintJobInfoCollection = PrintQ.GetPrintJobInfoCollection
Dim able As SysPrint.PrintCapabilities = PrintQ.GetPrintCapabilities()
Dim CurrentTicket As SysPrint.PrintTicket = PrintQ.CurrentJobSettings.CurrentPrintTicket
If able.StaplingCapability IsNot Nothing AndAlso able.StaplingCapability.Count > 0 Then
If able.StaplingCapability.Contains(Printing.Stapling.StapleTopLeft) Then
CurrentTicket.Stapling = Printing.Stapling.StapleTopLeft
End If
Else
Debug.Print("no stapling capability")
End If
CurrentTicket.Duplexing = Printing.Duplexing.TwoSidedLongEdge
Dim fiName As String = "S:\Temp\PS\XPS\Test.xps"
Dim TestJob As SysPrint.PrintSystemJobInfo _
= PrintQ.AddJob("Test job", fiName, False)

XPS (code named "metro") was clearly designed as the Microsoft alternative to PDF and PostScript in Windows Vista. See this old article here: Microsoft Readies New Document Printing Specification

There are different approches when inventing document formats:
Some are made for quick view on screen (say low def) like HTML, doc.
Some other are (or can be) very respectuous of printing contrains (say quality based) like PDF, XPS.
So XPS whiches to be as good as PDF, plus it is formated in XML, so "maybe" it is more standard than the stream approch in PDF.
The black spot of XPS is maybe the management of special colors (Pantone, paper effects).

Related

Silverlight printing size is too large

I've developed application using Silverlight, and now I need to implement printing, I've used
PrintDocument class, it looks like this:
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);
printDocument.Print("My docuement");
But I've faced with problem - it takes much time if there are 20-30 pages, and if I open printer's queue, it shows that printing size is about 1.2GB for 10 printed pages. I've tried to print canvas (800*1000) with only one textblock with simple text like "Test printing".
I'm using Silverlight 5.
Did anybody faced with such problem? Is there any way to avoid it?
My guess is that the content to be printed is send as a bitmap instead of a more efficient vector format.
This might be due to the driver. If the driver of the printer is not able to translate the Silverlight graphics into vectors, all it can do is print it as a bitmap.
I tried to force Silverlight into using Vectors but my printer didn't support PostScript.
So I switched to a printer that does and suddenly it's just kilobytes.
I see only two ways to avoid bitmap printing:
Get a PostScript printer or
Create a printer friendly document (PDF, Word, text) on the server and allow the client to download and print it by hand.

Highlight text in a PDF document viewer

I have a WinForms application and am using AxAcroPDF COM component to display PDF files in my application. I want to highlight occurrences of some text in the PDF document using the AxAcroPDF component. Is there a way to do that?
just since there are no answers here - according to Adobe's API there is a method SetCurrentHighlight, which hightlights text within the specified rectangle (I haven't tried this):
SetCurrentHighlight
Highlights the text selection within the specified bounding rectangle
on the current page.
Syntax void setCurrentHighlight(LONG nLeft, LONG nTop, LONG nRight,
LONG nBottom);
Another alternative way is there, you may like to do all kind of text editing features using Foxit PDF Reader. It is now available for major platforms like linux, Windows and Mac.
Mendeley (https://www.mendeley.com/) does the work like a charm!
In fact, Mendeley is more than a PDF-reader. You can use it to organize your docs and references .

Printing text from silverlight application

I'm trying to print a report (just text in columns) from my Silverlight 4 application. The only way I know about is to use PrintDocument and set the PageVisual to a framework element showing the report. However, this results in an insanely large print job (like 120 MB). This is not what my customer wants. Is there any other solution?
The problem with Silverlight 4 is that everything you go to print is produced as a rasterized image at 600 DPI. This explains the large job size you are seeing. This is also why text from Silverlight can look very fuzzy compared to what you'd expect from your output. Silverlight 5 will address this with Postscript vector-based printing, but for now you are facing the reality that without third-party solutions you are effectively stuck with the out of the box limitations or using HTML.
If you are willing to generate the report from HTML being served server-side you may have the best solution considering what you're doing. In that case you would at least not have to worry about complexities like pagination as part of printing from Silverlight.
A good third-party solution for reporting, but perhaps more than what you need, is to use Reporting Services coupled with this product: http://www.perpetuumsoft.com/Silverlight-Viewer-for-Reporting-Services.aspx?lang=en
One easy solution is to print web page directly from browser (I assume this is not what you want).
I think the PrintDocument and PageVisual is the only way how to print from inside of SL.
I did some printing even with large visual structures, but I haven't checked how large my print job is, so I can't tell you if it is normal or not.
Anyway, you can try not to print directly the element you want to print, but made some other lightweight one which will serve only for printing purposes and fill it with same data and print that lightweight element.
You can convert your text into picture and then print picture.. mb this will help you
http://www.andybeaulieu.com/Home/tabid/67/EntryID/161/Default.aspx
http://www.snowball.be/Printing+In+Silverlight+3+Yes+We+Can.aspx

How should I use .ico files in a Winforms Application?

I'm developing a WinForms c# 3.0 application. Our designer created quite a lot of .ico files containing all the needed art. The choice of .ico was made because quite often, the same image is needed in several places in different dimensions.
Now, it seems .ico files are really annoying to use in visual studio. The only way to use those images seems to be through images list (which aren't supported by all controls).
Compared to other resources, you can't write this :
foo.Image = global::RFQHUB.RFQHUBClient.Properties.Resources.foo; // Cannot implicitly convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
Here are the options I'm considering :
create ImageLists of all possible sizes referencing all my icons in my main window. Link these ImageLists from other windows and find a way to export Image objects from the ImageList when I can't use it directly ; since ImageList contains a Draw() method, this should probably be possible.
convert all the x.ico I've got in several x16.gif ...x48.gif, and use those through resources.
I'd be interested to know if some people have been successfully using .ico resources in a Winform application. In so, how did you set up things ?
ICO isn't quite an obsolete format, but it's close. It's still useful for your application icon, but for almost everything else, it's better to use an ImageList for each size that you need. And it's much faster to populate an ImageList from a bitmap that contains multiple images layed out in a grid.
You also want to use an Alpha channel transparency in your bitmaps to get the best result, so storing them as .PNG files in your resources is the best way to go, since PNG supports an alpha channel. ICO and GIF files support only single bit for transparency - every pixel is either fully opaque or fully transparent. An 8 bit alpha channel for transparency looks much nicer.
If you can send your artist back to the drawing board then you should do so, and have him/her do full anti-aliased images with alpha. If you can't, then I suggest that you write a small program to convert all of your icon files into bitmaps suitable for loading into ImageLists.
Convert the images into PNG. Point. Whoever decided to use .ico files to start with should get talked to in private - the argument holds no ground.

Get font name of odttf files generated by XPS document writer

I have pdf file using only 2 type of fonts.
But when exporting the pdf to xps format, there are odttf files as many as glyphs elements in generated xps document.
Now I want every glyphs with the same font type to have the same FontUri value. But how to identify if 2 odttf files is the same font type?
Sometimes (but not always) it is possible to figure out the font name by looking at the final part of the odttf file. In fact, I am routinely extracting font names from odttf files (generated by the XPS Document Writer) with a python script by looking at what immediately precedes and follows the phrase "This is a unique ID" that can be found in the last 100 or so bytes of every odttf file that I have seen so far. Of course, this is a terrible hack - but it works (for me, at least).
Whether this really works seems to depend on what program generated the PDF file and/or what program is used to print the PDF file to the XPS Document Writer and/or what specific fonts the PDF document contains.
My experience is mostly limited to converting PDFs generated by pdflatex in this way: as an online math tutor I need to convert formulas, text and drawings from PDF to XAML in order to be able to paste them into a shared whiteboard that is based on WPF. So this hack might or might not work for you. (Also: this hack might stop working any time...)
Instead of trying to figure out the font name you could also convert your PDFs to XAML by eliminating the font references entirely: You can crop the PDF with pdfcrop (by Heiko Oberdiek) before sending it to the XPS Document Writer. In my experience this forces the combination of Adobe Reader and XPS Document Writer (for reasons unknown to me) to produce XAML code that does not contain any Glyphs, and thus no references to fonts (contained in obfuscated odttf files). But, unfortunately, you get much more XAML code, because all glyphs have to be represented by paths (which involves a great deal of repetition).
Regards,
Christian

Resources