Inserting Images with Aspose Word

So I the requirement for a project was to have documents to be mailed to customers show to render from a mvc website so a user could print them and mail them out.  We contemplated connecting to the printer from the web server and forcing the print, but we decided to mail merge the document together and simply dump it to the end user to print out through their own means.  Which down the road will save us time.  Because there could be hundreds of these a day, we decided to do all of them in the same word document so only one download and one print would be needed.  So I had to do a multi merge, not too hard.  But then we needed to put labels in there document.  Inserting images, isn't straight forward I quickly learned.

To accomplish this, I first created a controller function for it looked like the following:

public ActionResult Print(string id, string carrier)
            var ctx = new ContextManaged();
            var returns = get data;
            //do printing
            var doc = new Document(HttpContext.Server.MapPath(Config.GetString("PrintTemplateFile")));
            doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob(HttpContext);
            var ds = new DataSet();
            foreach (var ret in returns)
                var dr = ds.Tables["Returns"].NewRow();
                dr["CustomerName"] = ret.CustomerName;
                dr["AddressLine1"] = ret.CustomerAddressLine1;
                dr["AddressLine2"] = ret.CustomerAddressLine2;
                dr["City"] = ret.CustomerCity;
                dr["State"] = ret.CustomerState;
                dr["Zip"] = ret.CustomerZip;
                dr["OrderNumber"] = ret.OrderNumber;
                dr["Date"] = DateTime.Now.ToString("MMMM d, yyyy");
                dr["CompanyName"] = ret.tbl_Company.CompanyName;
                if (ret.Image != null)
                        dr["Image"] = ret.Id;
                    catch (Exception ex)
                        dr["Image"] = "";
                    dr["Image"] = "";

            var ms = new System.IO.MemoryStream();
            doc.Save(ms, SaveFormat.Doc);
            ms.Position = 0;
            var fileStreamResult = new FileStreamResult(ms, "application/ms-word");
            fileStreamResult.FileDownloadName = "PrintReturns.doc";
            return fileStreamResult;

Then I created functions that overrides the Image Field merging and puts them into the word document as a stream.  This was a bit tricky, but it works.  I also had a function to resize the image as my images were too large.

/// <summary>
        /// This is called when mail merge engine encounters Image:XXX merge field in the document.
        /// You have a chance to return an Image object, file name or a stream that contains the image.
        /// </summary>
        void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
            if (e.FieldValue.ToString() == String.Empty)
                e.ImageStream = new FileStream(context.Server.MapPath(Config.GetString("NoImage")), FileMode.Open);
                    //get image
                    var ctx = new PPSHP_ShippingServiceContextManaged();
                    var returns = (from ret in ctx.tbl_Returns where ret.Id == Convert.ToInt32(e.FieldValue) select ret.Image).FirstOrDefault();
                    //resize image
                    var bit = ResizeBitmap(new System.Drawing.Bitmap(new MemoryStream(returns.ToArray())), 700, 500);
                    // The field value is a byte array, just cast it and create a stream on it.
                    var ms = new MemoryStream();
                    bit.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    MemoryStream imageStream = ms;
                    // Now the mail merge engine will retrieve the image from the stream.
                    e.ImageStream = imageStream;
                catch (Exception ex)
                    e.ImageStream = new FileStream(context.Server.MapPath(Config.GetString("BadImage")), FileMode.Open);

        Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
                //draw border
                g.DrawLine(new Pen(Brushes.Black, 2), new Point(0, 0), new Point(0, nHeight));
                g.DrawLine(new Pen(Brushes.Black, 2), new Point(0, nHeight), new Point(nWidth, nHeight));
                g.DrawLine(new Pen(Brushes.Black, 2), new Point(nWidth, nHeight), new Point(nWidth, 0));
                g.DrawLine(new Pen(Brushes.Black, 2), new Point(nWidth, 0), new Point(0, 0));
            return result;

Starting a blog with BlogEngine . Net

I decided to create a blog that covers the various projects I do from day to day.  I am an Engineer by trade but am always busy building something.  In order to leave more time for building things, I decided to use the BlogEngine.Net because, it works with my winhost account, I understand the code, it has good support and theme options, and its free.  

Half the reason I created this blog is simply to remember the things I do, almost like a timeline of projects.  So its really for me, but if anyone else finds it useful, good for you.

Tiling a Kitchen Backsplash

So I in the building of my home and kitchen, I wanted to tile the backsplash.  This is nothing too exciting but I wanted to do something like a mural you see in so many awesome houses.  So I set out to find the right set of tiles to stick out but also work with the kitchen.  We ended up finding some copper tiles online to match our copper sink and hardware in the kitchen, and the smaller tiles for the backsplash matched the floor.  Along with some accent pieces from the same tile family at Menards I was off. 

The first thing I did was measure the space for my backsplash mural.  Once I knew my dimensions, I started setting tile out on my counter to fit the size I had and then trying to center and organize to make the best of the space.  This is the pattern I would try to create.

Now all the tiles are square, so this was going to require quite a bit of cutting.  Once I had cut all the pieces, I again set it out to make sure it was all going to fit.  This should also make the tile placing go much faster.


I must say, I thought I did good, and expected it to all fit nicely, I was wrong, I had to fudge edges and recut corners because it didn't quite fit as well on the wall, it still went rather smoothly and from the time cutting started, it only took 4 hours to have the last of the mural up.

Once grouted it looked rather good, I am happy and just need to finish off my edges.

Chicken Coop

o I had just moved to the country, and decided I wanted some chickens, problem, I didn't have a place for them.  Well, I wanted to enhance my carpentry skills anyways, so I decided to build a very classic looking chicken coop.  Basically, I created a foundation of cinder blocks, created a 2x6, 16 inch on center sub floor, covered it with plywood and then vinyl, and famed up walls and nesting boxes.Next I covered the outside with plywood and put a roof on, it is roofed with metal.
 Finally, I sided the coop with recycled wood from another shed that needs to come down. 
After that, I painted it barn yard red with white trim.  I built a door for it and put doors over the egg access holes to the nesting boxes.  I also created a frame for an old window I found to swing out in the summer.  I also added a run to the right in 2013 so they can have some protection when I'm not around, but not be cooped up.