MisterMystery MisterMystery - 26 days ago 5
C# Question

How to remove FixedPage from FixedDocument?

I am using a DocumentViewer to display a FixedDocument to the user before allowing the user to print or save the document. I have edited the DocumentViewer panel to have a checkbox that allows the user to select if they want a chart in the document or not.

I am able to get the event of the checkbox being checked and its value but I cannot figure out a way to remove the chart from the FixedDocument and not leave a blank page at the end. I currently have it so that on the checkbox click I hide and show the Chart but again this leaves a blank page.

if (value) BarChart.Visibility = Visibility.Visible;
else BarChart.Visibility = Visibility.Collapsed;

I have also tried rebuilding the FixedDocument on the checkbox event and use the checkbox value to decide whether or not to include the graph page. The document renders fine the first time, then after unchecking the checkbox the chart page disappears, then when rechecking the checkbox the graph page shows up but the graph has no data (series are missing).

Any ideas?

Could this be a problem with the data binding of the chart or an issue with the FixedDocument?

I am building the chart in the code behind and adding it like so:

// Add other pages to ReportDocument here

if (IncludeGraphPage)
// Add graph page
PageContent ChartDocumentPageContent = new PageContent();
FixedPage ChartDocumentPage = new FixedPage();
ChartDocumentPage.Margin = new Thickness(20);

// Set the document viewer content
DocumentViewer.Document = ReportDocument;

I have checked out other SO questions but no solutions have worked for me yet.

Answer Source

I was able to figure out a way to rebuild the FixedDocument without making the chart series disappear. The key was to add the chart as a VisualBrush like so:

VisualBrush chartBrush = new VisualBrush(BarChart) { Stretch = Stretch.Uniform };
Rectangle ChartRectangle = new Rectangle { Width = (1056-2*96), Height = (816 - 2*96), Fill = chartBrush };