Friday, May 18, 2007
Finding a Templating Tool for Printing in .NET
We have a pretty basic requirement for printing cards, envelopes, brochures, etc. We want to take requests in for donations and then allow for printing in batches of these orders.
The PrintDocument is the main class in .NET for printing. The sample code shown looks like this:
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Print each line of the file.
While count < linesPerPage line = streamToPrint.ReadLine() If line Is Nothing Then Exit While End If yPos = topMargin + count * printFont.GetHeight(ev.Graphics) ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()) count += 1 End While ' If more lines exist, print another page. If Not (line Is Nothing) Then ev.HasMorePages = True Else ev.HasMorePages = False End If End Sub
However, this requires you to manually print by drawing through the graphics context. This is not a great solution for something like envelopes, cards, etc. because hard coding a bunch of strings is not really a scalable solution.
What we need is a template engine - something to define a template that can be filled with data and then printed, all through .NET code. In addition, I would like the ability to print the same document to an image file so that I can show the user a print preview through the web.
So what should we use for a template engine? The options we came up with were:
1. Word - using the supplied primary interop assemblies, we could load up a word document, fill out some form fields or do a mail merge programmatically. However, this isn't recommended to be done by server based applications because each time you load up an instance of Word to do the processing.
2. Custom template - we could define our template layout. Given the amount of data we have, it wouldn't be that difficult to define a template that contains a basic background, some text fields with X,Y locations, etc. I'm not exactly a fan of this type of solution because it doesn't scale in terms of functionality - what happens when the user wants rich font support, margins, tables, embedded bitmaps, paragraph formatting, etc. Surely, we shouldn't have to re-invent the wheel here.
3. Printing existing controls. See this article on a basic explanation. This would allow you use a windows form as a template and the print it. This is better than custom coding a template engine, but its not exactly a great solution either - who wants to build a windows form to represent an envelope?
After exploring a few other ideas, we came up with a basic idea - use SQL Server 2005 Reporting Services. After thinking about it, it provides us all the pieces we need in a nice package:
1. Template engine based on XML that can be generated by a relatively non-technical user.
2. Automatic data binding to the data source - simply create a report that represents a card, an envelope, etc.
3. Page sizing and pagination support - we would need this for envelopes and odd sized cards.
4. Rendering to image format - we can use this for preview.
5. Programming model through web services and ability to grab rendered output through web services. So for our preview feature, we can simply call the web service and stream out a rendered image. Alternatively, we can simply point the URL that will supply a rendered image of your report.
So we think we can use SQL Server 2005 Reporting Services as a template engine and embed it into our web application to enable server side printing.
One drawback on this approach - I haven't found anything in the web services API that allows me to render the report directly to the printer. I can see the ReportExecutionService.Render method that can give me back a stream in any format, but then I would need to figure out how send that stream to the printer. The simplest format to handle would be an image - simply then drawing on the graphics object and printing it would then be possible. There is a good post on this type of idea here...
The PrintDocument is the main class in .NET for printing. The sample code shown looks like this:
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
' Print each line of the file.
While count < linesPerPage line = streamToPrint.ReadLine() If line Is Nothing Then Exit While End If yPos = topMargin + count * printFont.GetHeight(ev.Graphics) ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()) count += 1 End While ' If more lines exist, print another page. If Not (line Is Nothing) Then ev.HasMorePages = True Else ev.HasMorePages = False End If End Sub
However, this requires you to manually print by drawing through the graphics context. This is not a great solution for something like envelopes, cards, etc. because hard coding a bunch of strings is not really a scalable solution.
What we need is a template engine - something to define a template that can be filled with data and then printed, all through .NET code. In addition, I would like the ability to print the same document to an image file so that I can show the user a print preview through the web.
So what should we use for a template engine? The options we came up with were:
1. Word - using the supplied primary interop assemblies, we could load up a word document, fill out some form fields or do a mail merge programmatically. However, this isn't recommended to be done by server based applications because each time you load up an instance of Word to do the processing.
2. Custom template - we could define our template layout. Given the amount of data we have, it wouldn't be that difficult to define a template that contains a basic background, some text fields with X,Y locations, etc. I'm not exactly a fan of this type of solution because it doesn't scale in terms of functionality - what happens when the user wants rich font support, margins, tables, embedded bitmaps, paragraph formatting, etc. Surely, we shouldn't have to re-invent the wheel here.
3. Printing existing controls. See this article on a basic explanation. This would allow you use a windows form as a template and the print it. This is better than custom coding a template engine, but its not exactly a great solution either - who wants to build a windows form to represent an envelope?
After exploring a few other ideas, we came up with a basic idea - use SQL Server 2005 Reporting Services. After thinking about it, it provides us all the pieces we need in a nice package:
1. Template engine based on XML that can be generated by a relatively non-technical user.
2. Automatic data binding to the data source - simply create a report that represents a card, an envelope, etc.
3. Page sizing and pagination support - we would need this for envelopes and odd sized cards.
4. Rendering to image format - we can use this for preview.
5. Programming model through web services and ability to grab rendered output through web services. So for our preview feature, we can simply call the web service and stream out a rendered image. Alternatively, we can simply point the URL that will supply a rendered image of your report.
So we think we can use SQL Server 2005 Reporting Services as a template engine and embed it into our web application to enable server side printing.
One drawback on this approach - I haven't found anything in the web services API that allows me to render the report directly to the printer. I can see the ReportExecutionService.Render method that can give me back a stream in any format, but then I would need to figure out how send that stream to the printer. The simplest format to handle would be an image - simply then drawing on the graphics object and printing it would then be possible. There is a good post on this type of idea here...
Tuesday, May 15, 2007
8 year olds now have blogs
I thought it was ridiculous when kids got their own cell phones, but now they're starting to have their own blogs as well. See here for details.
At least my traffic numbers are better than the 8 year olds - for now at least!
At least my traffic numbers are better than the 8 year olds - for now at least!
New Programming Language for Kids
I started programming when I was 5 years old in 1979 on a Commodore PET. In those days, the PET came with BASIC and no software, so my father who had brought one home showed me how to write some basic code.
Typical programs I could write at 5 were:
Print "What is your name?"
Input a$
For i = 1 to 100
Print a$ + " is great!"
Next i
We used to go into the local department store and write the following program and leave it running:
Print "K-Mart sucks!"
Goto 10
The average sales person at the time wouldn't know how to stop the program and have to turn off the computer!
This was cool stuff when I was 5. I started working with LOGO as well at an early age as well, doing lots of interesting things with turtle graphics, geometry, etc.
My daughter is now five, so I've been looking for programming languages to introduce her to programming concepts. The problem is that any programming language simple enough for a 5 year old is very lame by a modern computer's standards. LOGO was a big deal in 1979 because drawing a circle, a line, a square, etc. was pretty cool. When I showed my daughter LOGO this year, her reaction was, "That's all it does? I think I'll just go to a web site instead."
MIT has just published a new programming language that is graphical based, uses draggable commands and is focused on sound, animation, pictures, etc. I tested it out this evening with my daughter and it was a big hit! We created some animations together, did some drawing, added some sounds, etc. and then showed it to mom before bed.
If you have children or are simply interested, you can download the software here.
Wednesday, May 9, 2007
Developing an Analytics Platform for Microsoft .NET
A few weeks ago, I posted an article on a proposed new approach to analytics. I argued that we needed a new paradigm for analytics that goes beyond the static page analysis model found in major engines such as WebTrends, Google Analytics, etc.
So this weekend I started designing one in .NET and now have a pretty decent tracking model. I started with an article I found on .NET and tracking. I took Vikram's basic ideas and expanded them a little bit to add some more general concepts.
The basic architecture looks like this:

The terminology on the diagram comes from Domain Driven Design by Eric Evans.
I've tried developing this as a pretty basic framework and made it as light as possible so that if my site gets a lot it won't take a lot of a) memory or b) load to persist the data. But the architecture achieves what I wanted to achieve, namely it allows me to save event data based on userid, sessionid and affiliate referrer and to specify the nature of those events based on programming events (e.g. a transaction happening in the business domain) instead of simply tracking a logged URL.
Once the data is logged, we will design a data-mart that will suck the data out of the log and process it into some decent OLAP cubes for reporting purposes.
If you want to send me feedback on the ideas, please feel free to leave your comments...
So this weekend I started designing one in .NET and now have a pretty decent tracking model. I started with an article I found on .NET and tracking. I took Vikram's basic ideas and expanded them a little bit to add some more general concepts.
The basic architecture looks like this:

The terminology on the diagram comes from Domain Driven Design by Eric Evans.
I've tried developing this as a pretty basic framework and made it as light as possible so that if my site gets a lot it won't take a lot of a) memory or b) load to persist the data. But the architecture achieves what I wanted to achieve, namely it allows me to save event data based on userid, sessionid and affiliate referrer and to specify the nature of those events based on programming events (e.g. a transaction happening in the business domain) instead of simply tracking a logged URL.
Once the data is logged, we will design a data-mart that will suck the data out of the log and process it into some decent OLAP cubes for reporting purposes.
If you want to send me feedback on the ideas, please feel free to leave your comments...
Monday, May 7, 2007
Google Spreadsheets has less features than Excel for Windows 3.1
I was playing with Google Spreadsheets today and I found it lacking in some basic features. There are features that I remember being in Windows 3.1 Excel (Version 2.0?) that are missing in Google:
- Data extrapolation
- Double click a column to auto-size it
- Copying and pasting a column and having it re-size properly
- If you have a border that is boxed on all sides and then click the left border, it doesn't do anything.
- Clicking on ctrl to add non-contiguous cells together.
- Field names
- Autosorting of data
- Pivot tables
- Spell check
- Validation support
- Group and Outline
- Ability to fit print results on a page, see print preview in the spreadsheet
These are basic functions that I use all the time. In addition to these pretty basic features that are missing, the overall feeling of doing data entry is very slow - responsiveness is something that needs to be instant in a spreadsheet and there are too many operations that feel too slow for serious number crunching work.
So while Google Spreadsheet seems like a nice idea, its not something I could ever see replacing Excel.
See also my post previously on these types of "Internet applications" replacing desktop applications - looking at the current crop of examples, even a PocketPC running Windows CE has more application power than what's currently the best of the Internet applications.
The Half Life of a Good Post - About a Week
Last week I went to the MIX07 conference and blogged a lot. I acted like an intrepid reporter, posting up reviews of each session as I went to them. MIX07 became a top ten search term in technorati, so I received quite a bit of traffic to my blog.
So I wanted to find out - what is the half life of a successful blog entry? How much time does a blog entry live?
My conclusion - the half-life of a successful blog entry is about a week. After a week, traffic is almost completely gone from sites such as technorati, shoutwire, digg, etc. In addition, there doesn't seem to be much loyalty factor at least for my small blog - I haven't built enough brand power yet that I'm getting a lot of repeat visitors. Almost 70-80% of my visitors are brand new to my blog.
I'm not sure if this is typical, but it shows that attrition is very high and that bloggers who are not committed to posting every 2-3 days are not going to get any follow through in traffic. Its either post or perish in the blog world!
So I wanted to find out - what is the half life of a successful blog entry? How much time does a blog entry live?
My conclusion - the half-life of a successful blog entry is about a week. After a week, traffic is almost completely gone from sites such as technorati, shoutwire, digg, etc. In addition, there doesn't seem to be much loyalty factor at least for my small blog - I haven't built enough brand power yet that I'm getting a lot of repeat visitors. Almost 70-80% of my visitors are brand new to my blog.
I'm not sure if this is typical, but it shows that attrition is very high and that bloggers who are not committed to posting every 2-3 days are not going to get any follow through in traffic. Its either post or perish in the blog world!
Wednesday, May 2, 2007
MIX07 72 Hour Conversation Archive
Here is a list of the posts I made throughout the MIX07 conference. These were done in near real time as sessions ended. Enjoy!
Labels:
Microsoft,
Microsoft .NET,
MIX07,
Silverlight