top of page

Plot DVHs with OxyPlot (Part 4: Exporting as a PDF)

This blog post is part of a series on plotting with OxyPlot (see Part 1, Part 2, and Part 3).


It’s often the case that you want to export your plot to include it as part of a report document. You can do this using OxyPlot’s PDF exporting functionality. In this post, you’re going to expand on the DVH plotting script to let the user right-click on the plot, choose to export it as a PDF, and generate a PDF containing the plot. Open the MainView.xaml file and change the PlotView element to the following:


<oxy:PlotView
    Grid.Column="1"
    Model="{Binding PlotModel}"
    >
    <oxy:PlotView.ContextMenu>
        <ContextMenu>
            <MenuItem
                Header="Export to PDF ..."
                Click="ExportPlotAsPdf"
                />
        </ContextMenu>
    </oxy:PlotView.ContextMenu>
</oxy:PlotView>

The main change is the addition of a ContextMenu element inside the PlotView. The ContextMenu defines a list of MenuItems that will appear when the user right-clicks on the plot. The only item is the "Export to PDF...", which calls the ExportPlotAsPdf method when the item is clicked. The ExportPlotAsPdf needs to be defined in the MainView.xaml.cs file. Open it and add the method as follows:


private void ExportPlotAsPdf(object sender, RoutedEventArgs e)
{
    var filePath = GetPdfSavePath();
    if (filePath != null)
        _vm.ExportPlotAsPdf(filePath);
}

private string GetPdfSavePath()
{
    var saveFileDialog = new SaveFileDialog
    {
        Title = "Export to PDF",
        Filter = "PDF Files (*. pdf)|*. pdf"
    };

    var dialogResult = saveFileDialog.ShowDialog();

    if (dialogResult == true)
        return saveFileDialog.FileName;
    else
        return null;
}

First, the output file path (that is, location) is obtained from the user using the GetPdfSavePath helper method (more on this below). If the file path is not null, the ExportPlotAsPdf method of the view model is called, passing the path where the PDF file should be saved to.


In the GetPdfSavePath method, the user is shown the typical save dialog box. You don’t need to create this dialog box yourself because it already exists in the Microsoft.Win32 namespace. To use it, you need to create a new instance of SaveFileDialog and specify any relevant properties. Here, you set the dialog box’s title and the path filter. The filter causes only PDF files to be shown in the dialog, and when you type your path without the ".pdf" extension, it is automatically added.


Next, the dialog box is shown using the ShowDialog method. After the user chooses where to save the file and clicks OK, the ShowDialog method returns true. If this is the case, the FileName property of the dialog box, which stores the file path that the user selected, is returned. If the user clicks on the Cancel button on the dialog box, the method returns null. (This is why the file path was checked whether it was null in the ExportPlotAsPdf method.)


The ExportPlotAsPdf method in the MainView hasn’t actually done much, other than get the file path from the user. The real work is done by the ExportPlotAsPdf method in the MainViewModel. Open the MainViewModel.cs file and add the following method:


public void ExportPlotAsPdf(string filePath)
{
    using (var stream = File.Create(filePath))
    {
        PdfExporter.Export(PlotModel, stream, 600, 400);
    }
}

First, a Stream object is created from the file path and wrapped in a using statement. A stream provides a generic way of accessing a sequence of bytes, such as a file. The using statement manages when to close and dispose of the stream when you’re done using it. You can learn more about these concepts in any C# reference book.


The PdfExporter class from OxyPlot has a static Export method. You pass it the plot model, the stream, and the desired width and height of the plot. It then creates a PDF version of the plot and saves it to the stream (which in this case is a file). Notice that you didn’t need to pass the PlotView to the Export method, only the PlotModel. This is because the PlotModel contains everything that should be in the plot. PlotView uses it to render the plot on the screen; PdfExporter uses it to export it to a file.


Now, run the script. Choose some DVHs to plot, and then right-click on the plot. Choose to export as a PDF, and then specify the location to save it to. When done, you’ll be able to open the PDF file containing the plot. You can then use this PDF to add to a report document.


OxyPlot also comes with the classes PngExporter and SvgExporter to let you export your plot as a PNG or SVG file.

Related Posts

See All

ESAPI Essentials 1.1 and 2.0

A few months ago, I introduced ESAPI Essentials—a toolkit for working with ESAPI (available via NuGet). I've recently added one major feature: asynchronous access to ESAPI for binary plugin scripts. Y

Announcement: ESAPI Subreddit

A few months ago, Matt Schmidt started the ESAPI subreddit. It's another useful resource for finding and discussing ESAPI-related topics. According to the description, This community is to post and di

Dump All Patient Data from ESAPI

If, for whatever reason, you need to dump out all of the data for a patient from ESAPI, there's a quick and dirty way to do it. Well, there are probably several ways to do it, but here's one I've foun

Comments


bottom of page