Label control - Border lines issue on PDF export

Balamurugan Rajaraman 5 Reputation points
2023-01-19T12:33:27.3533333+00:00

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:

UI

Exported output

Output

Xaml Code

  

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.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,185 questions
No comments
1 vote