Wednesday, February 22, 2006

GreatNews Feed Reader

GreatNews is a free feed (RSS/Atom) reader that can work off portable drives and supports syncing with Bloglines.com, full page reading (newspaper style, Sage (Firefox feed reader) style, and others), labeling and news watches.


Tags: , , ,

Tuesday, February 21, 2006

iTextSharp: Generate a PDF file containing a table (ASP.NET/C#)

Contining on from iTextSharp: Generating a Basic PDF file (ASP.NET/C#), here is a demo of how to generate a PDF document with a table in it.

TablePDF.ashx

<%@ WebHandler Language="C#" Class="MyNamespace.TablePDF" %>
using System;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace MyNamespace
{
 public class TablePDF: IHttpHandler 
 {
  
  public bool IsReusable
  {
   get
   {
    return true;
   }
  }
  
  /// <summary>
  /// Font used for table headers
  /// </summary>
  private Font TableHeaderFont
  {
   get
   {
    return new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
   }
  }
  
  public void ProcessRequest(HttpContext ctx)
  {
   // make sure it is sent as a PDF
   ctx.Response.ContentType="application/pdf";
   // make sure it is downloaded rather than viewed in the browser window
   ctx.Response.AddHeader("Content-disposition", "attachment; filename=TablePDF.pdf");
   
   // create a MemoryStream (as there may not be write access to any folder on the server)
   using(MemoryStream m = new MemoryStream())
   {
    // create the PDF document
    Document document = new Document(PageSize.A4);
    PdfWriter.GetInstance(document, m);
    
    // open document to add content
    document.Open();
    
    // add table with 3 columns
    Table myTable = new Table(3);
    myTable.AutoFillEmptyCells = true;
    // add padding to table cells
    myTable.Cellpadding = 3.5f;
    // make sure table fits the width of the page
    myTable.WidthPercentage = 100.0f;
    
    // remove borders
    //myTable.DefaultCellBorder = Rectangle.NO_BORDER;
    //myTable.Border = Rectangle.NO_BORDER;
    
    // create cell
    Cell myCell = new Cell();
    
    // add header cell
    myCell.Header = true;
    myCell.Add(new Chunk("Header 1",TableHeaderFont));
    // row 1 column 1
    myTable.AddCell(myCell, 0, 0);
    
    // new header
    myCell = new Cell();
    myCell.Header = true;
    myCell.Add(new Chunk("Header 2",TableHeaderFont));
    // row 1 column 2
    myTable.AddCell(myCell, 0, 1);
 
    // another new header
    myCell = new Cell();
    myCell.Header = true;
    myCell.Add(new Chunk("Header 3",TableHeaderFont));
    // row 1 column 3
    myTable.AddCell(myCell, 0, 2);
 
    // add some data (6 rows worth)
    for (int i=1; i<=6; i++)
    {
     // on the third iteration, span several columns
     if(i == 3)
     {
      myCell = new Cell();
      myCell.Colspan = 3;
      myCell.HorizontalAlignment = Element.ALIGN_CENTER;
      myCell.Add(new Chunk("R" + (i + 1) + "C1 - " + "R" + (i + 1) + "C3"));
      myTable.AddCell(myCell, i, 0);
     }
     else
     {
      myCell = new Cell();
      myCell.Add(new Chunk("R" + (i + 1) + "C1"));
      myTable.AddCell(myCell, i, 0);
      
      myCell = new Cell();
      myCell.Add(new Chunk("R" + (i + 1) + "C2"));
      myTable.AddCell(myCell, i, 1);
      
      myCell = new Cell();
      myCell.Add(new Chunk("R" + (i + 1) + "C3"));
      myTable.AddCell(myCell, i, 2);
     }
    }
    // add table
    document.Add(myTable);
    
    // close the document
    document.Close();
    
    // stream the PDF to the user
    ctx.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
   }
   ctx.Response.End();
  }
 }
}

Tags: , , , , ,

Tuesday, February 14, 2006

Yahoo! User Interface Library and Design Pattern Library

Yahoo! User Interface Library is a JavaScript library (under the open source BSD license) that can be used to make a site more interactive. It is comprised of 'Core Utilities' and 'UI Controls'.

Core Utilities

  • Animation
  • Connection Manager (XMLHttpRequest / AJAX)
  • DOM (manipulating the elements (style, coordinates) in a loaded web page)
  • Drag and Drop
  • Event (adding them to existing elements on a page)

UI Controls

  • Calendar
  • Slider
  • TreeView

Yahoo! Design Pattern Library is a set of solutions to common problems (breadcrumbs, autocomplete, drag/drop, pagination etc). There is no code, but it does demonstrate the solutions Yahoo uses for its sites.

They also have a blog about it: Yahoo! User Interface Blog.


Tags: , , , ,

Monday, February 13, 2006

Add .NET Framework Folder to PATH via batch file (update)

Add .NET Framework Folder to PATH via batch file has been updated. This is a batch file that adds the .NET framework directory (i.e. where the command line compilers csc, vbc etc are in) to the PATH system variable. It does not add the path if it is already set, so it is of most use to those that have not done it manually (the SDK doesn't add the directory on install).


Tags: ,

Thursday, February 09, 2006

How to generate an iCalendar file (ASP.NET/C#)

It is fairly simple to generate an iCalendar file (for calendar clients like Microsoft Outlook or Mozilla Sunbird) as it is just a plain text file with the extension 'ics'. A WebHandler is the best method of doing this.

Update (10 Feb 2006): Now works in Sunbird. When prompted to open / save the file, choose to download. Then in Sunbird, go to File > Import and browse for the saved file.

iCalendar.ashx

<%@ WebHandler Language="C#" Class="MyNamespace.iCalendar" %>
using System;
using System.Web;
namespace MyNamespace
{
 public class iCalendar: IHttpHandler 
 {
  
  public bool IsReusable
  {
   get
   {
    return true;
   }
  }

  string DateFormat
  {
    get
    {
      return "yyyyMMddTHHmmssZ"; // 20060215T092000Z
    }
  }
  
  
  public void ProcessRequest(HttpContext ctx)
  {
   DateTime startDate = DateTime.Now.AddDays(5);
   DateTime endDate = startDate.AddMinutes(35);
   string organizer = "foo@bar.com";
   string location = "My House";
   string summary = "My Event";
   string description = "Please come to\\nMy House";
   
   ctx.Response.ContentType="text/calendar";
   ctx.Response.AddHeader("Content-disposition", "attachment; filename=appointment.ics");
   
   ctx.Response.Write("BEGIN:VCALENDAR");
   ctx.Response.Write("\nVERSION:2.0");
   ctx.Response.Write("\nMETHOD:PUBLISH");
   ctx.Response.Write("\nBEGIN:VEVENT");
   ctx.Response.Write("\nORGANIZER:MAILTO:" + organizer);
   ctx.Response.Write("\nDTSTART:" + startDate.ToUniversalTime().ToString(DateFormat));
   ctx.Response.Write("\nDTEND:" + endDate.ToUniversalTime().ToString(DateFormat));
   ctx.Response.Write("\nLOCATION:" + location);
   ctx.Response.Write("\nUID:" + DateTime.Now.ToUniversalTime().ToString(DateFormat) + "@mysite.com");
   ctx.Response.Write("\nDTSTAMP:" + DateTime.Now.ToUniversalTime().ToString(DateFormat));
   ctx.Response.Write("\nSUMMARY:" + summary);
   ctx.Response.Write("\nDESCRIPTION:" + description);
   ctx.Response.Write("\nPRIORITY:5");
   ctx.Response.Write("\nCLASS:PUBLIC");
   ctx.Response.Write("\nEND:VEVENT");
   ctx.Response.Write("\nEND:VCALENDAR");
   ctx.Response.End();
  }
 }
}

Wednesday, February 08, 2006

iTextSharp: Generating a Basic PDF file (ASP.NET/C#)

iTextSharp is free library for .NET that allows you to create PDF documents. It can be used to dynamically create PDF's which can be streamed to the user. As there is no HTML being sent to the user, a WebHandler (ashx file) is a more appropriate way to generate your PDF than an aspx page. Download itextsharp-3.0.10-dll.zip (latest version as of 08 Feb 2006) and save the dll in the archive to your websites bin directory.

Here is a basic sample of creating a PDF (more complex samples may follow in future posts).

BasicPDF.ashx

<%@ WebHandler Language="C#" Class="MyNamespace.BasicPDF" %>
using System;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace MyNamespace
{
 public class BasicPDF: IHttpHandler 
 {
  
  public bool IsReusable
  {
   get
   {
    return true;
   }
  }
  
  /// <summary>
  /// Font used for any hyperlinks added to the PDF
  /// </summary>
  private Font LinkFont
  {
   get
   {
    return new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.UNDERLINE, Color.BLUE);
   }
  }
  
  public void ProcessRequest(HttpContext ctx)
  {
   // make sure it is sent as a PDF
   ctx.Response.ContentType="application/pdf";
   // make sure it is downloaded rather than viewed in the browser window
   ctx.Response.AddHeader("Content-disposition", "attachment; filename=BasicPDF.pdf");
   
   // create a MemoryStream (as there may not be write access to any folder on the server)
   using(MemoryStream m = new MemoryStream())
   {
    // create the PDF document
    Document document = new Document(PageSize.A4);
    PdfWriter.GetInstance(document, m);
    
    // set meta data
    document.AddTitle("Test PDF");
    document.AddSubject("This is a PDF generated by a WebHandler in ASP.NET");
    document.AddKeywords("test document,foo,bar,baz");
    document.AddAuthor("John Doe");
    document.AddCreator("My Web Application");
    
    // open document to add content
    document.Open();
    
    // create a paragraph 
    Paragraph p = new Paragraph();
    
    // create a phrase object (which will contain the text to go in the paragraph)
    Phrase content = new Phrase();
    
    // start adding content to the phrase
    content.Add(new Chunk("Download iTextSharp from: "));
    
    Anchor link = new Anchor("itextsharp.sourceforge.net",LinkFont);
    link.Reference = "http://itextsharp.sourceforge.net";
    
    // add link to phrase
    content.Add(link);
    
    // add the phrase to the paragraph
    p.Add(content);
    
    // add the paragraph to the document
    document.Add(p);
    
    // add another paragraph
    document.Add(new Paragraph("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. "
     + "Praesent ante dui, adipiscing ac, pretium in, cursus non, dui. "
     + "Vivamus risus tellus, semper non, posuere eget, elementum eu, nulla. "
     + "Nulla scelerisque arcu et odio."));
    
    // close the document
    document.Close();
    
    // stream the PDF to the user
    ctx.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
   }
   ctx.Response.End();
  }
 }
}

Tags: , , , , ,

Monday, February 06, 2006

Alternatives to innerHTML

Alternatives to innerHTML (slayeroffice article) gives many examples of working with the DOM and modifying a document without resorting to innerHTML. The problem with innerHTML is that it could cause issues with strict XHTML documents (served with the correct mime type, application/xhtml+xml) and also can result in illegible code (may be hard to decipher another developers code).


Tags: , ,

Thursday, February 02, 2006

Clean Word Html Update

Clean Word Html has been updated with a few bug fixes.


Tags: