Is there a way to do form filling with XPS documents in C#?
If you want to fill a XPS form, yes. XPS is XML, so you can change it.
It wouldn't be an ideal solution. The reason is that text in XPS is represented by Glyphs, in which characters (roughly speaking) are specified as indexes into a particular typeface's table of character shapes, rather than in something handy like Unicode. And the Glyphs
have to be specifically positioned, each character offset from the string origin. If a string has to wrap onto multiple lines, that has to be done by putting in many separate Glyphs
objects, all individually positioned. It's not a logical format, but a physical one, and it isn't particularly amenable to being altered after it's generated.
You'd be much better off defining the template document as a FlowDocument. That's a much more logically manipulable format, rather than a physical "frozen" format. (An XPS document is technically a FixedDocument). You can then freeze a FlowDocument
into a FixedDocument
to export it as XPS, when it has been filled in.
Modifying XPS is certainly possible, it depends a bit on how you want this to work and the amount of programming you are willing to do (but this might be true for every programming issue ;-)).
Anyway: feel free to e-mail me directly on info-at-nixps-dot-com.
Here's how you do it.
First, you create an object (POCO is fine) that contains the form fields you wish to have the user fill in for your form. Next, you create your form as a FixedDocument. It should have Bindings that work against your POCO. Then you create a Window that is set up so that the user can enter values for those fields.
Bind the POCO (plain old CLR object) to the TextBoxes in the Window. After the user has entered the form data, take your now filled-in object and set it as the DataContext of the FixedDocument. Create an XpsWriter and Write your FixedDocument to your output (file, printer, etc).
I'd give you code, but since I'm actually doing this (or something relatively similar) for work I don't think I can. I can tell you that the hardest part is dealing with the XPS document. You can judge relative to your experiences there.
I never found a really great solution, so I made one. Check out www.flexsoftinc.com to see my program XPS Express.