Exemplo n.º 1
0
        private void DrawLineOfText(
            Page page, List <TextLine> list, bool draw)
        {
            if (alignment == Align.JUSTIFY)
            {
                float sum_of_word_widths = 0f;
                for (int i = 0; i < list.Count; i++)
                {
                    TextLine text = list[i];
                    sum_of_word_widths += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                float dx = (w - sum_of_word_widths) / (list.Count - 1);
                for (int i = 0; i < list.Count; i++)
                {
                    TextLine text = list[i];
                    text.SetPosition(x1, y1);

                    if (text.GetGoToAction() != null)
                    {
                        page.AddAnnotation(new Annotation(
                                               null,                 // The URI
                                               text.GetGoToAction(), // The destination name
                                               x,
                                               page.height - (y - text.font.ascent),
                                               x + text.font.StringWidth(text.GetFallbackFont(), text.GetText()),
                                               page.height - (y - text.font.descent),
                                               null,
                                               null,
                                               null));
                    }

                    if (rotate == 0)
                    {
                        text.SetTextDirection(0);
                        text.DrawOn(page, draw);
                        x1 += text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                    else if (rotate == 90)
                    {
                        text.SetTextDirection(90);
                        text.DrawOn(page, draw);
                        y1 -= text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                    else if (rotate == 270)
                    {
                        text.SetTextDirection(270);
                        text.DrawOn(page, draw);
                        y1 += text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                }
            }
            else
            {
                DrawNonJustifiedLine(page, list, draw);
            }
        }
Exemplo n.º 2
0
        private void DrawNonJustifiedLine(
            Page page, List <TextLine> list, bool draw)
        {
            float run_length = 0f;

            for (int i = 0; i < list.Count; i++)
            {
                TextLine text = list[i];
                if (i < (list.Count - 1))
                {
                    text.str += " ";
                }
                run_length += text.font.StringWidth(text.GetFallbackFont(), text.str);
            }

            if (alignment == Align.CENTER)
            {
                if (rotate == 0)
                {
                    x1 = x + ((w - run_length) / 2);
                }
                else if (rotate == 90)
                {
                    y1 = y - ((w - run_length) / 2);
                }
                else if (rotate == 270)
                {
                    y1 = y + ((w - run_length) / 2);
                }
            }
            else if (alignment == Align.RIGHT)
            {
                if (rotate == 0)
                {
                    x1 = x + (w - run_length);
                }
                else if (rotate == 90)
                {
                    y1 = y - (w - run_length);
                }
                else if (rotate == 270)
                {
                    y1 = y + (w - run_length);
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                TextLine text = list[i];
                text.SetPosition(x1, y1);

                if (text.GetGoToAction() != null)
                {
                    page.AddAnnotation(new Annotation(
                                           null,                 // The URI
                                           text.GetGoToAction(), // The destination name
                                           x,
                                           page.height - (y - text.font.ascent),
                                           x + text.font.StringWidth(text.GetFallbackFont(), text.GetText()),
                                           page.height - (y - text.font.descent),
                                           null,
                                           null,
                                           null));
                }

                if (rotate == 0)
                {
                    text.SetTextDirection(0);
                    text.DrawOn(page, draw);
                    x1 += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                else if (rotate == 90)
                {
                    text.SetTextDirection(90);
                    text.DrawOn(page, draw);
                    y1 -= text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                else if (rotate == 270)
                {
                    text.SetTextDirection(270);
                    text.DrawOn(page, draw);
                    y1 += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
            }
        }
Exemplo n.º 3
0
        private float[] DrawCode39(Page page, float x1, float y1)
        {
            text = "*" + text + "*";

            float x = x1;
            float y = y1;
            float w = m1 * barHeightFactor; // Barcode width when drawn vertically
            float h = m1 * barHeightFactor; // Barcode height when drawn horizontally

            float[] xy = new float[] { 0f, 0f };

            if (direction == LEFT_TO_RIGHT)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            x += m1;
                        }
                        else if (ch == 'W')
                        {
                            x += m1 * 3;
                        }
                        else if (ch == 'b')
                        {
                            DrawVertBar(page, x, y, m1, h);
                            x += m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawVertBar(page, x, y, m1 * 3, h);
                            x += m1 * 3;
                        }
                    }

                    x += m1;
                }

                if (font != null)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x1 + ((x - x1) - font.StringWidth(text)) / 2,
                        y1 + h + font.bodyHeight);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]);
                }
            }
            else if (direction == TOP_TO_BOTTOM)
            {
                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            y += m1;
                        }
                        else if (ch == 'W')
                        {
                            y += 3 * m1;
                        }
                        else if (ch == 'b')
                        {
                            DrawHorzBar(page, x, y, m1, h);
                            y += m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawHorzBar(page, x, y, 3 * m1, h);
                            y += 3 * m1;
                        }
                    }

                    y += m1;
                }

                if (font != null)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x - font.bodyHeight,
                        y1 + ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(270);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]) + w;
                    xy[1] = Math.Max(y, xy[1]);
                }
            }
            else if (direction == BOTTOM_TO_TOP)
            {
                float height = 0.0f;

                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    if (code == null)
                    {
                        throw new Exception("The input string '" + text +
                                            "' contains characters that are invalid in a Code39 barcode.");
                    }

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w' || ch == 'b')
                        {
                            height += m1;
                        }
                        else if (ch == 'W' || ch == 'B')
                        {
                            height += 3 * m1;
                        }
                    }

                    height += m1;
                }

                y += height - m1;

                for (int i = 0; i < text.Length; i++)
                {
                    String code = tableB[text[i]];

                    for (int j = 0; j < 9; j++)
                    {
                        char ch = code[j];
                        if (ch == 'w')
                        {
                            y -= m1;
                        }
                        else if (ch == 'W')
                        {
                            y -= 3 * m1;
                        }
                        else if (ch == 'b')
                        {
                            DrawHorzBar2(page, x, y, m1, h);
                            y -= m1;
                        }
                        else if (ch == 'B')
                        {
                            DrawHorzBar2(page, x, y, 3 * m1, h);
                            y -= 3 * m1;
                        }
                    }

                    y -= m1;
                }

                if (font != null)
                {
                    y = y1 + (height - m1);

                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x + w + font.bodyHeight,
                        y - ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(90);
                    xy    = textLine.DrawOn(page);
                    xy[1] = Math.Max(y, xy[1]);
                    return(new float[] { xy[0], xy[1] + font.descent });
                }
            }

            return(new float[] { xy[0], xy[1] });
        }
Exemplo n.º 4
0
    public Example_05()
    {
        FileStream fos = new FileStream("Example_05.pdf", FileMode.Create);
        BufferedStream bos = new BufferedStream(fos);

        PDF pdf = new PDF(bos);
        pdf.setCompressor(Compressor.ORIGINAL_ZLIB);

        // Before you enable this flag please read README.ZLIB.TXT
        // in the 'optional' directory.

        // If PDF/A is not required use Helvetica, TimesRoman or Courier
        Font f1 = new Font(pdf, "Helvetica");

        Page page = new Page(pdf, Letter.PORTRAIT);

        TextLine text = new TextLine(f1);
        text.SetPosition(300.0, 300.0);
        for (int i = 0; i < 360; i += 15) {
            text.SetTextDirection(i);
            text.SetUnderline(true);
            // text.SetStrikeLine(true);
            text.SetText("             Hello, World -- " + i + " degrees.");
            text.DrawOn(page);
        }

        text = new TextLine(f1, "WAVE AWAY");
        text.SetPosition(70.0, 50.0);
        text.DrawOn(page);

        f1.SetKernPairs(true);
        text.SetPosition(70.0, 70.0);
        text.DrawOn(page);

        f1.SetKernPairs(false);
        text.SetPosition(70.0, 90.0);
        text.DrawOn(page);

        f1.SetSize(8);
        text = new TextLine(f1, "-- font.SetKernPairs(false);");
        text.SetPosition(150.0, 50.0);
        text.DrawOn(page);
        text.SetPosition(150.0, 90.0);
        text.DrawOn(page);
        text = new TextLine(f1, "-- font.SetKernPairs(true);");
        text.SetPosition(150.0, 70.0);
        text.DrawOn(page);

        Point point = new Point(300.0, 300.0);
        point.SetShape(Point.CIRCLE);
        point.SetFillShape(true);
        point.SetColor(RGB.BLUE);
        point.SetRadius(37.0);
        point.DrawOn(page);
        point.SetRadius(25.0);
        point.SetColor(RGB.WHITE);
        point.DrawOn(page);

        pdf.Flush();
        bos.Close();
    }
Exemplo n.º 5
0
        private float[] DrawCode128(Page page, float x1, float y1)
        {
            float x = x1;
            float y = y1;
            float w = m1;
            float h = m1;

            if (direction == TOP_TO_BOTTOM)
            {
                w *= barHeightFactor;
            }
            else if (direction == LEFT_TO_RIGHT)
            {
                h *= barHeightFactor;
            }

            List <Int32> list = new List <Int32>();

            for (int i = 0; i < text.Length; i++)
            {
                char symchar = text[i];
                if (symchar < 32)
                {
                    list.Add(GS1_128.SHIFT);
                    list.Add(symchar + 64);
                }
                else if (symchar < 128)
                {
                    list.Add(symchar - 32);
                }
                else if (symchar < 256)
                {
                    list.Add(GS1_128.FNC_4);
                    list.Add(symchar - 160); // 128 + 32
                }
                else
                {
                    // list.Add(31);            // '?'
                    list.Add(256);          // This will generate an exception.
                }
                if (list.Count == 48)
                {
                    // Maximum number of data characters is 48
                    break;
                }
            }

            StringBuilder buf        = new StringBuilder();
            int           checkDigit = GS1_128.START_B;

            buf.Append((char)checkDigit);
            for (int i = 0; i < list.Count; i++)
            {
                int codeword = list[i];
                buf.Append((char)codeword);
                checkDigit += codeword * (i + 1);
            }
            checkDigit %= GS1_128.START_A;
            buf.Append((char)checkDigit);
            buf.Append((char)GS1_128.STOP);

            for (int i = 0; i < buf.Length; i++)
            {
                int    si     = buf[i];
                String symbol = GS1_128.TABLE[si].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 == 0)
                    {
                        if (direction == LEFT_TO_RIGHT)
                        {
                            DrawVertBar(page, x, y, m1 * n, h);
                        }
                        else if (direction == TOP_TO_BOTTOM)
                        {
                            DrawHorzBar(page, x, y, m1 * n, w);
                        }
                    }
                    if (direction == LEFT_TO_RIGHT)
                    {
                        x += n * m1;
                    }
                    else if (direction == TOP_TO_BOTTOM)
                    {
                        y += n * m1;
                    }
                }
            }

            float[] xy = new float[] { x, y };
            if (font != null)
            {
                if (direction == LEFT_TO_RIGHT)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x1 + ((x - x1) - font.StringWidth(text)) / 2,
                        y1 + h + font.bodyHeight);
                    xy    = textLine.DrawOn(page);
                    xy[0] = Math.Max(x, xy[0]);
                    return(new float[] { xy[0], xy[1] + font.descent });
                }
                else if (direction == TOP_TO_BOTTOM)
                {
                    TextLine textLine = new TextLine(font, text);
                    textLine.SetLocation(
                        x + w + font.bodyHeight,
                        y - ((y - y1) - font.StringWidth(text)) / 2);
                    textLine.SetTextDirection(90);
                    xy    = textLine.DrawOn(page);
                    xy[1] = Math.Max(y, xy[1]);
                }
            }

            return(xy);
        }