打印WPF段落时显示无边框时会呈现边框

时间:2018-12-13 21:23:18

标签: wpf printing

尝试打印带有段落的FlowDocument时遇到问题。当我在窗口中显示FlowDocument时,段落将正确呈现而没有边框。

FlowDocument - No border

但是,当我打印它们时(在此阶段我只能测试打印为PDF,因此我无法确认打印到纸上是否仍然存在问题),生成的PDF文档在每个段落周围都有边框。

enter image description here

在打印时如何从文档中删除这些边框?

这是我用来打印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 个答案:

答案 0 :(得分:0)

我发现此问题的原因是带有名称的元素。 解决方法是将“名称”更改为null。

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

ElementType可以是这样的:(图像取自https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/flow-document-overview

Element Type