<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>jp.hamilton</title>
    <description>software craftsman</description>
    <link>http://jphamilton.net/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.Net Syndication Generator 1.0.0.0 (http://dotnetblogengine.net/)</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://jphamilton.net/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>J.P. Hamilton</dc:creator>
    <dc:title>jp.hamilton</dc:title>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/JpHamilton" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>ReSharper 5.0 &amp;ndash; Now with VS 2010 Beta 2</title>
      <description>&lt;p&gt;I just checked the &lt;a href="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+5.0+Nightly+Builds"&gt;ReSharper 5.0 Nightly Builds&lt;/a&gt;. The comments for the 10/29 build says “Initial VS 2010 beta2 support”. I am installing this the second I get home.&lt;/p&gt;</description>
      <link>http://jphamilton.net/post/ReSharper-50-ndash3b-Now-with-VS-2010-Beta-2.aspx</link>
      <author>jp</author>
      <comments>http://jphamilton.net/post/ReSharper-50-ndash3b-Now-with-VS-2010-Beta-2.aspx#comment</comments>
      <guid>http://jphamilton.net/post.aspx?id=7b92ed43-bed4-4ff3-9424-af4ada268ac0</guid>
      <pubDate>Thu, 29 Oct 2009 01:15:29 -0800</pubDate>
      <dc:publisher>jp</dc:publisher>
      <pingback:server>http://jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://jphamilton.net/post.aspx?id=7b92ed43-bed4-4ff3-9424-af4ada268ac0</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://jphamilton.net/trackback.axd?id=7b92ed43-bed4-4ff3-9424-af4ada268ac0</trackback:ping>
      <wfw:comment>http://jphamilton.net/post/ReSharper-50-ndash3b-Now-with-VS-2010-Beta-2.aspx#comment</wfw:comment>
      <wfw:commentRss>http://jphamilton.net/syndication.axd?post=7b92ed43-bed4-4ff3-9424-af4ada268ac0</wfw:commentRss>
    </item>
    <item>
      <title>WPF Makes Printing Labels Easy</title>
      <description>&lt;p&gt;&lt;a href="http://code.google.com/p/labelsharp2/"&gt;Download Code Here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In my&lt;a href="http://www.jphamilton.net/post/Printing-Labels-in-C.aspx"&gt; last article&lt;/a&gt; on printing labels, I used PDFsharp to do the heavy lifting. I thought it was a pretty good solution for someone who was stuck in .NET 2.0. &lt;/p&gt;  &lt;p&gt;If you are not in that situation, I have a better solution: WPF. There are several classes available which make layout and printing very easy. Print preview is handled by a WPF control called DocumentViewer. I originally got the idea from &lt;a href="http://www.neodynamic.com/ND/FaqsTipsTricks.aspx?tabid=66&amp;amp;prodid=9&amp;amp;sid=99"&gt;Neodynamic&lt;/a&gt;. They have some sample code which is hard-coded to print sheets of Avery 5160 labels. I refactored their code, then rewrote the whole thing to be the starting point of a reusable library that can be used to print any size labels. But, kudos to them for the original idea.&lt;/p&gt;  &lt;p&gt;Here’s how it works.&lt;/p&gt;  &lt;p&gt;First, you need to create your actual label. This will be a WPF user control that is sized to the appropriate dimensions. You can layout the label anyway you want, of course. WPF uses 96 DPI internally (referred to as DIU for &lt;em&gt;device independent units&lt;/em&gt;).&amp;#160; So, label sizes in inches can use: &lt;em&gt;inches * 96&lt;/em&gt; to get the appropriate size in pixels. I’ll leave converting from cm and mm to you, the reader.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jphamilton.net/image.axd?picture=WindowsLiveWriter/WPFMakesPrintingLabelsEasy/009F3BD5/label.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="label" border="0" alt="label" src="http://www.jphamilton.net/image.axd?picture=WindowsLiveWriter/WPFMakesPrintingLabelsEasy/1A7602B5/label_thumb.jpg" width="413" height="225" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;&lt;em&gt;Figure 1. Label sized to 384 x 192 (4” x 2”).&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;After you design your label, you need to create a sheet to add them to. At the time of this writing, the library includes classes for Avery 5160, 5163, and 6873 labels, but defining your own sheets is easy. Just derive from LabelSheet and set the measurements in the constructor. Here’s the code for the Avery 6873 sheet. Notice that all measurements are entered in native units:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:3cb97c6b-7e81-4425-af04-fcdd37de1c24" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;public class Avery6873 : LabelSheet
{
    public Avery6873() : base(LabelUnit.Inches)
    {
        Rows = 4;
        Columns = 2;
        PaperSize = new Size(8.5, 11);
        LabelSize = new Size(3.75, 2); 
        Gutter = new Gutter(.25, .25);
        Margin = new Thickness(.375, 1.125, .375, 1.125);
    }
}&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The library comes with a form that will display the labels and handle printing them. All you need to do is create your list of labels. In this example, ShippingLabel is a label that I made. Next, pass the labels to LabelSheet.CreateDocument. The resulting FixedDocument can be passed right to the document viewer:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d908fe13-6e2a-44fc-b13c-350e3dde1482" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;LabelViewer viewer = new LabelViewer();
List&amp;lt;ShippingLabel&amp;gt; labels = new List&amp;lt;ShippingLabel&amp;gt;();
Avery5163 avery = new Avery5163();

for(int i = 0; i &amp;lt; avery.LabelsPerSheet; i++)
{
    ShippingLabel label =
    new ShippingLabel
    {
        Return1 = { Text = "J.P. Hamilton" },
        Return2 = { Text = "1313 Mockingbird Ln." },
        Return3 = { Text = "Los Angeles, CA 90210" },
        Line1 = { Text = "Microsoft" },
        Line2 = { Text = "c\\o Bill Gates" },
        Line3 = { Text = "1 Microsoft Way" },
        Line4 = { Text = "Redmond, WA 98052" }
    };

    labels.Add(label);
    
}

viewer.Document = avery.CreateDocument(labels); 
viewer.Show();&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The end result looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.jphamilton.net/image.axd?picture=WindowsLiveWriter/WPFMakesPrintingLabelsEasy/76363F94/viewer.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="viewer" border="0" alt="viewer" src="http://www.jphamilton.net/image.axd?picture=WindowsLiveWriter/WPFMakesPrintingLabelsEasy/7AE4222E/viewer_thumb.jpg" width="644" height="431" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I am not going to go over the technical aspects. That’s all in the code. Have a look at the LabelSheet class to see how it’s all done.&lt;/p&gt;

&lt;p&gt;Lastly, one thing to remember when you print. Make sure that you have all auto-sizing features turned off. You will want to print the output at 100%.&lt;/p&gt;</description>
      <link>http://jphamilton.net/post/WPF-Makes-Printing-Labels-Easy.aspx</link>
      <author>jp</author>
      <comments>http://jphamilton.net/post/WPF-Makes-Printing-Labels-Easy.aspx#comment</comments>
      <guid>http://jphamilton.net/post.aspx?id=5747935a-4793-4400-83a8-c682fedb4533</guid>
      <pubDate>Wed, 23 Sep 2009 03:07:46 -0800</pubDate>
      <dc:publisher>jp</dc:publisher>
      <pingback:server>http://jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://jphamilton.net/post.aspx?id=5747935a-4793-4400-83a8-c682fedb4533</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://jphamilton.net/trackback.axd?id=5747935a-4793-4400-83a8-c682fedb4533</trackback:ping>
      <wfw:comment>http://jphamilton.net/post/WPF-Makes-Printing-Labels-Easy.aspx#comment</wfw:comment>
      <wfw:commentRss>http://jphamilton.net/syndication.axd?post=5747935a-4793-4400-83a8-c682fedb4533</wfw:commentRss>
    </item>
    <item>
      <title>Printing Labels in C#</title>
      <description>&lt;p&gt;&lt;a href="http://code.google.com/p/labelsharp/"&gt;Download Here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;(&lt;em&gt;this is another entry in my POC series – proof of concept, piece of crap…you decide&lt;/em&gt;)&lt;/p&gt;  &lt;p&gt;In my current application, I need the ability to print labels – and I need something flexible, quick, and easy. I started down the path of using interop with Word and quickly decided that this approach completely sucks. It’s not flexible. It’s not quick. And it’s certainly not easy. What I settled on was creating the labels in a PDF and then printing from there. To handle PDF creation, I am using a really great library called &lt;a href="http://www.pdfsharp.net"&gt;PDFsharp&lt;/a&gt;. It’s API is similar to doing graphics with GDI+, so it took me no time at all to get started. &lt;/p&gt;  &lt;p&gt;Now for the basics. I have a base class that allows me to define margins, gutters and other label-specific stuff:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:7b351f7f-a992-4894-a3c0-ccd608dbb3a9" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;/// &amp;lt;summary&amp;gt;
/// One or more sheets of labels
/// &amp;lt;/summary&amp;gt;
public abstract class LabelSheet 
{
    public int Rows { get; set; }
    public int Columns { get; set; }
    public LabelSize Size { get; private set; }
    public LabelMargin Margin { get; private set; }
    public LabelGutter Gutter { get; private set; }
    public IList&amp;lt;LabelBase&amp;gt; Labels { get; private set; }

    protected LabelSheet()
    {
        Labels = new List&amp;lt;LabelBase&amp;gt;();
        Size = new LabelSize();
        Margin = new LabelMargin();
        Gutter = new LabelGutter();
    }

    public void Print(ILabelPrintingStrategy printer)
    {
        printer.Print(this);
    }

    public void Print(ILabelPrintingStrategy printer, int startWithLabel)
    {
        printer.Print(this, startWithLabel);
    }

    public void Add(LabelBase label)
    {
        Labels.Add(label);
    }

    public void Add(LabelBase label, int count)
    {
        while (count-- &amp;gt; 0)
            Add(label);
    }

    public int Count
    {
        get { return Rows*Columns; }
    }
}&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I found a site called WorldLabel.Com that has label information for every type of label you could ever want. So, armed with this, I can create specific sheets of labels like so:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:4c5f4649-81c6-4430-9f48-ab8362d5571b" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;/// &amp;lt;summary&amp;gt;
/// A sheet of Avery 5160 labels
/// &amp;lt;/summary&amp;gt;
public class Avery5160Labels : LabelSheet
{
    public Avery5160Labels()
    {
        Columns = 3;
        Rows = 10;

        Size.Length = 2.5935;
        Size.Height = 1;

        Margin.Top = 0.5;
        Margin.Bottom = 0.5;
        Margin.Left = .21975;
        Margin.Right = .21975;

        Gutter.Horizontal = 0.14;
        Gutter.Vertical = 0;
    }
 
}&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;One thing to note is that the class is currently using inches, because that’s how us Americans are used to dealing with labels. It should not take any effort at all to make this more flexible. I did not do so, because I do not have requirements for that yet, and I don’t feel like starting an open source project for printing labels.&lt;/p&gt;

&lt;p&gt;Another thing to note is that LabelSheet has a method called Print that takes an ILabelPrintingStrategy. This interface defines two methods:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:e42fa9c8-d101-4bbb-9a5f-72b73506f1a4" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c"&gt;void Print(LabelSheet sheet);
void Print(LabelSheet sheet, int startWithLabel);&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;If you download the code, you will see a single strategy called PdfLabelPrintingStrategy. This class handles the grunt work of laying out the labels and writing them out to a PDF. The second overload of Print allows you to specify a starting label. So, if you have a half sheet of labels, you can start printing from the 15th label. In the demo code, I put together a quick and dirty UI that allows you to pick the starting label. I am sure you could find plenty of ways to improve this code, and I am sure that I will improve this code in the future. But this is what I have for now. &lt;/p&gt;</description>
      <link>http://jphamilton.net/post/Printing-Labels-in-C.aspx</link>
      <author>jp</author>
      <comments>http://jphamilton.net/post/Printing-Labels-in-C.aspx#comment</comments>
      <guid>http://jphamilton.net/post.aspx?id=086139cf-cb6b-454d-86b2-cb1fad94c5d8</guid>
      <pubDate>Mon, 14 Sep 2009 06:18:29 -0800</pubDate>
      <dc:publisher>jp</dc:publisher>
      <pingback:server>http://jphamilton.net/pingback.axd</pingback:server>
      <pingback:target>http://jphamilton.net/post.aspx?id=086139cf-cb6b-454d-86b2-cb1fad94c5d8</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://jphamilton.net/trackback.axd?id=086139cf-cb6b-454d-86b2-cb1fad94c5d8</trackback:ping>
      <wfw:comment>http://jphamilton.net/post/Printing-Labels-in-C.aspx#comment</wfw:comment>
      <wfw:commentRss>http://jphamilton.net/syndication.axd?post=086139cf-cb6b-454d-86b2-cb1fad94c5d8</wfw:commentRss>
    </item>
  </channel>
</rss>
