4
votes

I've got an issue when trying to print a FlowDocument with paragraphs. When I display the FlowDocument in a window, the paragraphs are rendered correctly without a border.

FlowDocument - No border

However, when I print them (I can only test printing to PDF at this stage, so I can't confirm if this is still an issue when printed to paper), the resulting PDF document has a border around each paragraph.

enter image description here

How can I remove these borders from the document when printing?

This is the code I'm using to print the FlowDocument:

LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue printQueue = localPrintServer.DefaultPrintQueue;

// Create a XpsDocumentWriter object, open a Windows common print dialog.
// This methods returns a ref parameter that represents information about the dimensions of the printer media.
XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
PageImageableArea ia = printQueue.GetPrintCapabilities().PageImageableArea;
PrintTicket pt = printQueue.UserPrintTicket;

if (docWriter != null && ia != null)
{
    DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDoc).DocumentPaginator;
    // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
    paginator.PageSize = new Size((double)ia.ExtentWidth, (double)ia.ExtentHeight);
    Thickness pagePadding = flowDoc.PagePadding;
    flowDoc.PagePadding = new Thickness(
    Math.Max(ia.OriginWidth, pagePadding.Left),
    Math.Max(ia.OriginHeight, pagePadding.Top),
    Math.Max((double)pt.PageMediaSize.Width - (double)(ia.OriginWidth + ia.ExtentWidth), pagePadding.Right),
    Math.Max((double)pt.PageMediaSize.Height - (double)(ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom));
    flowDoc.ColumnWidth = double.PositiveInfinity;
    // Send DocumentPaginator to the printer.
    docWriter.Write(paginator);
}
1

1 Answers

1
votes

I've found that the cause of this issue is Element with Name. The fix is to change the Name to null.

var elementWithName = (ElementType)clone.FindName("elementName");
elementWithName.Name = null;

Where ElementType can be this: (Image taken from https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/flow-document-overview)

Element Type