I've got a problem whereby I've using this code to apply a watermark to each page in an Xps document.
private static XpsDocument AddWatermark(string watermark, Package container, XpsDocument xpsOld)
{
// Open original XPS document
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
// Create new XPS document
Uri u = new Uri("pack://TemporaryPackageUri.xps");
try
{
PackageStore.RemovePackage(u);
}
catch { }
PackageStore.AddPackage(u, container);
var newdoc = new XpsDocument(container, CompressionOption.Maximum, u.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(newdoc);
// Needed for writing multiple pages
SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
int pageno = 1;
foreach (DocumentReference r in seqOld.References)
{
FixedDocument d = r.GetDocument(false);
// Walk through each page
foreach (PageContent pc in d.Pages)
{
FixedPage fixedPage = pc.GetPageRoot(false);
double width = fixedPage.Width;
double height = fixedPage.Height;
Size sz = new Size(width, height);
// Convert to WPF Visual
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
fixedPage.UpdateLayout();
ContainerVisual newpage = new ContainerVisual();
newpage.Children.Add(fixedPage);
if (!string.IsNullOrEmpty(watermark))
{
newpage.Children.Add(CreateWatermark(width, height, watermark + " (" + pageno + ")"));
}
pageno++;
// Write out modified page
vxpsd.Write(newpage);
}
}
vxpsd.EndBatchWrite();
container.Flush();
// this part serializes the doc to a stream so we can get the bytes
var ms = new MemoryStream();
var writerx = new XpsSerializerFactory().CreateSerializerWriter(ms);
writerx.Write(newdoc.GetFixedDocumentSequence());
xpsOld.Close();
return newdoc;
}
It basically takes an Xps document as an argument and iterates through each page, creating a visual and adding diagonal watermark across it.
The problem I have is that it loses an image embedded in the original Xps.
Is there something I'm missing? like a series of embedded resources I need to copy?
Any help appreciated, I'm not that familiar with Xps docs.
EDIT 1: This is actually the method thats causing the problem
public static void PrintFlowDocument(PrintQueue printQueue, DocumentPaginator document, string watermark)
{
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
Uri u = new Uri("pack://TempTemporaryPackageUri.xps");
PackageStore.AddPackage(u, package);
XpsDocument doc = new XpsDocument(package, CompressionOption.NotCompressed, "pack://TempTemporaryPackageUri.xps");
XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
xpsDocumentWriter.Write(doc.GetFixedDocumentSequence());
PackageStore.RemovePackage(u);
}
This is the function that calls (although I've omitted to avoid confusion) the addwatermark function. All I'm doing is creating a new xps and adding the Document sequence to it (following the addition and scaling of watermark).
After this, the embedded XpsImage is a 1x1 black pixel... presumably as I need to copy resources (thanks Jon) to it.
Any suggestions on how I complete this?
Thanks