Wednesday, December 31, 2008

New DVD drive in notebook doesn't work (+ fix)

I had to replace the DVD-RW drive in one of our notebooks today. It is the older IDE/ATAPI type. When I plugged the drive into the laptop, it would not boot and BIOS kept displaying that
- The hard disk is S.M.A.R.T. capable, but command failed
- There is no DVD drive
- Boot from hard drive failed

It turned out that the new DVD-RW had a different Master/Slave/Cable option selected than the original drive. This is no big deal with a regular-sized burner, but laptop drives apparently have the option hard-wired and it cannot be easily changed.

The solution was quite simple - I have noticed that unlike the DVD-RW, the notebook's hard drive has Master/Slave/Cable pins on its back. I didn't have a notebook-sized jumper at hand, so I shorted the two "cable select" pins with a piece of wire and voila. The only glitch is that the hard disk is now the slave and DVD-RW is the master (I wonder why the cable is connected this way), but it doesn't seem to affect their performance, so who cares..

Labels: ,

Monday, December 29, 2008

Websites turn pink in Firefox

I don't know when it happened exactly, but over the last couple of days I have noticed that some websites have turned pink in my browser. Most notably, my Google Reader is now blue, white and pink.

Even the websites I have created myself are now part pink. It doesn't happen in IE, only in Firefox. The color settings in Firefox are just fine, the default background color is still set to white and I have tried resetting it to no avail.

I have no idea why this is happening and searching turns out only like two other people with the same problem, but no resolution.


Friday, December 26, 2008

Generating and downloading an XPS document using ASP.NET

Windows Presentation Foundation includes a new API for generating XPS documents. This article describes the steps involved in generating an XPS document on the server side of an ASP.NET web application using WPF and sending the resulting document to the client browser.

What we need to do is create a flow document, convert it to XPS and send it to the client.

Step 1 - Creating a FlowDocument on the server

Creating a FlowDocument object from scratch is as simple as calling the constructor:

FlowDocument flowDocument = new FlowDocument();
This gives us an empty FlowDocument that we can fill with content. The content is represented by a collection of Blocks. Filling the Blocks collection manually will probably suffice for very simple documents
Paragraph p1 = new Paragraph();
p1.Inlines.Add("This is the text");
p1.Inlines.Add("of the first paragraph");
Section section1 = new Section(p1);

but in a real project, we will most likely need some kind of a FlowDocument generator and/or have a predefined document template that we will fill with data. The natural format for such template is a XAML file.
<FlowDocument xmlns=""
    <Section FontFamily="Times New Roman">
      <Paragraph Fontcolor: rgb(139, 0, 0);">18" FontWeight="Bold" >@placeholder1</Paragraph>
      <Paragraph Fontcolor: rgb(139, 0, 0);">10">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit,
        sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua.
@placeholder1 and @placeholder2 will later be replaced by live data.

The template file can be saved (for example) as a global resource and later accessed by calling

string pageTemplate = (string)HttpContext.GetGlobalResourceObject("Resources", "MyTemplate");
Now that we have the template in memory as a string, it is a good time to replace the placeholders with real data and convert the final template to a real FlowDocument object
pageTemplate = pageTemplate.Replace("@placeholder1", MyData1);
pageTemplate = pageTemplate.Replace("@placeholder2", MyData2);
FlowDocument flowDocument = (FlowDocument)XamlReader.Parse(pageTemplate);
The placeholder replacement process is really lame here, but you get the point...

At this point, we have a filled-in FlowDocument object in memory ready to be converted to XPS.

Step 2 - Converting a FlowDocument to an (in-memory) XPS file

We start with a FlowDocument object and end up with an XPS file saved as an array of bytes. A code snippet is worth a thousand words here:
public static byte[] FlowDocumentToXPS(FlowDocument flowDocument, int width, int height)
    MemoryStream stream = new MemoryStream();

    // create a package
    using (Package package = Package.Open(stream, FileMode.Create))
        // create an empty XPS document            
        using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum))
            // create a serialization manager
            XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
            // retrieve document paginator
            DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;

            // set page size
            paginator.PageSize = new System.Windows.Size(width, height);       
            // save as XPS
        return stream.ToArray();

Step 3 - Sending an in-memory file to the client browser

If we want to generate the XPS file (or any other kind of file) based on a client request, it is very inconvenient to have to save the file on the server and give the client its URI.

What we will do instead is create a hidden form on the web page and send the XPS file to the client as a response to submitting this form.

The hidden form can be very simple, all we need to include is the parameters that specify the XPS (or other) file to retrieve.

<form id="generateFileForm" action="DownloadFile.aspx" method="post">
    <input runat="server" type="hidden" id="id" />

Once we fill in the "id" parameter and submit the form

HtmlDocument doc = HtmlPage.Document;
HtmlElement id = doc.GetElementById("id");
id.SetAttribute("value", MyDocumentId);

the code-behind of DownloadFile.aspx then does the trick

public partial class DownloadFile : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        string sid = Request.Form["id"];     
        byte[] bytes = GetXpsFileBytes(sid);
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=document.xps");
        Response.OutputStream.Write(bytes, 0, bytes.Length);

The [StaSyncOperationBehavior] attribute was developed by Scott Seely and you can read about it here. UPDATE: The blog no longer seems to work, but here is the source code for the attribute.
To ensure that the response generation runs on a STA thread, we also need to set the AspCompat attribute on the DownloadFile.aspx page like this

<%@ Page AspCompat="true" Language="C#" AutoEventWireup="true" CodeBehind="DownloadFile.aspx.cs" Inherits="MyNamespace.DownloadFile" %>


The good thing about this solution is that nothing is saved to disk on the server side and that the client page does not reload when requesting the document, which means that you can happily use it to generate and download documents in Silverlight applications.

I hope this experience of mine will be useful to someone and if you have any questions or comments, please let me know in the discussion.


Labels: , , ,