问题
I have got an application that is supposed to send a formatted document to a printer with some barcodes.
I've made other applications that work with printers and print directly through the printserver by sending a xps file, so I thought I would try to see if I could make a .xps file, change the text and be done with it, however every article I can find on the net has to do with creating xps files and not changing them. I feel like it should be possible, and it would be nice not to have to resort to installing Office on the server and print through there. Then I might as well use Open XML and a .docx file.
It is very simple. Let's say I want to change the text INCNUMMER in a .xps file to "testing123". How would I go about that?I have tried the whole unzip, open the xml, find the text, edit, rezip but I'm afraid there's too much about the .xps format I don't understand to make that work.
Best regards, Kaspar.
回答1:
As you already know, an XPS file is just a ZIP archive containing a number of files and folders that have particular names and a defined structure.
At the root level there is a Documents
folder which will typically contain just a single document folder named 1
. Inside that is a Pages
folder containing one or more .fpage
files: these define the content of each page in the document.
Documents
1
Pages
1.fpage
2.fpage
etc
If you open up these .fpage
files in a text editor you will see that they are just XML files. Each page is typically represented by a <Canvas>
element that contains multiple <Path>
and <Glyphs>
elements (text is represented by the latter). However, even though <Glyphs>
elements do have a UnicodeString
attribute the value of that attribute cannot be changed in isolation.
Each <Glyphs>
element also has an Indices
attribute. If you remove this attribute altogether and change the UnicodeString
attribute at the same time, this almost works. However, you will probably find that when viewing the file in the XPS Viewer application certain characters in the text are replaced by question mark symbols.
Font glyphs are embedded in the XPS file (odttf
files in the Resources
folder), and the software that generated the XPS file will only embed glyphs that are used in the source document. For example, this means that (for a given font) if you did not use the letter "A" in the source document, then the glyph for that letter will not be written to the resources of the XPS file. Hence if you change the UnicodeString
attribute to include a letter "A" then that character will display as a question mark in the viewer because it has no glyph resource that tells it how that character must be drawn.
If you have control over the source document (the one that later gets converted to XPS) then I suppose you could include a piece of text containing all of the characters that you are likely to use, and set its colour to white so that it doesn't print, but I'm not sure whether the XPS printer driver would strip that text out anyway. If it didn't then you could probably do something like this:
- Open the relevant
.fpage
XML file - Search all
UnicodeString
attributes of<Glyphs>
elements to find the text you want - Replace that text with something else
- Remove the
Indices
attribute from the changed<Glyphs>
elements - Save the updated XML back to the file
- Re-zip then change the extension from ZIP to XPS
来源:https://stackoverflow.com/questions/28165478/edit-xps-content