First you have to download the pdfSharp dll from sourceforge and add as a reference DLL to your desired project.
Main objective of this solution is to overcome the complexity of building runtime complex reports and developer has a liberty to make her/his own design for the report.One more thing,no need to make the merge modules such as crystal and other reports.
Screen Capturing Class
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SyncClient.Reports
{
public enum CaptureMode
{
Screen, Window
}
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
///
Capture Active Window, Desktop, Window or Control by hWnd or .NET Contro/Form and save it to a specified file. ///
Filename.
///
* If extension is omitted, it's calculated from the type of file ///
* If path is omitted, defaults to %TEMP% ///
* Use %NOW% to put a timestamp in the filename ///
Optional. The default value is CaptureMode.Window.
///
Optional file save mode. Default is PNG
public void CaptureAndSave(string filename, CaptureMode mode, ImageFormat format)
{
mode = CaptureMode.Window;
format = null;
ImageSave(filename, format, Capture(mode));
}
///
Capture a specific window (or control) and save it to a specified file. ///
Filename.
///
* If extension is omitted, it's calculated from the type of file ///
* If path is omitted, defaults to %TEMP% ///
* Use %NOW% to put a timestamp in the filename ///
hWnd (handle) of the window to capture
///
Optional file save mode. Default is PNG
public void CaptureAndSave(string filename, IntPtr handle, ImageFormat format)
{
format = null;
ImageSave(filename, format, Capture(handle));
}
///
Capture a specific window (or control) and save it to a specified file. ///
Filename.
///
* If extension is omitted, it's calculated from the type of file ///
* If path is omitted, defaults to %TEMP% ///
* Use %NOW% to put a timestamp in the filename ///
Object to capture
///
Optional file save mode. Default is PNG
public void CaptureAndSave(string filename, Control c, ImageFormat format)
{
format = null;
ImageSave(filename, format, Capture(c));
}
///
Capture the active window (default) or the desktop and return it as a bitmap ///
Optional. The default value is CaptureMode.Window.
public Bitmap Capture(CaptureMode mode)
{
mode = CaptureMode.Window;
return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
}
///
Capture a .NET Control, Form, UserControl, etc. ///
Object to capture
///
Bitmap of control's area public Bitmap Capture(Control c)
{
return Capture(c.Handle);
}
///
Capture a specific window and return it as a bitmap ///
hWnd (handle) of the window to capture
public Bitmap Capture(IntPtr handle)
{
Rectangle bounds;
Rect rect = new Rect();
GetWindowRect(handle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
Bitmap result = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(result))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return result;
}
///
Position of the cursor relative to the start of the capture public Point CursorPosition;
///
Save an image to a specific file ///
Filename.
///
* If extension is omitted, it's calculated from the type of file ///
* If path is omitted, defaults to %TEMP% ///
* Use %NOW% to put a timestamp in the filename ///
Optional file save mode. Default is PNG
///
Image to save. Usually a BitMap, but can be any
/// Image.
void ImageSave(string filename, ImageFormat format, Image image)
{
format = format ?? ImageFormat.Png;
if (!filename.Contains("."))
filename = filename.Trim() + "." + format.ToString().ToLower();
if (!filename.Contains(@"\"))
filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @"C:\Temp", filename);
filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd@hh.mm.ss"));
image.Save(filename, format);
}
}
}
Reference Form
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;
using System.Drawing.Imaging;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;
namespace SyncClient.Reports
{
public partial class AdvanceReport : Form
{
ScreenCapture capScreen = new ScreenCapture();
public string ISBN { get; set; }
public string Dept { get; set; }
public string Cat { get; set; }
public string Imprint { get; set; }
public string Binding { get; set; }
public string NoofPages { get; set; }
public string RetailPrice { get; set; }
public string CoverImage { get; set; }
public string Title { get; set; }
public string Auth { get; set; }
public string AuthInfo { get; set; }
public AdvanceReport()
{
InitializeComponent();
}
private void AdvanceReport_Load(object sender, EventArgs e)
{
lblisbn.Text = ISBN;
lblDept.Text = Dept;
lblCat.Text = Cat;
lblimprint.Text = Imprint;
lblbinding.Text = Binding;
lblnopage.Text = NoofPages;
lblRetprice.Text = RetailPrice;
lbltitle.Text = Title;
lblauth.Text = " By " + Auth;
txtAuthInfo.Text = AuthInfo;
//txtHide.Focus();
try
{
imgcover.BackgroundImage = Image.FromFile(CoverImage);
}
catch (FileNotFoundException err)
{
}
}
private void MakeReport()
{
capScreen.CaptureAndSave(@"C:\tempImage\test.png", CaptureMode.Window, ImageFormat.Png);
// Create new pdf document and page
PdfDocument doc = new PdfDocument();
PdfPage oPage = new PdfPage();
// Add the page to the pdf document and add the captured image to it
doc.Pages.Add(oPage);
XGraphics xgr = XGraphics.FromPdfPage(oPage);
XImage img = XImage.FromFile(@"C:\tempImage\test.png");
xgr.DrawImage(img, 0, 0);
saveFileDialog.Filter = ("PDF File|*.pdf");
DialogResult btnSave = saveFileDialog.ShowDialog();
if (btnSave.Equals(DialogResult.OK))
{
doc.Save(saveFileDialog.FileName);
doc.Close();
}
// I used the Dispose() function to be able to
// save the same form again, in case some values have changed.
// When I didn't use the function, an GDI+ error occurred.
img.Dispose();
}
private void AdvanceReport_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString().ToUpper() == "P")
{
MakeReport();
}
else if (e.KeyChar.ToString().ToUpper() == "C")
{
this.Close();
}
}
}
}
Sample