1

I am creating a FlowDocument in code that I immediately send to the default printer. Everything works fine except that I have a hideous spacing between each paragraph. I am using paragraphs as atomic elements that will not break up over page breaks.

I've tried this: How to adjust the space between paragraph in a flow document by programming

It does nothing. I tried an alternate version of the above solution as follows:

paragraph.Margin = new Thickness(0);

Here is the code:

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");
Community
  • 1
  • 1
Jordan
  • 9,642
  • 10
  • 71
  • 141

2 Answers2

2

You want to set the LineHeight to 1 for the paragraphs.

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
1

You'll notice that instead of using plFigure, I create a new Figure to put into the paragraph. I fixed that problem. The real problem that I had was that a Figure object has a default Padding. I set this to zero and everything works nicely.

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

var partsList = new Paragraph(plFigure);
Jordan
  • 9,642
  • 10
  • 71
  • 141