报告查看器 - 正常模式与打印预览模式不同

时间:2018-05-16 12:55:12

标签: c# reportviewer

我使用ReportViewer控件为我的报告和用户提供了Reportviewer已经内置的搜索功能,并且还打印了当前页面"微软没有包含的功能所以我自己编写了...问题是在普通模式下所有链接数据都组合在同一页面中,而打印预览页面中则有分页符。这导致正常与打印预览模式下的页码不同,因为切换这些模式时ReportViewer的呈现方式不同。所以当我尝试打印当前页面时,例如" 19"在普通模式下,我将打印页面" 19"来自“打印预览”模式,这与您在屏幕上看到的内容不同。

我能做些什么 - 以普通模式打破与打印预览相同的方式更改.rdlc项目吗?或者获得我在屏幕上看到的内容的流图像并将其打印出来?....任何帮助都很感激,我很难找到解决这个问题的任何事情,但到目前为止还没有成功......

编辑:我在ReportViewer中打印当前页面的类。它可能对某人有用,它适用于打印预览模式但不适用于普通模式如果您的链接数据太大而页面有分页符 - 因为用于打印当前页面的流图像实际上复制了您在.rdlc报告项目中设置的内容 - 就我而言A4纸张尺寸:

class TiskanjeTrenutneStrani : PrintDocument
{
    private PageSettings nastavitve_strani;
    private int stran_za_tiskanje;
    private List<Stream> strani_reporta = new List<Stream>();

    public TiskanjeTrenutneStrani(LocalReport localReport, int stran)
        : this((Report)localReport)
    {
        //Set page for printing
        stran_za_tiskanje = stran-1;
        RenderirajStraniIzpisa(localReport);
    }

    private TiskanjeTrenutneStrani(Report report)
    {
        //Set page as defined in report project
        ReportPageSettings nastavitve_Report_Strani = report.GetDefaultPageSettings();
        var  nastavitve_strani = new PageSettings
        {
            PaperSize = nastavitve_Report_Strani.PaperSize,
            Margins = nastavitve_Report_Strani.Margins
        };
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            foreach (Stream s in strani_reporta)
            {
                s.Dispose();
            }

            strani_reporta.Clear();
        }
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        base.OnPrintPage(e);

        Stream TiskanaStran = strani_reporta[stran_za_tiskanje];
        TiskanaStran.Position = 0;

        // Load each page into a Metafile to draw it.
        using (Metafile pageMetaFile = new Metafile(TiskanaStran))
        {
            Rectangle rc = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);


            e.Graphics.FillRectangle(Brushes.White, rc);

            e.Graphics.DrawImage(pageMetaFile, rc);

        }
    }

    protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
    {
        e.PageSettings = (PageSettings)nastavitve_strani.Clone();
    }

    private void RenderirajStraniIzpisa(LocalReport localReport)
    {
        string deviceInfo = CreateEMFDeviceInfo();

        localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out Warning[] warnings);
    }

    private Stream LocalReportCreateStreamCallback(string name, string extension, Encoding encoding, string mimeType, bool willSeek)
    {
        MemoryStream stream = new MemoryStream();
        strani_reporta.Add(stream);
        return stream;
    }

    private string CreateEMFDeviceInfo()
    {
        PaperSize velikost_papirja = nastavitve_strani.PaperSize;
        Margins margins = nastavitve_strani.Margins;

        // The device info string defines the page range to print as well as the size of the page.
        // A start and end page of 0 means generate all pages.
        return
            string.Format(CultureInfo.InvariantCulture,
            "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
        ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(velikost_papirja.Height),
            ToInches(velikost_papirja.Width));
    }

    private static string ToInches(int hundrethsOfInch)
    {
        double inches = hundrethsOfInch / 100.0;
        return inches.ToString(CultureInfo.InvariantCulture) + "in";
    }

}

用法:

  TiskanjeTrenutneStrani tiskaj_stran = new 
  TiskanjeTrenutneStrani(this.LocalReport, this.CurrentPage);
  tiskaj_stran.Print(); // And It prints current page - unfortunally not what you see on screen If in normal mode

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

StartPage中的EndpageDeviceInfo设置为stran_za_tiskanje。

string.Format(CultureInfo.InvariantCulture,
            "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>{6}</StartPage><EndPage>{6}</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
        ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(velikost_papirja.Height),
            ToInches(velikost_papirja.Width),
            stran_za_tiskanje);

由于您指定了一个页面,因此您应该只能抓取一个项目。

Stream TiskanaStran = strani_reporta[0];