Latest

Movies:

Game of Thrones Amazing Spider-Man, The Cabin in the Woods, The

Photoalbums:

Amsterdam Tenerife Forest Fires Tenerife North

Printing XPS files to a physical printer

2013-03-21 12:43
I had the need to programatically print an XPS file, but a couple of hours of Google searches only returned examples of how to print TO an XPS file. It was extremely difficult to find how to send a XPS file to an actual physical printer.
Eventually something turned up and the solution seemed to be the XpsDocument class.

Look at this simplified example to see how easy it turned out to be:


PrintDialog dlg = new PrintDialog();
XpsDocument xpsDoc = new XpsDocument(PATH_TO_YOUR_XPS_FILE_HERE, System.IO.FileAccess.Read);
dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Document title");
 


As you can see, reading a XPS file and then sending the contents to a printer is not very hard at all.

Looking back at getting lost near Grand Canyon

2012-09-14 14:47
Having recently published my big Californian vacation video to YouTube I think it's time to reminisce a bit about my adventures.

As you already know from seeing the video and by photos in my photo album I went to Grand Canyon. Before traveling to the US, a colleague of mine mentioned the place where he visited Grand Canyon West and suggested I go there to.
I looked up the place on Google Maps, found the GPS coordinates and plotted those into my GPS to get driving directions.
My colleague warned me that the drive would seem a bit wrong because you would have to go onto some gravel roads, so when my GPS directed me onto a gravel road I didn't think much of it.
I did get a bit more suspicious when the gravel road got more narrow and led into some mountains. My worry reached its peak when I suddenly got to a sign that said "Only 4WD vehicles beyond this point". My GPS advised me to continue, but the sign, and the fact that I didn't have a 4WD car, advised me to turn around. What to do?!

Read more

Posted in Life, Travel | No Comments

Attention comment spammers

2012-09-12 17:44
I have now added a nofollow attribute to all links in the comments section, so spammers will no longer get increased page ranks from Google or other search engines.

For non-spammers, add the following to an anchor tag and google won't follow that link:

rel="nofollow"
Posted in Technology | No Comments

For my sister

2012-09-03 16:20
My sister got married on Saturday, September 1.
There was a great party with many great speeches. I'm not claiming greatness on my own, but I will post it here (translated to English for my foreign friends) for everybody else to enjoy (for the first time, or again if you were there).
I said it many times that day, but I'll say it here again.

Congratulations to my sister and her husband!

Read more

Posted in Life | No Comments

Change image tag generated by CKEditor

2012-06-11 21:08
For a new project of mine I'm using a rich text editor called CKEditor.
This is a highly configurable editor that, unfortunately, suffers from cluttered and not easily understandable documentation.
I needed to change the generated HTML from the editor so that images that were resized not only had their size set as style attributes, but as query string variables.

So I needed this:
<img src="http://yoursite.com/photo.jpg" style="width:150px; height:200px;" />

to be output as this:
<img src="http://yoursite.com/photo.jpg?width=150&height=200" style="width:150px; height:200px;" />

That proved to be easier said than done, but after quite a lot of googling, asking on several forums, and getting some helpful (and some not helpful at all) replies, I finally ended up with the following:


Put the following code into CKEditor's config.js

CKEDITOR.on('instanceReady', function (ev) {
    var editor = ev.editor,
        dataProcessor = editor.dataProcessor,
        htmlFilter = dataProcessor && dataProcessor.htmlFilter;

    htmlFilter.addRules( {
        elements : {
            $ : function( element ) {
                // Output dimensions of images as width and height attributes on src
                if ( element.name == 'img' ) {
                    var style = element.attributes.style;
                    if (style) {
                        // Get the width from the style.
                        var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec( style ),
                            width = match && match[1];

                        // Get the height from the style.
                        match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec( style );
                        var height = match && match[1];

                        var imgsrc = element.attributes.src + "?width=" + width + "&height=" + height;

                        element.attributes.src = imgsrc;
                        element.attributes['data-cke-saved-src'] = imgsrc;
                    }
                }
            }
        }
    });
});


This code is run whenever the CKEditor generates the actual HTML, which happens when you either view the source by clicking the "Source" button, or by performing an HTTP POST of that page.

A small warning, though. The code above will keep appending the width and height query strings for each click on the "Source" button, or for each postback, so you might want to add some extra logic to filter out the width and height query strings before appending them to the src attribute. (I might edit this post in the future with the correct syntax for that ;))

Art history lesson

2012-02-26 19:23
Art history lesson
Today my nephew got baptized. Let's not dwell on that and look at what I got him instead.
A few days ago I actually went into an art gallery and bought a piece of art. There's certainly a first time for everything! I hope the little guy will appreciate a genuine piece of art as he grows older.

What I got him was this lithograph from artist Per Krohg, son of the renowned Christian Krohg. Per originally made a series of pictures for his son Guy when he was a child. Each picture represented a letter in the alphabet. The family of artists were typical of their time and lived in France, so each picture also represented something in French.
Unfortunately the gallery didn't have the letter L, so I got the letter C (for "Collage" or "la chambre") instead because I liked the motif and it had a fitting look for a baby room. The picture is also numbered, so there are apparently 75 copies and this is number 35.
I'm interested in more information about this series, so if anybody out there can help do write me an email or a comment on this post.
Posted in Life | No Comments

Response.Redirect inside an UpdatePanel

2011-10-18 16:37
Recently I've had more and more pages displaying unexpected behaviour.
I had an UpdatePanel with a button inside it. The button's Click event performed a Response.Redirect(), but nothing happened.
The Javascript error console showed the following error message:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.


After some googling it seems that ASP.Net doesn't quite support Response.Redirect() inside UpdatePanels any more.

The solution was to make sure the button actually performed a full postback of the webpage, and not only a partial postback (as it would do because it was inside an UpdatePanel).

There are two ways of doing this. First the easy way.

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Button ID="MyButton" runat="server" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="MyButton" />
    </Triggers>
</asp:UpdatePanel>


Here you're basically telling the UpdatePanel that "MyButton" should perform a full postback instead of a partial one.

My problem was a bit more complicated, though, because "MyButton" was inside a GridView. This complicated things a bit, but not too much. Mainly you have to have access to the ScriptManager.

Add the OnRowDataBund event to your GridView. In this event do the following:

protected void MyGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button MyButton = (Button)e.Row.FindControl("MyButton");
        ScriptManager1.RegisterPostBackControl(MyButton);
    }
}


So instead of telling the UpdatePanel that your button should do a full postback, you're telling the ScriptManager the same thing.
Doing this in code-behind would obviously work for the first example as well.

UPDATE:

This seems to be a recurring bug in .Net 2.0, 3.0 and 3.5, and is actually fixed in .Net 4.x.
I have successfully got a Response.Redirect() to work inside an update panel without using any of the above fixes, and only by upgrading my solution, projects and website to .Net 4.0.

Samba/Winbind connection issues

2011-10-15 13:08
I recently had to reconfigure my linux (Ubuntu 10.04 LTS) servers.
I had previously used these two howto's to configure my servers.
Suddenly one of them refused to properly connect with various error messages:

# wbinfo -p
Ping to winbindd failed

# wbinfo -u
Error looking up domain users

# wbinfo -t
checking the trust secret via RPC calls failed
Could not check secret

After a week of googling I finally found the following solution, which did the trick for me.

Stop smbd, nmbd and winbindd (make sure they are really dead using ps. winbindd still lingered after I stopped the service)
Delete the linux computer from the Primary Domain Controller (using the Management Console)
Delete the secrets database (/var/lib/samba/secrets.tdb)
Join the domain again
Start the daemons (smbd, nmbd and winbindd)
Test the winbind commands to see that everything is working

Trying a new thing with my blog

2011-10-15 11:23
Because I'm using Twitter, Facebook and Google+ I haven't really made any good posts to my blog for a while.
I'm trying to remedy this by taking my blog in a new direction.
As a "Technology Enthusiast" I regularly get stuck in various situations where I have to do a lot of googling. Some times I can google for a week and not find a solution.
When I finally do find a solution I've decided I should write about it on my blog so that others can (maybe) find the solution as well.
So don't be surprised when you see lots of technical stuff popping up on this blog instead of the usual ramblings about what happens (or not) in my life.

Give a little bit of yourself today and save a life tomorrow!

2011-06-14 08:15
Give a little bit of yourself today and save a life tomorrow!
Today is the World Blood Donor Day. Donating blood is such an easy thing to do, and is also a very easy way to help save lives. You only give about 15-30 minutes of your time, and a pint of blood. Seeing how many lives are saved each year because of blood donors, the choice shouldn't really be that hard to make, but still there is a profound lack of available blood in hospitals around the world.

I am, of course, a donor. Even though I hate syringes and needles, I go through with the donation roughly once every three months. Being such an easy way to help others, I gladly do it. I get a gift from the local hospital every time I do it, but I would continue doing it even if I got nothing at all.

Use today as an incentive to go to your local blood bank and sign up to be a donor! If you're at work, tell your boss you're taking a few moments off work to go register. The boss should welcome this action. If he doesn't, remind him that some day maybe he or his family could be needing blood.
I'm doing my part, are you?

For more information, see the following sites:

American Red Cross, USA
Give Blood, UK
GiBlod.no, Norway