How to replace a FixedDocument in an XPS file with another FixedDocument?
I have an existing XPS file and it contains one document. I'm trying to
replace this document with another one.
I try to do it as below but get a "Package already has a root
DocumentSequence or FixedDocument." exception.
using (var xpsDoc = new XpsDocument(_Url, FileAccess.ReadWrite))
{
string path = @"C:\Users\Wesley Manning\Desktop\test.xps";
FixedDocument fd_read;
using (var xpsDocToRead = new XpsDocument(path, FileAccess.Read))
{
FixedDocumentSequence fds_read =
xpsDocToRead.GetFixedDocumentSequence();
DocumentReference dr_read = fds_read.References[0];
fd_read = dr_read.GetDocument(false);
}
FixedDocumentSequence fds = xpsDoc.GetFixedDocumentSequence();
DocumentReference dr = fds.References[0];
dr.SetDocument(fd_read);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
writer.Write(fds);
}
Is there any way to replace the existing document without having to
overwrite the entire file? I can do it by overwriting the entire file but
I don't want to do that.
The reason I don't want to overwrite the file is that it is a custom file
format (mine) and the OPC package contains other data not related to XPS.
If I overwrite the file I would have recopy the data to the new file or
lose it. Also lets say in future there could be multiple documents in the
same XPS file.
FYI: I want to use the XPS payload/portion of the package to easily
present a "preview" of the data to the user.
No comments:
Post a Comment