【问题标题】:Printing WPF paragraph renders border when displaying it has no border打印 WPF 段落在显示没有边框时呈现边框
【发布时间】:2019-05-15 03:59:50
【问题描述】:

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

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

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

这是我用来打印 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);
}

【问题讨论】:

    标签: wpf printing


    【解决方案1】:

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

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

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

    【讨论】:

    • 克隆为 FlowDocument
    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2011-12-03
    相关资源
    最近更新 更多