Tuesday, August 24, 2010

Silent Printing PDF File in C# and closing Adobe Popup

Printing is very common task in Desktop App. In this article i will demonstrate how to print a PDF File Silently. There are two approaches. First We can print the file using PDF Sharp ( a open source library to support PDF Related Operations) and Arobate exe. Second Approach is to intiate a process that call Acrobate now doubt both need at least acrobat installed on machine, as PDF Sharp can not render PDF It self. I will demonstrate the first way.
Add Reference to PDF Sharp. First inform PdfFilePrinter about path of Adobe reader exe on target machine.
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";

///Then in next step create a instance of the class PdfFilePrinter and provide path of file to print and your default active printer.

try
{
PdfFilePrinter Pdfprinter = new PdfFilePrinter(filetoprint, printer);
Pdfprinter.Print();
}
catch(Exception ex)
{
throw ex;
}
Now get the Adobe Process and kill it if it has finished its job.

foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.StartsWith("Acro"))
{
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
proc.Kill();
break;
}
else
{
proc.Kill();
break;
}
}
}


if you have any question please free to ask. Hope this helps.


Monday, August 16, 2010

How to Draw a shape in Silverlight 3

In Silverlight, some time we need to draw shapes. and to draw these shapes we need mouse coordinates while mouse button is down, Up and most important while mouse is moving to give illusion that shape is creating. to do this first Add a static shape (circle, rectangle etc) on canvas and set its visibility to Hidden. after this follow the following code.

Create four Global Variables to store coordinates.

double x1 = 0.0;
double y1 = 0.0;
double x2 = 0.0;
double y2 = 0.0;

Now in Canvas_ MouseLeftButtonDown Event of Mouse get the start points like this

x1 = e.GetPosition(canvas).X;
y1 = e.GetPosition(canvas).Y;
_down = true;

In Canvas_MouseMove event write this code

if ((_down)
{
double xo = e.GetPosition(canvas).X;
double yo = e.GetPosition(canvas).Y;
///// Now set position of that shape (circle, rectangle etc).

Shape.SetValue(Canvas.LeftProperty, Math.Min(xo, x1));
Shape.SetValue(Canvas.TopProperty, Math.Min(yo, y1));

//////Set Shape Size
Shape.Width = Math.Abs(xo - x1);
Shape.Height = Math.Abs(yo - y1);

if (Shape.Visibility != Visibility.Visible)
Shape.Visibility = Visibility.Visible;
}

and Finally in Canvas_MouseLeftButtonUp Event write this code
_down = false;
x2 = e.GetPosition(canvas).X;
y2 = e.GetPosition(canvas).Y;
DrawShape(x1, y1, x2, y2);

I'll explain and provide the Draw function code in my next article.

Happy Coding :)

Wednesday, August 11, 2010

Sending HTML Tempate based Email in Asp.Net

Few months ago one of our client requested a feature. They want to send HTML Template based email to their clients and staff containing some Dynamic data. I come up with following solution. I downloaded a open source parser vici.core. This is great tool and has lot of features HTML Template parser is just one on them.
First you should have HTML Template for the email. like
<blockquote>

<p>"

<strong>Creation Date:</strong>

$CreateDate

<strong>Order No:</strong>

$OrderNo

"</p>

</blockquote>
put a $ sign before your variables (that u want to provide on run time) in your html template. Then in your application add reference to vici.core. and use following code.
TemplateParser template = new TemplateParser();
IParserContext pContext = new CSharpContext();

Now in first value add name of your variable in html template. without $ sign

Dictionary emailData = new Dictionary();
emailData.Add("CreateDate", dpCDate.Text);
emailData.Add("OrderNo", txtOrderNo.Text);
emailData.Add("CDate", txtCompDate.Text);

foreach (KeyValuePair pair in eData)
{
pContext.Set(pair.Key, pair.Value);
}
string tempStr = File.ReadAllText("EmailTemplate.html");

MailMessage msg = new MailMessage();
msg.Subject = "Order Confirmation ";
msg.From = new MailAddress("from@yourdomain.com");
msg.Body = template.Render(tempStr, pContext);
msg.IsBodyHtml = true;

for (int i = 0; i < toAddrArray.Length; i++)
{
msg.To.Add(toAddrArray[i]);
}

///////////Define your smtp setting in web.config
var mailclient = new SmtpClient();
mailclient.Send(msg);


Hope this helps...


Tuesday, August 10, 2010

Converting WPF Control to Image

In one of my application I need to convert my WPF Form to Image. I Googled around and find RenderTargetBitmap is solution to my Problem. The Code is given below.

public static PngBitmapEncoder ImageFromControl(Control myControl)
{
Transform transform =
myControl.LayoutTransform;

System.Windows.Size sizeOfControl = new System.Windows.Size(
myControl.ActualWidth, controlToConvert.ActualHeight);
myControl.Measure(sizeOfControl);
myControl.Arrange(new Rect(sizeOfControl));

RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(
myControl);

PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

return pngEncoder;
}
and in my Main function is use it like,,,
stream = File.Create("Control.jpg");
ImageFromControl(myControl).Save(stream);
stream.Close();
I hope this will Help..... Happy Coding :)