打印FlowDocument时如何删除段落间距?

时间:2014-05-05 18:36:23

标签: wpf flowdocument

我正在代码中创建FlowDocument,我会立即将其发送到默认打印机。一切都很好,除了我在每个段落之间有一个可怕的间距。我使用段落作为原子元素,不会破坏分页符。

我试过这个:How to adjust the space between paragraph in a flow document by programming

它什么都不做。我尝试了上述解决方案的替代版本,如下所示:

paragraph.Margin = new Thickness(0);

以下是代码:

var flowDocument = new FlowDocument();
flowDocument.FontFamily = new FontFamily("Segoe UI");
flowDocument.FontSize = 14;

var strings = new PrintPartsListViewStrings();

var jobName = new Paragraph(new Run(String.Format("{0}{1}", strings.JobNameLabel, JobName)));
jobName.KeepTogether = true;

flowDocument.Blocks.Add(jobName);

foreach (var nestedCabinet in _nestedCabinets)
{
    var cabinetInfo = new Paragraph(new Run(String.Format("{0}   {1}", nestedCabinet.CabinetName, nestedCabinet.Size)));
    cabinetInfo.KeepWithNext = true;
    cabinetInfo.Margin = new Thickness(0);

    flowDocument.Blocks.Add(cabinetInfo);

    var plTable = new Table();
    plTable.Margin = new Thickness();

    var plFigure = new Figure(plTable);
    plFigure.CanDelayPlacement = false;
    plFigure.Margin = new Thickness(0);

    var partsList = new Paragraph(new Figure(plTable));
    partsList.KeepTogether = true;
    partsList.Margin = new Thickness(0);

    var plRowGroup = new TableRowGroup();
    plTable.RowGroups.Add(plRowGroup);

    plTable.Columns.Add(
        new TableColumn 
        {
            Width = new GridLength(250, GridUnitType.Pixel),
        },
        new TableColumn 
        {
            Width = GridLength.Auto,
        },
        new TableColumn 
        {
            Width = GridLength.Auto,
        },
        new TableColumn 
        {
            Width = GridLength.Auto,
        }
        );


    var plHeaderRow = new TableRow();
    plHeaderRow.Cells.Add(new TableCell());
    plHeaderRow.Cells.Add(new TableCell(new Paragraph(new Run(strings.PartLengthLabel))));
    plHeaderRow.Cells.Add(new TableCell(new Paragraph(new Run(strings.PartWidthLabel))));
    plHeaderRow.Cells.Add(new TableCell(new Paragraph(new Run(strings.PartQuantityLabel))));
    plRowGroup.Rows.Add(plHeaderRow);

    foreach (var part in nestedCabinet.Doors)
    {
        var plRow = new TableRow();
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(part.PartName))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(DimensionConverter.Format(part.Length)))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(DimensionConverter.Format(part.Width)))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(part.Quantity.ToString(CultureInfo.InvariantCulture)))));
        plRowGroup.Rows.Add(plRow);
    }

    foreach (var part in nestedCabinet.Drawers)
    {
        var plRow = new TableRow();
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(part.PartName))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(DimensionConverter.Format(part.Length)))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(DimensionConverter.Format(part.Width)))));
        plRow.Cells.Add(new TableCell(new Paragraph(new Run(part.Quantity.ToString(CultureInfo.InvariantCulture)))));
        plRowGroup.Rows.Add(plRow);
    }

    flowDocument.Blocks.Add(partsList);
}

var pd = new PrintDialog();
flowDocument.PageHeight = pd.PrintableAreaHeight;
flowDocument.PageWidth = pd.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(50);
flowDocument.ColumnGap = 0;
flowDocument.ColumnWidth = pd.PrintableAreaWidth;

IDocumentPaginatorSource dps = flowDocument;
pd.PrintDocument(dps.DocumentPaginator, "flow doc");

2 个答案:

答案 0 :(得分:2)

您想为段落设置LineHeight1

答案 1 :(得分:0)

你会注意到我没有使用plFigure,而是创建了一个新的图形放入段落。我解决了这个问题。我遇到的真正问题是Figure对象有一个默认的Padding。我把它设置为零,一切都很好。

var plFigure = new Figure(plTable);
plFigure.CanDelayPlacement = false;
plFigure.Margin = new Thickness(0);
plFigure.Padding = new Thickness(0);

var partsList = new Paragraph(plFigure);