Posted August 10, 2008 and filed under    tags: 

One thing I have noticed with BlogEngine is the absence of a printer friendly view of a post and comments.  For example, of you wanted to print out this post you would end up with a print out that included my sidebar and such.  Since this is a technical blog I sometimes write entries that include code samples and such that would make the print out be exceptionally long if printed within the margins of my theme.  So I set out to build a view that would provide a clean printer friendly view of a post.

I did a quick look at the syndication.axd process that looks up the page based on it's ID value:

Guid id = new Guid(Request.QueryString["id"]);
Post post = Post.GetPost(id);

Now that I have the post I need to set the title of the page and build the content of the page:

// Set Page Title
Page.Title = Server.HtmlEncode(post.Title);

// Build page content
StringBuilder sb = new StringBuilder();
                
// Add post title
sb.AppendLine("<div style=\"font-size: 130%; padding-bottom: 10px;\">" + Server.HtmlEncode(post.Title) + "</div>");

// Add post link
sb.AppendLine(string.Format("<div><a href='{0}' rel='me'>{0}</a></div>", post.AbsoluteLink.ToString()));

sb.AppendLine(string.Format("<div style=\"float: right;\">{0}</div>", post.DateCreated));
sb.AppendLine(string.Format("<div>Posted by:&nbsp;&nbsp;{0}<div>", post.Author));

// Add post description
if (!string.IsNullOrEmpty(post.Description))
sb.AppendLine(string.Format("<div>{0}<div>", post.Description));

// Add post content
sb.AppendLine(string.Format("<hr />{0}<hr />",post.Content));

Now I need to add any comments that are included in the post:

 

if (post.ApprovedComments.Count >= 1)
{
    //Add approved Comments
    sb.AppendLine(string.Format("<div style=\"font-size: 130%; padding-bottom: 10px;\">Comments ({0})</div>", post.ApprovedComments.Count));
    sb.AppendLine("<div id=\"comments\">");
    foreach (Comment comment in post.ApprovedComments)
    {
        StringBuilder cm = new StringBuilder();
        sb.AppendLine("<div id=\"comment\" style=\"margin-bottom: 5px; margin-top: 5px;\">");
        sb.AppendLine(string.Format("<div style=\"float: right;\">{0}</div>", comment.DateCreated));
        sb.AppendLine(string.Format("<div>by <strong>{0}</strong>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"mailto:{1}\">{1}</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"{2}\" target=\"_blank\" rel=\"friend\">{2}</a></div>", comment.Author, comment.Email, comment.Website));
        sb.AppendLine(string.Format("<div><blockquote>{0}</blockquote></div>", comment.Content));
        sb.AppendLine("</div>"); 
    }
    sb.AppendLine("</div>");
}

Finally I render the page:

HtmlGenericControl spanControl = new HtmlGenericControl("span");
spanControl.InnerHtml = sb.ToString();
form1.Controls.Add(spanControl);

The results can be seen by looking at this page using the printer friendly view.  You can download the Printer Friendly page if you want to use this on your page.

PrinterFriendly.zip

If you liked this article why not share it with others?

Kick it up to DotNetKicks.com

Comments

Add comment


(Will show your Gravatar icon)

biuquote
Loading