Erratic behavior with XPS editing: what could be going wrong?
Posted
by
Ariel Arjona
on Stack Overflow
See other posts from Stack Overflow
or by Ariel Arjona
Published on 2010-12-28T18:48:55Z
Indexed on
2010/12/28
18:54 UTC
Read the original article
Hit count: 374
Hello folks,
I'm working on a class that annotates existing XPS documents. The problem I've been having is that some annotations randomly don't make it to the finished document. The following test code is supposed to draw a rectangle on every page. On random pages the rectangle does not appear. Upon inspection of the page XML, the tags for the rectangle are missing. I run the program again and sometimes it appears on that particular page, sometimes it's then missing from some other page, sometimes from all but 1, and so on.
public void TestXpsAnnotate()
{
var xpsFile = this.GetXpsFile();
var xpsDoc = new XpsDocument(xpsFile, FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
// new XPS document
var newFds = new FixedDocumentSequence();
var newDocRef = new DocumentReference();
var newFixedDoc = new FixedDocument();
// get documents
foreach (var docRef in docSeq.References)
{
FixedDocument fixedDoc = docRef.GetDocument(true);
// get pages
foreach (PageContent pageContent in fixedDoc.Pages)
{
var newPageContent = new PageContent();
newPageContent.Source = pageContent.Source;
(newPageContent as IUriContext).BaseUri = ((IUriContext)pageContent).BaseUri;
FixedPage fixedPage = newPageContent.GetPageRoot(true);
var r = new System.Windows.Shapes.Rectangle()
{
Width = 300,
Height = 400,
Stroke = new SolidColorBrush(Colors.Red),
Fill = new SolidColorBrush(Colors.Yellow),
StrokeThickness = 3,
};
//var r = new TextBlock();
//r.Text = "BLAH";
//r.Foreground = new SolidColorBrush(Colors.Red);
var theCanvas = fixedPage.Children.Cast<UIElement>().OfType<Canvas>().First();
theCanvas.Children.Add(r);
Canvas.SetLeft(r, 10);
Canvas.SetTop(r, 10);
fixedPage.UpdateLayout();
newFixedDoc.Pages.Add(newPageContent);
}
}
xpsDoc.Close();
newDocRef.SetDocument(newFixedDoc);
newFds.References.Add(newDocRef);
string outputFile = this.GetOutputFile();
if (File.Exists(outputFile))
{
File.Delete(outputFile);
}
var newXpsDoc = new XpsDocument(outputFile, FileAccess.ReadWrite);
var writer = XpsDocument.CreateXpsDocumentWriter(newXpsDoc);
writer.Write(newFds);
newXpsDoc.Close();
}
This code follows the examples I've seen around the internet and it seems to do what it's supposed to, when it works. Any idea what could be going wrong here?
© Stack Overflow or respective owner