【问题标题】:How to auto pagebreak the PDFsharp document?如何自动分页 PDFsharp 文档?
【发布时间】:2018-09-11 04:09:38
【问题描述】:

我正在尝试从 C# WPF 控件生成 PDF 文档。 这是我的

结果出来一半我的对象,如何自动分页?
以下是我的代码:

System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedDoc = new FixedDocument()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedDoc1 = new FixedDocument()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => pageContent = new PageContent()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => pageContent1 = new PageContent()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedPage = new FixedPage()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedPage1 = new FixedPage()));

        var visual = Child as UIElement;
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => (Content).Children.Remove(visual)));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedPage.Children.Clear()));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedPage.Children.Add(visual)));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage)));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => fixedDoc.Pages.Add(pageContent)));

        // write to PDF file
        string tempFilename = "temp.Xps";
        File.Delete(tempFilename);
        XpsDocument xpsd = new XpsDocument(tempFilename, FileAccess.ReadWrite);
        XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => xw.Write(fixedDoc)));
        System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => xpsd.Close()));
        filename = filepath + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + title_ + ".pdf";
        PdfSharp.Xps.XpsConverter.Convert(tempFilename, filename, 1);

        var visual2 = Child2 as UIElement;
        Application.Current.Dispatcher.Invoke(new Action(() => (Content).Children.Remove(visual2)));
        Application.Current.Dispatcher.Invoke(new Action(() => fixedPage1.Children.Clear()));
        Application.Current.Dispatcher.Invoke(new Action(() => fixedPage1.Children.Add(visual2)));
        Application.Current.Dispatcher.Invoke(new Action(() => ((System.Windows.Markup.IAddChild)pageContent1).AddChild(fixedPage1)));
        Application.Current.Dispatcher.Invoke(new Action(() => fixedDoc1.Pages.Add(pageContent1)));

        // write to PDF file
        string tempFilename2 = "temp2.Xps";
        File.Delete(tempFilename2);
        XpsDocument xpsd2 = new XpsDocument(tempFilename2, FileAccess.ReadWrite);
        XpsDocumentWriter xw2 = XpsDocument.CreateXpsDocumentWriter(xpsd2);
        Application.Current.Dispatcher.Invoke(new Action(() => xw2.Write(fixedDoc1)));
        Application.Current.Dispatcher.Invoke(new Action(() => xpsd2.Close()));
        filename2 = filepath + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + title_ + ".pdf";
        PdfSharp.Xps.XpsConverter.Convert(tempFilename2, filename2, 1);

【问题讨论】:

    标签: c# wpf pdfsharp


    【解决方案1】:

    PDFsharp 不自动分页。

    我建议按照其他人的图表处理方式:只显示大的(10、20、30 左右),而将真正小的图表总结为“其他”。这样,图例将适合一页,并且您可以避免图表中出现“零宽度”饼图。

    这并不是您问题的真正答案,但它是一种也符合良好做法的解决方法。

    【讨论】:

    • 感谢您的回答,但出于某种原因,我需要显示所有数据。
    • 我不知道如何使用 PDFsharp XPS 转换器,但是否可以选择增加 PDF 页面的大小,以便整个图表适合单个页面,显示所有项目?在第二步中,如果标准页面(如果需要),则可以在系列中显示此超大页面
    • 如果我增加页面大小,那么我就无法以 A4 大小打印它,这就是我需要自动分页符或一些类似功能的原因。你是什​​么意思显示系列页面?
    • 如果您增加页面大小,则可以在单独的循环中进行分页 - 或使用 Adob​​e Reader 在多个 A4 页面上打印大页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多