Tuesday, November 04, 2008

Regular Expressions

ReguLazy

... is a tool which helps you quickly put together regular expressions.

Get it from: http://tools.osherove.com/

Download it. Run it.

I have version 1.0.3.0.

In Text Edit mode, enter a value such as 0.25

Go into Regex Edit mode, the background will turn blue.

Do not highlight the text, just right-click it.

Select one of the options (e.g. Decimal number).

The resulting expression is ^-?\d+.\d+$

Click the Generate button, you get .Net code you can use.

I used this regular expression as follows in my form validation:

if (!System.Text.RegularExpressions.Regex.IsMatch(PassMark.Text.Trim(), @"^\d+\.\d+$"))
{
error = "Pass Mark must be a number";
}

The Regulator

... is from the same developer, covers the same ground but is a more full-fledged product. It's also free, which is really amazing.

How to use it - this is explained in the help - quick start.

The only thing that got me was I started typing my input in the box intended for the external file browse bottom-left. Type the input in the white area below.

Friday, September 12, 2008

Random string generation in C#

I needed test data for one of my systems, so I put together a console app which generates random strings.


private static System.Random random = new System.Random((int)(System.DateTime.Now.Ticks % System.Int32.MaxValue));

static void Main(string[] args)
{
for (int i=0; i<10; i++)
{
Console.WriteLine(GetRandomString(5));
}
Console.ReadLine();
}

private static string GetRandomString(int length)
{
char[] returnValue = new char[length];
returnValue[0] = GetRandomUpperCaseCharacter();
for (int i = 1; i < length; i++)
{
returnValue[i] = GetRandomLowerCaseCharacter();
}
return new String(returnValue);
}

private static char GetRandomLowerCaseCharacter()
{
return ((char)((short)'a' + random.Next(26)));
}

private static char GetRandomUpperCaseCharacter()
{
return ((char)((short)'A' + random.Next(26)));
}


The key to getting random data is to initialize a Random object once with a value from the system clock (DateTime.Now.Ticks), and then to use the Next method each time you need a new random value.

If you create a new Random object each time you actually need a random number, chances are the clock will not have moved on, so you'll get repeated values until it does.

Thanks to:
http://www.developerfusion.co.uk/show/3940/


Thursday, August 28, 2008

Server-side PDF form generation with OpenOffice Writer and iTextSharp

OpenOffice.org Writer has the ability to create PDF forms:

Create a form in Writer with textboxes.















Be sure to give each of your fields unique names (LastName, etc) - these will be needed later when we reference the fields in the PDF from iTextSharp.

When the form is ready, choose File | Export as PDF to create the PDF file.

iTextSharp is an open-source .Net library that allows you to reference the form fields from ASP.Net.

In Visual Studio, add a reference to the iTextSharp dll.


using iTextSharp.text.pdf;

...

string template = HttpContext.Current.Server.MapPath("../PDF/Template.pdf");

PdfReader pdfReader = new PdfReader(template);
MemoryStream pdfResponse = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, pdfResponse);

AcroFields pdfFormFields = pdfStamper.AcroFields;

// in real life, this value comes from a database
string lastName = "Bloggs";

pdfFormFields.SetField("LastName", lastName);

...

pdfStamper.FormFlattening = true;
pdfStamper.Writer.CloseStream = false;
pdfStamper.Close();

// in this case we're streaming the PDF without saving to disk.
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=FileName.pdf");
pdfResponse.WriteTo(HttpContext.Current.Response.OutputStream);

pdfResponse.Close();


In the above example, the PDF is generated on the fly.

The way it works is: I have a page (PrintDocument.aspx) with no html in it; all it has is the iTextSharp code in the code behind.

On my main page, I have a link to PrintDocument.aspx. When the user clicks this link, the PDF is sent to them as a download.