2012-10-04 15 views
6

मैं विजुअल स्टूडियो 2010 सी # विंडोज फॉर्म एप्लिकेशन + माईएसक्ल का उपयोग कर रहा हूं मेरे पास एक रिपोर्ट व्यूअर है जो 100% काम कर रहा है। रिपोर्टव्यूवर मेरे डेटाबेस के डेटा से भरा हुआ है, यह दिखाता है कि मैं प्रिंट करने के लिए बटन पर क्लिक करता हूं और प्रिंट करता है ... लेकिन, मेरा क्लाइंट इस बटन पर क्लिक नहीं करना चाहता, वह स्वचालित रूप से प्रिंट करना चाहता है। जब मैं ReportViewer को कॉल करता हूं तो इसे करने के लिए बटन पर क्लिक करने की ज़रूरत के बिना इसे स्वयं प्रिंट करें। क्या कोई मुझे बता सकता है कि मैं यह कैसे करता हूं?पूर्वावलोकन के बिना एक रिपोर्ट व्यूअर प्रिंट करें

मैंने टूलबॉक्स से reportviewer1.print और PrintDocument को आजमाया। लेकिन मुझे नहीं पता कि इन्हें सही तरीके से कैसे उपयोग किया जाए।

धन्यवाद ध्यान दें!

+0

क्या यह क्रिस्टलरपोर्ट्स का उपयोग कर रहा है? यह एकमात्र रिपोर्ट व्यूअर है जो मुझे पता है, लेकिन इसका मतलब यह नहीं है कि यह केवल एकमात्र है। – Bobson

+0

नहीं, इसकी रिपोर्ट व्यूअर (टूलबॉक्स से बस 'रिपोर्ट') क्या आप एक शॉट दे सकते हैं और मुझे बता सकते हैं कि यह कैसे करें? शायद यह इतना अलग नहीं है =) –

उत्तर

0

इस प्रकार हम क्रिस्टल रिपोर्ट के साथ ऐसा करते हैं।

  ReportDocument rd = new ReportDocument(); 
      // Insert code to run the report here 

      // This gets the user's default printer to print to. 
      PrintDialog prt = new PrintDialog(); 
      rd.PrintOptions.PrinterName = prt.PrinterSettings.PrinterName; 
      // This does the printing. 
      rd.PrintToPrinter(copies, true, 1, 1000); 

मुझे लगता है कि आप के लिए PrintOptions.PrinterName के बराबर ReportViewer.PrinterSettings होगा, लेकिन मुझे लगता है कि क्या तुम सच में की जरूरत है PrintToPrinter() के बराबर है, जो मैं अपने संक्षिप्त नज़र में नहीं दिख रहा है।

+0

वास्तव में मुझे जवाब देने के लिए धन्यवाद बॉबसन =) मैं इसे अभी आज़माउंगा, मैं प्रिंटडिअलॉग को केवल प्रिंट नहीं चाहता हूं, मैं अब कोशिश करूँगा और जितनी जल्दी हो सके प्रतिक्रिया दे सकता हूं। मेरे पास "ReportDocument" नहीं है; केवल रिपोर्टडेटा स्रोत है जिसमें 'प्रिंट' के बारे में कोई विकल्प नहीं है; –

+0

@alannaidon - आपके लिए, यह 'ReportDocument' के बजाय' ReportViewer' होगा, लेकिन विचार वही होगा । अगर यह काम नहीं करता है, तो मेरा दूसरा जवाब आज़माएं। मुझे संदेह है कि यह आपके मुद्दे के लिए अधिक सटीक है। – Bobson

1

यदि मेरा क्रिस्टल रिपोर्ट उत्तर आपके लिए काम नहीं करता है, तो आप this page भी आजमा सकते हैं। दोबारा, मैंने इसका परीक्षण नहीं किया है, और यह सुनिश्चित नहीं हो सकता कि यह काम करता है, लेकिन यह एक पूरी तरह से अलग दृष्टिकोण की तरह दिखता है जो काम कर सकता है। यदि नहीं, तो दुर्भाग्यवश, मैं कोई मदद नहीं कर रहा हूं।

+0

यह ठीक है बॉबसन। मैंने SendKey के साथ कुछ कोशिश की, बटन "प्रिंट" पर दबाकर रिपोर्टव्यूअर के साथ स्वचालित रूप से बनाया गया है। क्या आप इसके बारे में कुछ जानते हैं? –

+0

@alannaidon - मुझे नहीं लगता कि मैंने कभी भी SendKey का उपयोग किया है। हालांकि, यह एक अच्छा सरल समाधान की तरह लगता है। इसे एक नए उत्तर के रूप में जोड़ने के लिए स्वतंत्र महसूस करें और इसे स्वीकार करते हुए इसे स्वीकार करें। – Bobson

+0

यहां प्रदान किया गया लिंक बहुत अच्छी तरह से काम करता है और पूरी तरह से कक्षा में लगाया जाता है, इसलिए आपको इस कोड में केवल एक कॉल को अपने कोड में जोड़ने की ज़रूरत नहीं है। – MrWuf

7

मेरे पास एक ही समस्या थी जो मैं उपयोग करता हूं और एक आकर्षण की तरह काम करता है!

using System; 
using System.IO; 
using System.Text; 
using System.Globalization; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Printing; 
using Microsoft.Reporting.WinForms; 
using System.Collections.Generic; 
using System.Collections.Specialized; 
using System.Diagnostics; 
using System.ComponentModel; 
using System.Data; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 


namespace NewLabelPrinter 
{ 
/// <summary> 
/// The ReportPrintDocument will print all of the pages of a ServerReport or LocalReport. 
/// The pages are rendered when the print document is constructed. Once constructed, 
/// call Print() on this class to begin printing. 
/// </summary> 
class AutoPrintCls : PrintDocument 
{ 
    private PageSettings m_pageSettings; 
    private int m_currentPage; 
    private List<Stream> m_pages = new List<Stream>(); 

    public AutoPrintCls(ServerReport serverReport) 
     : this((Report)serverReport) 
    { 
     RenderAllServerReportPages(serverReport); 
    } 

    public AutoPrintCls(LocalReport localReport) 
     : this((Report)localReport) 
    { 
     RenderAllLocalReportPages(localReport); 
    } 

    private AutoPrintCls(Report report) 
    { 
     // Set the page settings to the default defined in the report 
     ReportPageSettings reportPageSettings = report.GetDefaultPageSettings(); 

     // The page settings object will use the default printer unless 
     // PageSettings.PrinterSettings is changed. This assumes there 
     // is a default printer. 
     m_pageSettings = new PageSettings(); 
     m_pageSettings.PaperSize = reportPageSettings.PaperSize; 
     m_pageSettings.Margins = reportPageSettings.Margins; 
    } 

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

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

      m_pages.Clear(); 
     } 
    } 

    protected override void OnBeginPrint(PrintEventArgs e) 
    { 
     base.OnBeginPrint(e); 

     m_currentPage = 0; 
    } 

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

     Stream pageToPrint = m_pages[m_currentPage]; 
     pageToPrint.Position = 0; 

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

      // Draw a white background for the report 
      e.Graphics.FillRectangle(Brushes.White, adjustedRect); 

      // Draw the report content 
      e.Graphics.DrawImage(pageMetaFile, adjustedRect); 

      // Prepare for next page. Make sure we haven't hit the end. 
      m_currentPage++; 
      e.HasMorePages = m_currentPage < m_pages.Count; 
     } 
    } 

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

    private void RenderAllServerReportPages(ServerReport serverReport) 
    { 
     try 
     { 
      string deviceInfo = CreateEMFDeviceInfo(); 

      // Generating Image renderer pages one at a time can be expensive. In order 
      // to generate page 2, the server would need to recalculate page 1 and throw it 
      // away. Using PersistStreams causes the server to generate all the pages in 
      // the background but return as soon as page 1 is complete. 
      NameValueCollection firstPageParameters = new NameValueCollection(); 
      firstPageParameters.Add("rs:PersistStreams", "True"); 

      // GetNextStream returns the next page in the sequence from the background process 
      // started by PersistStreams. 
      NameValueCollection nonFirstPageParameters = new NameValueCollection(); 
      nonFirstPageParameters.Add("rs:GetNextStream", "True"); 

      string mimeType; 
      string fileExtension; 


      Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension); 



      // The server returns an empty stream when moving beyond the last page. 
      while (pageStream.Length > 0) 
      { 
       m_pages.Add(pageStream); 

       pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension); 
      } 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("possible missing information :: " + e); 
     } 
    } 

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

      Warning[] warnings; 

      localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show("error :: " + e); 
     } 
    } 

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

     return stream; 
    } 

    private string CreateEMFDeviceInfo() 
    { 
     PaperSize paperSize = m_pageSettings.PaperSize; 
     Margins margins = m_pageSettings.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(paperSize.Height), 
      ToInches(paperSize.Width)); 
    } 

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

}

इस वर्ग क्या आप तो जरूरत है तुम सब करने की जरूरत है के लिए एकदम सही की स्थापना की है:

private void AutoPrint() 
    { 
     AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport); 
     autoprintme.Print(); 
    } 

और हे की सफ़ाई यह प्रिंट। बस इसे अपने कोड में एक विधि से संलग्न करें (शायद रिपोर्ट लोड के बाद।) और आपका सेटअप अच्छी तरह से!

विकल्प: (परीक्षण नहीं)

के रूप में देखा यह डिफ़ॉल्ट प्रिंटर के लिए बाहर प्रिंट, प्रिंटर को बदलने के लिए आप निम्न कर सकता है:

if (printDialog.ShowDialog() == DialogResult.OK) 
{ 
    m_pageSettings .PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName; 
} 

हालांकि मैं अब किसी भी राशि के रूप में परीक्षण नहीं इस कोड का परीक्षण करने के लिए स्रोत कोड

+0

हाय, प्रिंटडियलॉग से अपने कोड में प्रिंटर चुनना संभव है? – Kate

+0

हाँ मुझे लगता है कि प्रिंटडिअलॉग का उपयोग करने से आपकी ज़रूरत होगी, http://msdn.microsoft.com/en-us/library/cfkddyc2(v=vs.110).aspx – lemunk

+0

हां, लेकिन मुझे नहीं पता कि मैं कैसे कर सकता था अपने कोड में printdialog दें ... मैं m_pageSettings.PrinterSettings.PrinterName = PrintDialog1.PrinterSettings.PrinterName; ... लेकिन अभी भी डिफ़ॉल्ट प्रिंटर ... – Kate

संबंधित मुद्दे