C# 有没有办法将窗口保存为PDF格式?

C# 有没有办法将窗口保存为PDF格式?,c#,wpf,windows,pdf,resultset,C#,Wpf,Windows,Pdf,Resultset,我正在使用C#生成一个包含大量结果的窗口(滚动条):Window ResultsWindow=new Window() 在底部,有两个按钮,即取消和打印。第一个做它应该做的。尽管如此,打印按钮应该以某种方式将窗口转换为PDF文件,或者在两者之间进行一步,用户可以在之后保存它 private void Print_click(object sender, RoutedEventArgs e) { //add code to print the whole wi

我正在使用C#生成一个包含大量结果的窗口(滚动条):Window ResultsWindow=new Window()

在底部,有两个按钮,即取消和打印。第一个做它应该做的。尽管如此,打印按钮应该以某种方式将窗口转换为PDF文件,或者在两者之间进行一步,用户可以在之后保存它

    private void Print_click(object sender, RoutedEventArgs e)
    {   
        //add code to print the whole window??
        ResultsWindow.Close();            
    }
你们有谁知道这是怎么回事吗


致以最诚挚的问候

鉴于您正在使用WPF,以下是我如何完成类似任务的:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var wasMax = this.WindowState == WindowState.Maximized;
        UBlattWindow.WindowState = WindowState.Normal;
        var initHeight = UBlattWindow.ActualHeight;
        var initWidth = UBlattWindow.ActualWidth;
        UBlattWindow.Width = 955;
        UBlattWindow.Height = UBlattWindow.Height + (ScrollerContent.ActualHeight - Scroller.ActualHeight) + 20;

        Print(printGrid);
        UBlattWindow.Height = initHeight;
        UBlattWindow.Width = initWidth;
        if (wasMax)
        {
            UBlattWindow.WindowState = WindowState.Maximized;
        }
    }

    private void Print(Visual v)
    {

        System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement;
        if (e == null)
            return;

        PrintDialog pd = new PrintDialog();
        if (pd.ShowDialog() == true)
        {
            PageMediaSize pageSize = null;

            pageSize = new PageMediaSize(PageMediaSizeName.ISOA4);

            pd.PrintTicket.PageMediaSize = pageSize;

            //store original scale
            Transform originalScale = e.LayoutTransform;
            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);

            //get scale of the print wrt to screen of WPF visual
            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                           e.ActualHeight);

            //Transform the Visual to scale
            e.LayoutTransform = new ScaleTransform(scale, scale);

            //get the size of the printer page
            System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

            //update the layout of the visual to the printer page size.
            e.Measure(sz);
            e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

            //now print the visual to printer to fit on the one page.
            pd.PrintVisual(v, "My Print");

            //apply the original transform.
            e.LayoutTransform = originalScale;
        }
    }

请注意,我使用打印方法缩放窗口,因此它将适合ISOA4格式。在打印之前,我还将窗口设置为固定的宽度和高度,然后将其重置。

如果您正在使用WPF,下面是我如何完成类似操作的:

    private void Print_click(object sender, RoutedEventArgs e)
    {   
        //add code to print the whole window??
        ResultsWindow.Close();            
    }
private void Button_Click(object sender, RoutedEventArgs e)
    {
        var wasMax = this.WindowState == WindowState.Maximized;
        UBlattWindow.WindowState = WindowState.Normal;
        var initHeight = UBlattWindow.ActualHeight;
        var initWidth = UBlattWindow.ActualWidth;
        UBlattWindow.Width = 955;
        UBlattWindow.Height = UBlattWindow.Height + (ScrollerContent.ActualHeight - Scroller.ActualHeight) + 20;

        Print(printGrid);
        UBlattWindow.Height = initHeight;
        UBlattWindow.Width = initWidth;
        if (wasMax)
        {
            UBlattWindow.WindowState = WindowState.Maximized;
        }
    }

    private void Print(Visual v)
    {

        System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement;
        if (e == null)
            return;

        PrintDialog pd = new PrintDialog();
        if (pd.ShowDialog() == true)
        {
            PageMediaSize pageSize = null;

            pageSize = new PageMediaSize(PageMediaSizeName.ISOA4);

            pd.PrintTicket.PageMediaSize = pageSize;

            //store original scale
            Transform originalScale = e.LayoutTransform;
            //get selected printer capabilities
            System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);

            //get scale of the print wrt to screen of WPF visual
            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                           e.ActualHeight);

            //Transform the Visual to scale
            e.LayoutTransform = new ScaleTransform(scale, scale);

            //get the size of the printer page
            System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

            //update the layout of the visual to the printer page size.
            e.Measure(sz);
            e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

            //now print the visual to printer to fit on the one page.
            pd.PrintVisual(v, "My Print");

            //apply the original transform.
            e.LayoutTransform = originalScale;
        }
    }

请注意,我使用打印方法缩放窗口,因此它将适合ISOA4格式。在打印之前,我还将窗口设置为固定的宽度和高度,然后将其重置。

这不是特别漂亮(或经过测试),但使用了来自此窗口的信息

    private void Print_click(object sender, RoutedEventArgs e)
    {   
        //add code to print the whole window??
        ResultsWindow.Close();            
    }
这将创建窗口的XPS文件,并将其转换为PDF

using System.IO;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;

namespace WpfApp8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            /*
             *  Convert WPF -> XPS -> PDF
             */
            MemoryStream lMemoryStream = new MemoryStream();
            Package package = Package.Open(lMemoryStream, FileMode.Create);
            XpsDocument doc = new XpsDocument(package);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
            
            // This is your window
            writer.Write(this);

            doc.Close();
            package.Close();
            
            // Convert 
            MemoryStream outStream = new MemoryStream();
            PdfSharp.Xps.XpsConverter.Convert(lMemoryStream, outStream, false);

            // Write pdf file
            FileStream fileStream = new FileStream("C:\\test.pdf", FileMode.Create);
            outStream.CopyTo(fileStream);

            // Clean up
            outStream.Flush();
            outStream.Close();
            fileStream.Flush();
            fileStream.Close();
        }
    }
}
使用System.IO;
使用System.IO.Packaging;
使用System.Windows;
使用System.Windows.Xps;
使用System.Windows.Xps.Packaging;
命名空间WpfApp8
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
private void按钮base_OnClick(对象发送方,RoutedEventTarget e)
{
/*
*转换WPF->XPS->PDF
*/
MemoryStream lMemoryStream=新的MemoryStream();
Package Package=Package.Open(lMemoryStream,FileMode.Create);
XpsDocument doc=新XpsDocument(包);
XpsDocumentWriter=XpsDocument.CreateXpsDocumentWriter(doc);
//这是你的窗户
写(这个);
doc.Close();
package.Close();
//皈依
MemoryStream outStream=新的MemoryStream();
PdfSharp.Xps.XpsConverter.Convert(lMemoryStream、outtream、false);
//编写pdf文件
FileStream FileStream=newfilestream(“C:\\test.pdf”,FileMode.Create);
CopyTo(文件流);
//清理
冲水;
exptream.Close();
Flush();
fileStream.Close();
}
}
}
它使用
PdfSharp
nuget包和
kenjuino.PdfSharp.Xps
包向PdfSharp添加Xps支持


这不是特别漂亮(或经过测试),但使用了来自此的信息

这将创建窗口的XPS文件,并将其转换为PDF

using System.IO;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;

namespace WpfApp8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            /*
             *  Convert WPF -> XPS -> PDF
             */
            MemoryStream lMemoryStream = new MemoryStream();
            Package package = Package.Open(lMemoryStream, FileMode.Create);
            XpsDocument doc = new XpsDocument(package);
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
            
            // This is your window
            writer.Write(this);

            doc.Close();
            package.Close();
            
            // Convert 
            MemoryStream outStream = new MemoryStream();
            PdfSharp.Xps.XpsConverter.Convert(lMemoryStream, outStream, false);

            // Write pdf file
            FileStream fileStream = new FileStream("C:\\test.pdf", FileMode.Create);
            outStream.CopyTo(fileStream);

            // Clean up
            outStream.Flush();
            outStream.Close();
            fileStream.Flush();
            fileStream.Close();
        }
    }
}
使用System.IO;
使用System.IO.Packaging;
使用System.Windows;
使用System.Windows.Xps;
使用System.Windows.Xps.Packaging;
命名空间WpfApp8
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
private void按钮base_OnClick(对象发送方,RoutedEventTarget e)
{
/*
*转换WPF->XPS->PDF
*/
MemoryStream lMemoryStream=新的MemoryStream();
Package Package=Package.Open(lMemoryStream,FileMode.Create);
XpsDocument doc=新XpsDocument(包);
XpsDocumentWriter=XpsDocument.CreateXpsDocumentWriter(doc);
//这是你的窗户
写(这个);
doc.Close();
package.Close();
//皈依
MemoryStream outStream=新的MemoryStream();
PdfSharp.Xps.XpsConverter.Convert(lMemoryStream、outtream、false);
//编写pdf文件
FileStream FileStream=newfilestream(“C:\\test.pdf”,FileMode.Create);
CopyTo(文件流);
//清理
冲水;
exptream.Close();
Flush();
fileStream.Close();
}
}
}
它使用
PdfSharp
nuget包和
kenjuino.PdfSharp.Xps
包向PdfSharp添加Xps支持


此窗口中到底有什么?也许您真的想保存正在显示的数据,而不是窗口本身?NET中有一些库可以生成PDF文件并允许您将数据插入其中。然而,如果你真的想要一个截图,那是另一回事(尽管你提到了滚动条,所以截图可能不是很有用?)嗨,是的,其中的数据有各自的颜色和格式。您指的是哪些库?有几个.net库作为nuget包提供,可用于生成PDF。有些是免费的,有些不是。你需要在网上做一些简单的搜索,看看是否能找到适合你情况的东西。如果您使用了颜色和格式,这是UI的一部分,而不是数据的一部分。因此,要在不截屏的情况下生成等效的PDF,您可能需要类似的逻辑来格式化PDF文档中与屏幕输出等效的数据。另外,你没说这是WinForms,还是WPF,或者其他什么?好的,对不起,我从哪里得到这个?我假设它是WinForms,在标题中有:使用System.Windows;使用System.Windows.Controls;使用System.Windows.Media;我也可以将输出保存为字符串或其他内容,并将其保存在格式中;或者截图的窗口。是否可以截图一个窗口,即它的顶部,然后使用滚动条和屏幕截图底部?这个窗口中到底是什么?也许您真的想保存正在显示的数据,