5

印刷機能の実装はほぼ完了しましたが、最後のハードルを達成するのに問題があります。

私の問題は、ヘッダー(レポートの対象者に関する情報を含む)、フッター(ページ番号を含む)、および中央のコンテンツ(FlowDocument)で構成されるレポートを印刷していることです。フロードキュメントはかなり長くなる可能性があるため、複数のページにまたがる可能性が非常に高くなります。

私のアプローチは、DocumentPaginatorから派生したカスタムFlowDocumentPaginatorを作成することです。

そこで、ヘッダーとフッターを定義します。

ただし、ページを印刷すると、フロードキュメントとヘッダーおよびフッターが重なり合っています。

したがって、私の質問は単純明快です。ページのフロードキュメント部分をどこからどこに配置するかをどのように定義すればよいでしょうか。

これが私のカスタムメイドのPaginatorからのコードです:

public class HeaderedFlowDocumentPaginator : DocumentPaginator
{
    private DocumentPaginator flowDocumentpaginator;

    public HeaderedFlowDocumentPaginator(FlowDocument document)
    {
        flowDocumentpaginator = ((IDocumentPaginatorSource) document).DocumentPaginator;
    }

    public override bool IsPageCountValid
    {
        get { return flowDocumentpaginator.IsPageCountValid; }
    }

    public override int PageCount
    {
        get { return flowDocumentpaginator.PageCount; }
    }

    public override Size PageSize
    {
        get { return flowDocumentpaginator.PageSize;  }
        set { flowDocumentpaginator.PageSize = value; }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return flowDocumentpaginator.Source; }
    }

    public override DocumentPage GetPage(int pageNumber)
    {
        DocumentPage page = flowDocumentpaginator.GetPage(pageNumber);

        ContainerVisual newVisual = new ContainerVisual();
        newVisual.Children.Add(page.Visual);

        DrawingVisual header = new DrawingVisual();
        using (DrawingContext dc = header.RenderOpen())
        {
            //Header data
        }
        newVisual.Children.Add(header);

        DrawingVisual footer = new DrawingVisual();
        using (DrawingContext dc = footer.RenderOpen())
        {
            Typeface typeface = new Typeface("Trebuchet MS");
            FormattedText text = new FormattedText("Page " + (pageNumber + 1).ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 14, Brushes.Black);

            dc.DrawText(text, new Point(page.Size.Width - 100, page.Size.Height-30));
        }

        newVisual.Children.Add(footer);

        DocumentPage newPage = new DocumentPage(newVisual);
        return newPage;
    }
}

そして、これがprintdialogueの呼び出しです:

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
    try
    {
        PrintDialog printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {
            FlowDocument fd = new FlowDocument();
            MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(<My string of text - RTF formatted>));

            TextRange tr = new TextRange(fd.ContentStart, fd.ContentEnd);
            tr.Load(stream, DataFormats.Rtf);

            stream.Close();
            fd.ColumnWidth = printDialog.PrintableAreaWidth;

            HeaderedFlowDocumentPaginator paginator = new HeaderedFlowDocumentPaginator(fd);

            printDialog.PrintDocument(paginator, "myReport");
        }
    }
    catch (Exception ex)
    {
        //Handle
    }
}
4

1 に答える 1

6

私はそれを自分で見つけました-ページパディングと呼ばれる機能があり、紙の4つの側面からの距離を設定できます:)

かなり簡単な解決策-私は何を探すべきかわからなかった

例:

Flowdocument fd = new FlowDocument();

fd.PagePadding = new Thickness(0.25,160,0.25,45);
于 2010-06-15T13:29:48.333 に答える