0

I am trying to print my active form to PDF. I have successfully printed it but I can't change it to fit/stretched the whole page of the PDF document.

enter image description here

Below is the code that I've used.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

// additional namespaces
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Drawing.Printing;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Button printButton = new Button();
        private PrintDocument printDocuments1 = new PrintDocument();

        public Form1()
        {
            InitializeComponent();
            printButton.Click += new EventHandler(button12_Click);
            printDocuments1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        }


         Bitmap memoryImages;

         private void CaptureScreens()
         {
             Graphics myGraphics = this.CreateGraphics();
             Size s = this.Size;
             memoryImages = new Bitmap(s.Width, s.Height, myGraphics);
             Graphics memoryGraphics = Graphics.FromImage(memoryImages);
             memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
         }

         private void printDocument1_PrintPage(System.Object sender,
                    System.Drawing.Printing.PrintPageEventArgs e)
         {
             e.Graphics.DrawImage(memoryImages, 0, 0);
         }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            CaptureScreens();
            printDocuments1.Print();
        }
    }
}

Can anyone help me on how to do the resize of image to fit the whole PDF document? Thank you very much for the help.

thompogi
  • 39
  • 9
  • Possible duplicate of [Converting windows form in C# to PDF using PdfSharp](https://stackoverflow.com/questions/10618036/converting-windows-form-in-c-sharp-to-pdf-using-pdfsharp) – Jeric Cruz Sep 28 '17 at 05:55
  • Hi Jeric, I don't want to use PdfSharp if it is possible. – thompogi Sep 28 '17 at 06:07
  • Either setting the size of the Image you want to Print to full page or setting the size of the page to the image size should help, `https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.originatmargins(v=vs.110).aspx` looks promising – efkah Sep 28 '17 at 08:19
  • Graphics.DrawImage() has several overloads, they allow you to rescale the image. Consider using them. Or use ScaleTransform(). And do consider that blowing up a screenshot does make the printout pretty grainy, text in particular is not going to look very good unless you have long arms. – Hans Passant Sep 28 '17 at 08:59

0 Answers0