我有一个类ReportViewerView
,它继承了Window
class。我想将此类的内容保存为pdf文件。
我已经可以将该类打开为ShowDialog()
的窗口,一旦打开该窗口,就可以选择保存为正常工作的Acrobat(pdf)文件。但是,我想保存该窗口的内容 ,不用 首先打开一个对话框。我拥有打开此窗口所需的所有数据,我想立即将其保存到预定的文件位置,无需任何用户输入。我怎么能这样做?
public void SaveReport(MyClass report, string title)
{
ReportViewerView reportViewer = new ReportViewerView(report, title);
//reportViewer.ShowDialog();
// is it even possible to make this a byte[] ?
byte[] reportBytes = Convert.FromBase64String(reportViewer.ToString());
System.IO.File.WriteAllBytes("C:\\Temp\\Test_Save.pdf", reportBytes);
//Maybe a FileStream?
using (FileStream fs = new FileStream("C:\\Temp\\Test_Save.pdf", FileMode.Open))
{ // Maybe not a TextReader... what would a 'Window' use?
using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(fs))
{
reportViewer.Serialize(xmlReader); // cannot serialize anyway
}
}
}
类ReportViewerView
是我的遗留代码,与Telerik类ReportViewer
相关。类MyClass
是继承自Telerik.Reporting.Report
的遗留代码。我怀疑Windows
课程是我的答案,但如果它有帮助,ReportViewerView.xaml.cs
:
public partial class ReportViewerView : Window
{
public ReportViewerView(MyClass reportDoc, string title)
{
InitializeComponent();
InstanceReportSource rs = new InstanceReportSource();
rs.ReportDocument = reportDoc;
this.reportViewer1.ReportSource = rs;
this.Title = title;
}
}
课程MyClass
有点笨重,可以完全复制/粘贴,但它应该执行其父Telerik.Reporting.Report
可以做的任何事情。这是简短版本:
partial class MyClass
{
private void InitializeComponent()
{
// lot of stuff
}
private Telerik.Reporting.PageHeaderSection pageHeaderSection1;
private Telerik.Reporting.DetailSection detail;
private Telerik.Reporting.PageFooterSection pageFooterSection1;
private Telerik.Reporting.TextBox textBox1;
private Telerik.Reporting.TextBox TitleTB;
private Telerik.Reporting.TextBox CustomerNameTB;
private Telerik.Reporting.TextBox ReportDateTB;
private string dateString
{
get { return DateTime.Now.ToString("g", CultureInfo.CreateSpecificCulture("en-us")); }
}
}