Пример #1
0
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw barcode
        /// </summary>
        /// <param name="PosX">Position X</param>
        /// <param name="PosY">Position Y</param>
        /// <param name="Justify">Barcode justify (using TextJustify enumeration)</param>
        /// <param name="BarWidth">Narrow bar width</param>
        /// <param name="BarHeight">Barcode height</param>
        /// <param name="BarColor">Barcode color</param>
        /// <param name="Barcode">Derived barcode class</param>
        /// <param name="TextFont">Text font</param>
        /// <param name="FontSize">Text font size</param>
        /// <returns>Barcode width</returns>
        /// <remarks>
        /// <para>
        /// PosX can be the left, centre or right side of the barcode.
        /// The Justify argument controls the meaning of PosX.
        /// PosY is the position of the bottom side of the barcode. 
        /// If optional text is displayed it will be
        /// displayed below PosY. If optional text is wider than the
        /// barcode it will be extended to the left and right sides
        /// of the barcode.
        /// </para>
        /// <para>
        /// The BarWidth argument is the width of the narrow bar.
        /// </para>
        /// <para>
        /// The BarcodeHeight argument is the height of the barcode 
        /// excluding optional text.
        /// </para>
        /// <para>
        /// Set Barcode to one of the derived classes. 
        /// This library supports: Barcode128, Barcode39 and BarcodeEAN13.
        /// Note BarcodeEAN13 supports Barcode UPC-A.
        /// </para>
        /// <para>
        /// Barcode text is optional. If TextFont and FontSize are omitted 
        /// no text will be drawn under the barcode. If TextFont and
        /// FontSize are specified the barcode text will be displayed
        /// under the barcode. It will be horizontally centered in relation
        /// to the barcode.
        /// </para>
        /// <para>
        /// Barcode text is displayed below PosY. Make sure to leave
        /// space under the barcode.
        /// </para>
        /// <para>
        /// If color other than black is given make sure there is
        /// a good contrast to white.
        /// </para>
        /// </remarks>
        ////////////////////////////////////////////////////////////////////
        public Double DrawBarcode(
			Double		PosX,
			Double		PosY,
			TextJustify	Justify,
			Double		BarWidth,
			Double		BarHeight,
			Color		BarColor,
			Barcode		Barcode,
			PdfFont		TextFont = null,
			Double		FontSize = 0.0
			)
        {
            // save graphics state
            SaveGraphicsState();

            // set barcode color
            SetColorNonStroking(BarColor);

            // barcode width
            Double TotalWidth = BarWidth * Barcode.TotalWidth;

            // switch for adjustment
            switch(Justify)
            {
            case TextJustify.Center:
                PosX -= 0.5 * TotalWidth;
                break;

            case TextJustify.Right:
                PosX -= TotalWidth;
                break;
            }

            // initial bar position
            Double BarPosX = PosX;

            // initialize bar color to black
            Boolean Bar = true;

            // all barcodes except EAN-13 or UPC-A
            if(Barcode.GetType() != typeof(BarcodeEAN13))
            {
            // loop for all bars
            for(Int32 Index = 0; Index < Barcode.BarCount; Index++)
                {
                // bar width in user units
                Double Width = BarWidth * Barcode.BarWidth(Index);

                // draw black bars
                if(Bar) DrawRectangle(BarPosX, PosY, Width, BarHeight, PaintOp.Fill);

                // update bar position and color
                BarPosX += Width;
                Bar = !Bar;
                }

            // display text if font is specified
            if(TextFont != null) DrawBarcodeText(TextFont, FontSize, PosX + 0.5 * TotalWidth, PosY, TextJustify.Center, Barcode.Text);
            }

            // EAN-13 or UPC-A
            else
            {
            // loop for all bars
            for(Int32 Index = 0; Index < Barcode.BarCount; Index++)
                {
                // bar width in user units
                Double Width = BarWidth * Barcode.BarWidth(Index);

                // adjust vertical position
                Double DeltaY = Index < 7 || Index >= 27 && Index < 32 || Index >= 52 ? 0.0 : 5 * BarWidth;

                // draw black bars
                if(Bar) DrawRectangle(BarPosX, PosY + DeltaY, Width, BarHeight - DeltaY, PaintOp.Fill);

                // update bar position and color
                BarPosX += Width;
                Bar = !Bar;
                }

            // display text if font is specified
            if(TextFont != null)
                {
                // substrings positions
                Double PosX1 = PosX - 2.0 * BarWidth;
                Double PosX2 = PosX + 27.5 * BarWidth;
                Double PosX3 = PosX + 67.5 * BarWidth;
                Double PosX4 = PosX + 97.0 * BarWidth;
                Double PosY1 = PosY + 5.0 * BarWidth;

                // UPC-A
                if(Barcode.Text.Length == 12)
                    {
                    DrawBarcodeText(TextFont, FontSize, PosX1, PosY1, TextJustify.Right, Barcode.Text.Substring(0, 1));
                    DrawBarcodeText(TextFont, FontSize, PosX2, PosY1, TextJustify.Center, Barcode.Text.Substring(1, 5));
                    DrawBarcodeText(TextFont, FontSize, PosX3, PosY1, TextJustify.Center, Barcode.Text.Substring(6, 5));
                    DrawBarcodeText(TextFont, FontSize, PosX4, PosY1, TextJustify.Left, Barcode.Text.Substring(11));
                    }
                // EAN-13
                else
                    {
                    DrawBarcodeText(TextFont, FontSize, PosX1, PosY1, TextJustify.Right, Barcode.Text.Substring(0, 1));
                    DrawBarcodeText(TextFont, FontSize, PosX2, PosY1, TextJustify.Center, Barcode.Text.Substring(1, 6));
                    DrawBarcodeText(TextFont, FontSize, PosX3, PosY1, TextJustify.Center, Barcode.Text.Substring(7));
                    }
                }
            }

            // restore graphics state
            RestoreGraphicsState();

            // return width
            return(TotalWidth);
        }
Пример #2
0
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw barcode
        /// </summary>
        /// <param name="PosX">Position X</param>
        /// <param name="PosY">Position Y</param>
        /// <param name="Justify">Barcode justify (using TextJustify enumeration)</param>
        /// <param name="BarWidth">Narrow bar width</param>
        /// <param name="BarcodeHeight">Barcode height</param>
        /// <param name="Barcode">Derived barcode class</param>
        /// <param name="TextFont">Text font</param>
        /// <param name="FontSize">Text font size</param>
        /// <returns>Barcode width</returns>
        /// <remarks>
        /// <para>
        /// PosX can be the left, centre or right side of the barcode.
        /// The Justify argument controls the meaning of PosX.
        /// PosY is the position of the bottom side of the barcode. 
        /// If optional text is displayed it will be
        /// displayed below PosY. If optional text is wider than the
        /// barcode it will be extended to the left and right sides
        /// of the barcode.
        /// </para>
        /// <para>
        /// The BarWidth argument is the width of the narrow bar.
        /// </para>
        /// <para>
        /// The BarcodeHeight argument is the height of the barcode 
        /// excluding optional text.
        /// </para>
        /// <para>
        /// Set Barcode to one of the derived classes. 
        /// This library supports: Barcode128, Barcode39 and BarcodeEAN13.
        /// Note BarcodeEAN13 supports Barcode UPC-A.
        /// </para>
        /// <para>
        /// Barcode text is optional. If TextFont and FontSize are omitted 
        /// no text will be drawn under the barcode. If TextFont and
        /// FontSize are specified the barcode text will be displayed
        /// under the barcode. It will be horizontally centered in relation
        /// to the barcode.
        /// </para>
        /// <para>
        /// Barcode text is displayed below PosY. Make sure to leave
        /// space under the barcode.
        /// </para>
        /// </remarks>
        ////////////////////////////////////////////////////////////////////
        public Double DrawBarcode(
			Double		PosX,
			Double		PosY,
			TextJustify	Justify,
			Double		BarWidth,
			Double		BarcodeHeight,
			Barcode		Barcode,
			PdfFont		TextFont = null,
			Double		FontSize = 0.0
			)
        {
            return(DrawBarcode(PosX, PosY, Justify, BarWidth, BarcodeHeight, Color.Black, Barcode, TextFont, FontSize));
        }