<Rectangle Width="{Binding b}" Height="{Binding tt}" Stroke="Black" StrokeThickness="1" Canvas.Left="{Binding leftb}" x:Name="topPlateRect" Style="{StaticResource topPlateStyle}"><Canvas.Top><MultiBinding Converter="{l:PositionConverterNegative}"><Binding Path="(Canvas.Top)" ElementName="gussetLeftRect"/><Binding Path="Height" RelativeSource="{RelativeSource Self}"/></MultiBinding></Canvas.Top></Rectangle>
<Style x:Key="topPlateStyle" TargetType="{x:Type FrameworkElement}"><Setter Property="Visibility" Value="Collapsed"/><Style.Triggers><DataTrigger Binding="{Binding IsChecked, ElementName=topPlateCB}" Value="true"><Setter Property="Visibility" Value="Visible"/></DataTrigger></Style.Triggers></Style>
When I try to save the above rectangle which is in a canvas as an xps, the rectangle disappears for some reason. However, when I save it as .png the rectangle appears.
public void SaveCanvasAsXps(Canvas canvas, string fileName)
{
SaveAsXPSMultipleFixedDocSeq(fileName, PaginatorToFixedDocSequence(DrawingToPaginator(canvas,pl.Size)));
}
private FixedDocumentSequence PaginatorToFixedDocSequence(DocumentPaginator paginator)
{
MemoryStream strm = new MemoryStream();
Package pkg = Package.Open(strm, FileMode.OpenOrCreate);
string pack = "pack://" + Guid.NewGuid().ToString() + ".xps";
PackageStore.AddPackage(new Uri(pack), pkg);
XpsDocument xpsDoc = new XpsDocument(pkg, CompressionOption.Maximum, pack);
XpsSerializationManager xpsSM = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
xpsSM.SaveAsXaml(paginator);
return xpsDoc.GetFixedDocumentSequence();
}
public void SaveAsXPSMultipleFixedDocSeq(string fileName,FixedDocumentSequence fixedDocSeq)
{
//this.token = ct;
//this.uiTask = uiTask;
if (File.Exists(fileName))
try
{
File.Delete(fileName);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return;
}
XpsDocument xps = new XpsDocument(fileName, FileAccess.ReadWrite, CompressionOption.Normal);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xps);
writer.Write(fixedDocSeq);
xps.Close();
}
private DocumentPaginator DrawingToPaginator(Canvas canvas,Size pageSize)
{
DocumentPaginator paginator = null;
using (MemoryStream ms = new MemoryStream())
{
Canvas canvasTemp = new Canvas();
XamlWriter.Save(canvas, ms);
ms.Seek(0, SeekOrigin.Begin);
canvasTemp = XamlReader.Load(ms) as Canvas;
Section section = new Section();
Paragraph para = new Paragraph();
para.Inlines.Add(canvasTemp);
section.Blocks.Add(para);
section.BreakPageBefore = true;
fd = new FlowDocument();
//fd.PageWidth = PrintLayout.A4.ColumnWidth;
fd.Blocks.Add(section);
}
if (fd.Blocks.Count > 0)
{
//fd.Blocks.Add(para);
paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator;
//paginator = new DocumentPaginatorWrapper(paginator, pl.Size);
paginator = new DocumentPaginatorWrapper(paginator, pageSize);
paginator.PageSize = pl.Size;
//documentPaginators.Add(key, paginatorWrapper);
//bool ss = paginatorWrapper.IsPageCountValid;
}
//return paginatorWrapper;
return paginator;
//}
}
Is there anyway that I could use style and at the same time save the canvas as xps? Thanks.