1
///////////////////////////////////////////////////////////////////////////////
3
// Purpose: Example of label printing
4
// Author: Ulrich Telle
7
// Copyright: (c) Ulrich Telle
8
// Licence: wxWindows licence
9
///////////////////////////////////////////////////////////////////////////////
11
// For compilers that support precompilation, includes "wx/wx.h".
12
#include "wx/wxprec.h"
22
#include "wx/pdfdoc.h"
27
* This example demonstrates the PDF label printing.
29
* The code is based on the PHP script PDF_Label of Laurent PASSEBECQ.
31
* Following are the comments from the original PHP code:
33
* ////////////////////////////////////////////////////
36
* // Class to print labels in Avery or custom formats
38
* // Copyright (C) 2003 Laurent PASSEBECQ (LPA)
39
* // Based on code by Steve Dillon : steved@mad.scientist.com
41
* //-------------------------------------------------------------------
43
* // 1.0 : Initial release
44
* // 1.1 : + : Added unit in the constructor
45
* // + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
46
* // + : Added in the description of a label :
47
* // font-size : defaut char size (can be changed by calling Set_Char_Size(xx);
48
* // paper-size : Size of the paper for this sheet (thanx to Al Canton)
49
* // metric : type of unit used in this description
50
* // You can define your label properties in inches by setting metric to 'in'
51
* // and printing in millimiter by setting unit to 'mm' in constructor.
52
* // Added some labels :
53
* // 5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
54
* // 8600 : thanx to Kunal Walia : kunal@u.washington.edu
55
* // + : Added 3mm to the position of labels to avoid errors
56
* // 1.2 : + : Added Set_Font_Name method
57
* // = : Bug of positioning
58
* // = : Set_Font_Size modified -> Now, just modify the size of the font
59
* // = : Set_Char_Size renamed to Set_Font_Size
60
* ////////////////////////////////////////////////////
63
typedef struct tPdfLabelFormat
66
wxPaperSize paperSize;
79
static PdfLabelFormat s_averyLabels[] =
81
{ wxT("5160"), wxPAPER_LETTER, wxT("mm"), 1.762, 10.7, 3, 10, 3.175, 0., 66.675, 25.4, 8 },
82
{ wxT("5161"), wxPAPER_LETTER, wxT("mm"), 0.967, 10.7, 2, 10, 3.967, 0., 101.6, 25.4, 8 },
83
{ wxT("5162"), wxPAPER_LETTER, wxT("mm"), 0.97, 20.224, 2, 7, 4.762, 0., 100.807, 35.72, 8 },
84
{ wxT("5163"), wxPAPER_LETTER, wxT("mm"), 1.762, 10.7, 2, 5, 3.175, 0., 101.6, 50.8, 8 },
85
{ wxT("5164"), wxPAPER_LETTER, wxT("in"), 0.148, 0.5, 2, 3, 0.2031, 0., 4.0, 3.33, 12 },
86
{ wxT("8600"), wxPAPER_LETTER, wxT("mm"), 7.1, 19, 3, 10, 9.5, 3.1, 66.6, 25.4, 8 },
87
{ wxT("L7163"), wxPAPER_A4, wxT("mm"), 5.0, 15, 2, 7, 25.0, 0., 99.1, 38.1, 9 },
88
// Last sentinel entry (do not delete)
89
{ wxT(""), wxPAPER_NONE, wxT("mm"), 0.0, 0, 0, 0, 0.0, 0., 0.0, 0.0, 0 },
92
class PdfLabel : public wxPdfDocument
96
PdfLabel (const PdfLabelFormat& format, const wxString& unit = wxT("mm"), int posX = 1, int posY = 1)
97
: wxPdfDocument(wxPORTRAIT, format.metric, format.paperSize)
105
SetFontName(wxT("Helvetica"));
107
SetAutoPageBreak(false);
110
// Start at the given label position
111
m_xCount = (posX >= m_xNumber) ? m_xNumber-1 : ((posX > 1) ? posX-1 : 0);
112
m_yCount = (posY >= m_yNumber) ? m_yNumber-1 : ((posY > 1) ? posY-1 : 0);
116
void AddLabel(const wxString& texte)
118
// We are in a new page, then we must add a page
119
if ((m_xCount == 0) && (m_yCount == 0))
124
double posX = m_marginLeft+(m_xCount*(m_width+m_xSpace));
125
double posY = m_marginTop+(m_yCount*(m_height+m_ySpace));
126
SetXY(posX+3, posY+3);
127
MultiCell(m_width, m_lineHeight, texte);
131
if (m_yCount == m_yNumber)
133
// End of column reached, we start a new one
138
if (m_xCount == m_xNumber)
140
// Page full, we start a new one
147
void SetFontName(const wxString& fontname)
149
if (fontname != wxT(""))
151
m_fontName = fontname;
156
/// Sets the font size
158
* This changes the line height too
160
void SetFontSize(double pt)
165
m_lineHeight = GetHeightChars(pt);
166
wxPdfDocument::SetFontSize(m_charSize);
170
/// Finds a predefined label format by name
171
static const PdfLabelFormat& FindFormat(const wxString& format)
173
PdfLabelFormat* tFormat = &s_averyLabels[0];
175
for (j = 0; wxString(wxT("")).Cmp(s_averyLabels[j].name) != 0; j++)
177
if (format == s_averyLabels[j].name)
179
tFormat = &s_averyLabels[j];
187
/// Convert units (in to mm, mm to in)
189
* src and dest must be 'in' or 'mm'
191
double ConvertMetric (double value, const wxString& src, const wxString& dest)
193
double retValue = value;
194
if (src == wxT("in") && dest == wxT("mm"))
196
retValue *= (1000./ 39.37008);
198
else if (src == wxT("mm") && dest == wxT("in"))
200
retValue *= (39.37008 / 1000.);
205
/// Gives the height for a char size given.
206
double GetHeightChars(double pt)
209
// Array matching character sizes and line heights
210
static const int nTable = 10;
211
static const double cTable[nTable] = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
212
static const double hTable[nTable] = { 2, 2.5, 3, 4, 5, 6, 7, 8, 9, 10 };
214
for (i = 0; i < nTable; i++)
225
/// Sets the label format
226
void SetFormat(const PdfLabelFormat& format)
228
m_metric = format.metric;
229
m_averyName = format.name;
230
m_marginLeft = ConvertMetric(format.marginLeft, m_metric, m_metricDoc);
231
m_marginTop = ConvertMetric(format.marginTop, m_metric, m_metricDoc);
232
m_xSpace = ConvertMetric(format.xSpace, m_metric, m_metricDoc);
233
m_ySpace = ConvertMetric(format.ySpace, m_metric, m_metricDoc);
234
m_xNumber = format.nx;
235
m_yNumber = format.ny;
236
m_width = ConvertMetric(format.width, m_metric, m_metricDoc);
237
m_height = ConvertMetric(format.height, m_metric, m_metricDoc);
238
SetFontSize(format.fontSize);
242
wxString m_averyName; //< Name of format
243
double m_marginLeft; //< Left margin of labels
244
double m_marginTop; //< Top margin of labels
245
double m_xSpace; //< Horizontal space between 2 labels
246
double m_ySpace; //< Vertical space between 2 labels
247
int m_xNumber; //< Number of labels horizontally
248
int m_yNumber; //< Number of labels vertically
249
double m_width; //< Width of label
250
double m_height; //< Height of label
251
double m_charSize; //< Character size
252
double m_lineHeight; //< Default line height
253
wxString m_metric; //< Type of metric for labels.. Will help to calculate good values
254
wxString m_metricDoc; //< Type of metric for the document
255
wxString m_fontName; //< Name of the font
264
// To create the object, 2 possibilities:
265
// either pass a custom format via an array
266
// or use a built-in AVERY name
268
// Example of custom format; we start at the second column
269
// PdfLabelFormat perso1 = { wxT("perso1"), wxPAPER_A4, wxT("mm"), 1., 1., 2, 7, 0., 0., 99.1, 38.1, 14 },
270
// PdfLabel pdf(perso1, wxT("mm"), 1, 2);
273
PdfLabel pdf(PdfLabel::FindFormat(wxT("L7163")), wxT("mm"), 1, 2);
278
for (i = 1; i <= 40; i++)
280
pdf.AddLabel(wxString::Format(wxT("Laurent %d\nImmeuble Titi\nav. fragonard\n06000, NICE, FRANCE"), i));
282
pdf.SaveAsFile(wxT("labels.pdf"));