【问题标题】:How to save a screenshot as pdf in C# (Forms)?如何在 C# (Forms) 中将屏幕截图保存为 pdf?
【发布时间】:2019-05-19 00:49:12
【问题描述】:

如何截取我的 Windows 窗体程序的屏幕截图并保存为 pdf ???

我试过这个代码:输出是一个白页

            // generate a file name as the current date/time in unix timestamp format
            string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();

            // the directory to store the output.
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            PrintDocument doc = new PrintDocument()
            {
                PrinterSettings = new PrinterSettings()
                {
                    // set the printer to 'Microsoft Print to PDF'
                    PrinterName = "Microsoft Print to PDF",

                    // tell the object this document will print to file
                    PrintToFile = true,

                    // set the filename to whatever you like (full path)
                    PrintFileName = Path.Combine(directory, file + ".pdf"),
                }
            };
            doc.PrintPage+= new PrintPageEventHandler(PrintImage);
            CaptureScreen();
            doc.Print();

【问题讨论】:

  • 因为您实际上并没有保存任何东西?您需要将屏幕截图保存到文件(.jpg、gif 等),然后打印该文件

标签: c# .net forms pdf printing


【解决方案1】:

我解决了这个问题。 (使用 iTextSharp) 这是我的代码:

            Graphics g = this.CreateGraphics();
            bmp = new Bitmap(this.Size.Width, this.Size.Height + 10, g);
            Graphics mg = Graphics.FromImage(bmp);
            mg.CopyFromScreen(this.Location.X + 20, this.Location.Y + 25 ,40, 47, this.Size);
            // Save the screenshot to the specified path that the user has chosen.
            bmp.Save("Screenshot.png", ImageFormat.Png);


            Document document = new Document();
            document.SetMargins(1,1,1,1);


            using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                PdfWriter.GetInstance(document, stream);
                document.Open();
                using (var imageStream = new FileStream("Screenshot.png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var image = iTextSharp.text.Image.GetInstance(imageStream);                       
                    float maxWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
                    float maxHeight = document.PageSize.Height - document.TopMargin - document.BottomMargin;
                    if (image.Height > maxHeight || image.Width > maxWidth)
                        image.ScaleToFit(maxWidth, maxHeight);
                    document.Add(image);
                }
                document.Close();
            }

【讨论】:

  • 如果我们有多页表格,你能指导我吗,它只适用于一页
  • 此代码无法编译。 PdfWriter 没有 GetInstance 的定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
相关资源
最近更新 更多