Question


I'm currently facing a problem with printing a Plotmodel to an XPS-File.

What I have so far:


/// <summary>
/// Plotmodel to XPS-File
/// </summary>
/// <param name="model">The model</param>
/// <param name="fileName">The fileName</param>
/// <param name="width">Width of the model</param>
/// <param name="height">Height of the model</param>
public void Export(PlotModel model, string fileName, double width, double height)
{
   try
   {
      using (Package xpsPackage = Package.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
      {
        using (var doc = new XpsDocument(xpsPackage))
        {
            var g = new Grid();

            var p = new OxyPlot.Wpf.Plot { Model = model }; //Problem?
            g.Children.Add(p);

            var size = new Size(width, height);
            g.Measure(size);
            g.Arrange(new Rect(size));
            g.UpdateLayout();


            if (xpsdw == null) //XpsDocumentWriter xpsdw
                xpsdw = XpsDocument.CreateXpsDocumentWriter(doc);
            }
            if (xpsdw != null)
            {
                xpsdw.Write(g);
            }
        }
    }
}

This Code works fine (once), but the Problem is: No matter how often you use this method, you will always have just one Page with data. So if you want to print a second Plotmodel into the XPS-File the old one is deleted and you can only see the new one.

So the Question is: Do you have an idea, how to append the new Plotmodel to the old XPS-File without overwriting it?

I also tried to use:

XpsExporter.Export(model, fileName, width, height);

instead of my function, but this didn't worked either.

Was it helpful?

Solution

If you know in advance that you will be writing multiple plots to a single file, you should look into VisualsToXpsDocument.

If you truly must append additional pages to an existing document on disk, it will be more difficult. You will need to dig down through the existing document to get to a FixedDocument (a child of the FixedDocumentSequence). Once you have the FixedDocument, you can add a new PageContent object to it. There is an example near the bottom of this thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top