I attempted to export a Label control with a white background and noticed some border lines in the output file. Is there a workaround for this problem?
UI As seen below:
Exported output
Xaml Code
<Canvas x:Name="sampleCanvas" Grid.Row="1" Grid.Column="0">
<Canvas>
<Grid x:Name="SimpleNode" Height="50" Width="50" Margin="200,200" Background="CornflowerBlue">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Canvas>
<Border x:Name="Bd" BorderThickness="1"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Border BorderThickness="1" Margin="2">
<Grid Name="parent">
</Grid>
</Border>
</Border>
<Label x:Name="lbl" BorderBrush="Transparent" Background="#FFFF" BorderThickness="0"
Content="Node Text" FontSize="12" FontFamily="Calibri"
Canvas.Top="40"
Panel.ZIndex="2" HorizontalAlignment="Center">
</Label>
</Canvas>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</Canvas>
</Canvas>
Code Used for Printing
private void Button_Click(object sender, RoutedEventArgs e)
{
var filename = SaveCurrentDocument();
PrintDialog printDialog = new PrintDialog();
if ((bool)printDialog.ShowDialog())
{
PrintXPSDocument(printDialog, filename);
}
}
public string SaveCurrentDocument()
{
var filename = @"D:\exportWPF.xps";
File.Delete(filename);
using (Stream stream = File.Create(filename))
{
this.InvokeXps(stream);
}
return filename;
}
private void InvokeXps(Stream stream)
{
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = CreateFixedPage(new Rect(0, 0, 500, 500));
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(fixedDoc);
doc.Close();
package.Close();
}
internal FixedPage CreateFixedPage(Rect area)
{
VisualBrush visualBrush = new VisualBrush(sampleCanvas);
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
RenderOptions.SetBitmapScalingMode(visualBrush, BitmapScalingMode.HighQuality);
visualBrush.Stretch = Stretch.None;
visualBrush.ViewboxUnits = BrushMappingMode.Absolute;
visualBrush.Viewbox = area;
rect.Fill = visualBrush;
rect.Stretch = Stretch.Fill;
rect.Width = area.Width;
rect.Height = area.Height;
rect.Arrange(new Rect(area.Size));
FixedPage fp = new FixedPage();
fp.Width = area.Width;
fp.Height = area.Height;
fp.Children.Add(rect);
fp.Arrange(new Rect(area.Size));
return fp;
}
internal void PrintXPSDocument(PrintDialog printDialog, string filePath)
{
var document = new XpsDocument(filePath, FileAccess.Read);
var sequence = document.GetFixedDocumentSequence();
FixedDocument fixedDocument = sequence.References[0].GetDocument(false);
fixedDocument.DocumentPaginator.PageSize = new System.Windows.Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
printDialog.PrintDocument(fixedDocument.DocumentPaginator, "Printing");
document.Close();
}
I attempted to export my canvas control to.xps format and then print it using the Print Dialog via the shared code.


If you just want text then you should use a textblock rather than label.
Instead of
You can use a textblock
The functionality is very similar.
When you set content of a label to a string, a label creates a textblock as it's content and sets text to the string.
If you need the white background then you could try setting the height and width of the textblock but it seems you already have a white background anyhow.