Example #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);
            }
        }
Example #2
0
        private TextLine DrawLineOnPage(TextLine textLine, Page page)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();

            String[] tokens     = Regex.Split(textLine.GetText(), @"\s+");
            bool     testForFit = true;

            for (int i = 0; i < tokens.Length; i++)
            {
                String token = tokens[i] + Single.space;
                if (testForFit && textLine.GetStringWidth((sb1.ToString() + token).Trim()) < this.w)
                {
                    sb1.Append(token);
                }
                else
                {
                    if (testForFit)
                    {
                        testForFit = false;
                    }
                    sb2.Append(token);
                }
            }
            textLine.SetText(sb1.ToString().Trim());
            if (page != null)
            {
                textLine.DrawOn(page);
            }

            textLine.SetText(sb2.ToString().Trim());
            return(textLine);
        }
Example #3
0
 public void DrawOn(Page page)
 {
     if (!prefix.Equals(""))
     {
         prefix.DrawOn(page);
     }
     text.DrawOn(page);
 }
Example #4
0
 public float[] DrawOn(Page page)
 {
     if (!prefix.Equals(""))
     {
         prefix.DrawOn(page);
     }
     return(textLine.DrawOn(page));
 }
    public Example_04()
    {
        String fileName = "data/happy-new-year.txt";

        FileStream fos = new FileStream("Example_04.pdf", FileMode.Create);
        BufferedStream bos = new BufferedStream(fos);

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

        Font f1 = new Font(
                pdf,
                "AdobeMingStd-Light",       // Chinese (Traditional) font
                CodePage.UNICODE);
        Font f2 = new Font(
                pdf,
                "AdobeSongStd-Light",       // Chinese (Simplified) font
                CodePage.UNICODE);
        Font f3 = new Font(
                pdf,
                "KozMinProVI-Regular",      // Japanese font
                CodePage.UNICODE);
        Font f4 = new Font(
                pdf,
                "AdobeMyungjoStd-Medium",   // Korean font
                CodePage.UNICODE);

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

        f1.SetSize(14);
        f2.SetSize(14);
        f3.SetSize(14);
        f4.SetSize(14);

        double x_pos = 100.0;
        double y_pos = 20.0;
        StreamReader reader = new StreamReader(
                new FileStream(fileName, FileMode.Open));
        TextLine text = new TextLine(f1);
        String line = null;
        while ((line = reader.ReadLine()) != null) {
            if (line.IndexOf("Simplified") != -1) {
                text.SetFont(f2);
            } else if (line.IndexOf("Japanese") != -1) {
                text.SetFont(f3);
            } else if (line.IndexOf("Korean") != -1) {
                text.SetFont(f4);
            }
            text.SetText(line);
            text.SetPosition(x_pos, y_pos += 24);
            text.DrawOn(page);
        }
        reader.Close();

        pdf.Flush();
        bos.Close();
    }
Example #6
0
 //-------------------------------------------------------------------------------------------
 private void DrawText(PDFjet.NET.PDF pdf, PDFjet.NET.Page page, string text, int x, int y)
 {
     Font f1 = new Font(pdf, "Helvetica");
        f1.SetSize(10);
        PDFjet.NET.TextLine tLine = new TextLine(f1);
        tLine.SetText(text);
        tLine.SetPosition(x, y);
        //tLine.SetURIAction("http://www.weavver.com/accounting/");
        tLine.DrawOn(page);
 }
Example #7
0
        public float[] DrawOn(Page page)
        {
            for (int row = 0; row < 7; row++)
            {
                for (int col = 0; col < 7; col++)
                {
                    if (row == 0)
                    {
                        float    offset = (dx - f1.StringWidth(days[col])) / 2;
                        TextLine text   = new TextLine(f1, days[col]);
                        text.SetLocation(x1 + col * dx + offset, y1 + (dy / 2) - f1.descent);
                        text.DrawOn(page);
                        // Draw the line separating the title from the dates.
                        Line line = new Line(
                            x1,
                            y1 + dy / 2 + f1.descent,
                            x1 + 7 * dx,
                            y1 + dy / 2 + f1.descent);
                        line.DrawOn(page);
                    }
                    else
                    {
                        int dayOfMonth = ((7 * row + col) - 6) - dayOfWeek;
                        if (dayOfMonth > 0 && dayOfMonth <= daysInMonth)
                        {
                            String   s1     = dayOfMonth.ToString();
                            float    offset = (dx - f2.StringWidth(s1)) / 2;
                            TextLine text   = new TextLine(f2, s1);
                            text.SetLocation(x1 + col * dx + offset, y1 + row * dy + f2.ascent);
                            text.DrawOn(page);

                            page.SetPenWidth(1.5f);
                            page.SetPenColor(Color.blue);
                            page.DrawEllipse(
                                x1 + col * dx + dx / 2,
                                y1 + row * dy + f2.GetHeight() / 2,
                                dx / 2.5,
                                dy / 2.5);
                        }
                    }
                }
            }
            return(new float[] { this.x1 + 7 * this.dx, this.y1 + 7 * this.dy });
        }
Example #8
0
        public void DrawOn(Page page)
        {
            for (int row = 0; row < 7; row++)
            {
                for (int col = 0; col < 7; col++)
                {
                    if (row == 0)
                    {
                        float    offset = (dx - f1.StringWidth(days[col])) / 2;
                        TextLine text   = new TextLine(f1, days[col]);
                        text.SetLocation(x1 + col * dx + offset, x1 + row * dy);
                        text.DrawOn(page);
                        // Draw the line separating the title from the dates.
                        Line line = new Line(
                            x1,
                            y1 + dx / 4,
                            x1 + 7 * dx,
                            y1 + dx / 4);
                        line.SetWidth(0.5f);
                        line.DrawOn(page);
                    }
                    else
                    {
                        int day_of_month = ((7 * row + col) - 6) - day_of_week;
                        if (day_of_month > 0 && day_of_month <= days_in_month)
                        {
                            String   s1     = day_of_month.ToString();
                            float    offset = (dx - f2.StringWidth(s1)) / 2;
                            TextLine text   = new TextLine(f2, s1);
                            text.SetLocation(x1 + col * dx + offset, y1 + row * dy);
                            text.DrawOn(page);

                            page.SetPenWidth(1.5f);
                            page.SetPenColor(Color.blue);
                            page.DrawEllipse(x1 + col * dx + dx / 2, y1 + row * dy - dy / 5, 8f, 8f);
                        }
                    }
                }
            }
        }
Example #9
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);
                }
            }
        }
    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();
    }
        /// <summary>
        /// Populate a voter card with the information of a given voter.
        /// </summary>
        /// <param name="page">The page containing the card.</param>
        /// <param name="pdf">The pdf containing the page.</param>
        /// <param name="xO">The horizontal offset of the card in points.</param>
        /// <param name="yO">The vertical offset of the card in points.</param>
        /// <param name="voter">The voter whose information to be populated onto the card.</param>
        private static void PopulateCard(Page page, PDF pdf, double xO, double yO, VoterDO voter)
        {
            // ----- POPULATE: POLLING STATION -----
            PollingStationDO ps = voter.PollingStation;

            var font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(9);

            var t = new TextLine(font, ps.Name);
            t.SetPosition(xO + 9 * U, yO + 27.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, ps.Address);
            t.SetPosition(xO + 9 * U, yO + 32 * U);
            t.DrawOn(page);

            t = new TextLine(font, "valgfrit");
            t.SetPosition(xO + 29 * U, yO + 48.8 * U);
            t.DrawOn(page);

            t = new TextLine(font, "02-04861");
            t.SetPosition(xO + 29 * U, yO + 58.7 * U);
            t.DrawOn(page);

            t = new TextLine(font, "09:00 - 20:00");
            t.SetPosition(xO + 29 * U, yO + 68.2 * U);
            t.DrawOn(page);

            // ----- POPULATE: VOTER -----
            MunicipalityDO mun = voter.PollingStation.Municipality;

            font = new Font(pdf, CoreFont.COURIER);
            font.SetSize(10);

            // Add top voter number 'Vælgernr.'
            t = new TextLine(font, "02-04861");
            t.SetPosition(xO + 102 * U, yO + 12 * U);
            t.DrawOn(page);

            // Add sender 'Afsender'
            t = new TextLine(font, mun.Name);
            t.SetPosition(xO + 102 * U, yO + 32.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, mun.Address);
            t.SetPosition(xO + 102 * U, yO + 36.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, mun.City);
            t.SetPosition(xO + 102 * U, yO + 40.5 * U);
            t.DrawOn(page);

            // Add reciever 'Modtager'
            t = new TextLine(font, voter.Name);
            t.SetPosition(xO + 102 * U, yO + 62.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, voter.Address);
            t.SetPosition(xO + 102 * U, yO + 66.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, voter.City);
            t.SetPosition(xO + 102 * U, yO + 70.5 * U);
            t.DrawOn(page);

            // Add CPR barcode
            string barcode = BarCodeHashing.Hash(voter.PrimaryKey.Value).ToString();
            var b = new BarCode(BarCode.CODE128, barcode);
            b.SetPosition(xO + 160 * U, yO + 60 * U);
            b.DrawOn(page);

            t = new TextLine(font, barcode);
            t.SetPosition(xO + 160 * U, yO + 72 * U);
            t.DrawOn(page);
        }
        /// <summary>
        /// Generate lines, boxes, watermark and default text of a single voter card.
        /// </summary>
        /// <param name="page">The page containing the card.</param>
        /// <param name="pdf">The pdf containing the page.</param>
        /// <param name="xO">The horizontal offset of the card in points.</param>
        /// <param name="yO">The vertical offset of the card in points.</param>
        private static void GenerateCard(Page page, PDF pdf, double xO, double yO)
        {
            // Add watermark.
            var font = new Font(pdf, CoreFont.HELVETICA_BOLD);
            font.SetSize(55);
            var t = new TextLine(font, "FAKE VALGKORT");
            var lightBlue = new[] { 0.647, 0.812, 0.957 };
            t.SetColor(lightBlue);
            t.SetPosition(xO + 20 * U, yO + 50 * U);
            t.DrawOn(page);

            // Add 'Afstemningssted' box.
            var b = new Box(xO + 6 * U, yO + 20 * U, 80 * U, 22 * U);
            b.DrawOn(page);

            // Add 'Valgbord' box.
            b = new Box(xO + 6 * U, yO + 44.5 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add 'Vælgernr' box.
            b = new Box(xO + 6 * U, yO + 54 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add 'Afstemningstid' box.
            b = new Box(xO + 6 * U, yO + 63.5 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add lines.
            var l = new Line(xO + 96 * U, yO + 5 * U, xO + 96 * U, yO + 85 * U);
            l.DrawOn(page);
            l = new Line(xO + 96 * U, yO + 25 * U, xO + 205 * U, yO + 25 * U);
            l.DrawOn(page);
            l = new Line(xO + 96 * U, yO + 45 * U, xO + 205 * U, yO + 45 * U);
            l.DrawOn(page);
            l = new Line(xO + 6 * U, yO + 85 * U, xO + 205 * U, yO + 85 * U);
            l.DrawOn(page);

            // Add default text.
            font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(7);
            t = new TextLine(font, "Afstemningssted:");
            t.SetPosition(xO + 7 * U, yO + 23 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Vælgernr.:");
            t.SetPosition(xO + 7 * U, yO + 58 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Afstemningstid:");
            t.SetPosition(xO + 7 * U, yO + 68 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Afsender:");
            t.SetPosition(xO + 98 * U, yO + 29 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Modtager:");
            t.SetPosition(xO + 98 * U, yO + 49 * U);
            t.DrawOn(page);

            font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(9);
            t = new TextLine(font, "Folketingsvalg");
            t.SetPosition(xO + 7 * U, yO + 10 * U);
            t.DrawOn(page);
            t = new TextLine(font, "20. november 2001");
            t.SetPosition(xO + 7 * U, yO + 14 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Valgbord:");
            t.SetPosition(xO + 7 * U, yO + 49 * U);
            t.DrawOn(page);

            font = new Font(pdf, CoreFont.HELVETICA_OBLIQUE);
            font.SetSize(7);
            t = new TextLine(font, "Medbring kortet ved afstemningen");
            t.SetPosition(xO + 6.5 * U, yO + 18.5 * U);
            t.DrawOn(page);
        }
Example #13
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] });
        }
Example #14
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);
        }
Example #15
0
        private float[] DrawCodeUPC(Page page, float x1, float y1)
        {
            float x = x1;
            float y = y1;
            float h = m1 * barHeightFactor; // Barcode height when drawn horizontally

            // Calculate the check digit:
            // 1. Add the digits in the odd-numbered positions (first, third, fifth, etc.)
            // together and multiply by three.
            // 2. Add the digits in the even-numbered positions (second, fourth, sixth, etc.)
            // to the result.
            // 3. Subtract the result modulo 10 from ten.
            // 4. The answer modulo 10 is the check digit.
            int sum = 0;

            for (int i = 0; i < 11; i += 2)
            {
                sum += text[i] - 48;
            }
            sum *= 3;
            for (int i = 1; i < 11; i += 2)
            {
                sum += text[i] - 48;
            }
            int reminder   = sum % 10;
            int checkDigit = (10 - reminder) % 10;

            text += checkDigit.ToString();

            x = DrawEGuard(page, x, y, m1, h + 8);
            for (int i = 0; i < 6; i++)
            {
                int digit = text[i] - 0x30;
                // page.DrawString(digit.ToString(), x + 1, y + h + 12);
                String symbol = tableA[digit].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 != 0)
                    {
                        DrawVertBar(page, x, y, n * m1, h);
                    }
                    x += n * m1;
                }
            }
            x = DrawMGuard(page, x, y, m1, h + 8);
            for (int i = 6; i < 12; i++)
            {
                int digit = text[i] - 0x30;
                // page.DrawString(digit.ToString(), x + 1, y + h + 12);
                String symbol = tableA[digit].ToString();
                for (int j = 0; j < symbol.Length; j++)
                {
                    int n = symbol[j] - 0x30;
                    if (j % 2 == 0)
                    {
                        DrawVertBar(page, x, y, n * m1, h);
                    }
                    x += n * m1;
                }
            }
            x = DrawEGuard(page, x, y, m1, h + 8);

            float[] xy = new float[] { x, y };
            if (font != null)
            {
                String label =
                    text[0] +
                    "  " +
                    text[1] +
                    text[2] +
                    text[3] +
                    text[4] +
                    text[5] +
                    "   " +
                    text[6] +
                    text[7] +
                    text[8] +
                    text[9] +
                    text[10] +
                    "  " +
                    text[11];
                float fontSize = font.GetSize();
                font.SetSize(10f);

                TextLine textLine = new TextLine(font, label);
                textLine.SetLocation(
                    x1 + ((x - x1) - font.StringWidth(label)) / 2,
                    y1 + h + font.bodyHeight);
                xy    = textLine.DrawOn(page);
                xy[0] = Math.Max(x, xy[0]);
                xy[1] = Math.Max(y, xy[1]);

                font.SetSize(fontSize);
                return(new float[] { xy[0], xy[1] + font.descent });
            }

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

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

        Font f1 = new Font(pdf, "Helvetica-Bold");
        Font f2 = new Font(pdf, "Helvetica");
        f1.SetSize(7.0);
        f2.SetSize(7.0);

        List<List<Cell>> tableData = new List<List<Cell>>();
        StreamReader reader = new StreamReader(
                new FileStream("data/winter-2009.txt", FileMode.Open));
        String line;
        while (( line = reader.ReadLine()) != null) {
            List<Cell> row = new List<Cell>();
            String[] columns = line.Split(new Char[] {'|'});
            for ( int i = 0; i < columns.Length; i++ ) {
                row.Add(new Cell(f2, columns[i]));
            }
            tableData.Add(row);
        }
        reader.Close();

        Table table = new Table(f1, f2);
        table.SetData(tableData, Table.DATA_HAS_2_HEADER_ROWS);
        table.SetPosition(100.0, 50.0);
        table.setCellMargin(2.0);

        table.RemoveLineBetweenRows(0, 1);

        Cell cell3 = table.GetCellAt(1, 1);
        cell3.border.top = true;

        cell3 = table.GetCellAt(1, 2);
        cell3.border.top = true;

        SetFontForRow(table, 0, f1);
        SetFontForRow(table, 1, f1);

        table.AutoAdjustColumnWidths();

        List<Cell> column = table.GetColumn(7);
        for ( int i = 0; i < column.Count; i++ ) {
            Cell cell = column[i];
            cell.SetTextAlignment(Align.CENTER);
        }

        column = table.GetColumn(4);
        for ( int i = 2; i < column.Count; i++ ) {
            Cell cell = column[i];
            try {
                cell.SetTextAlignment(Align.CENTER);
                if ( Int32.Parse( cell.GetText()) > 40 ) {
                    cell.SetBgColor( new double[] { 0.0, 0.85, 0.0 } );
                } else {
                    cell.SetBgColor( new double[] { 1.0, 1.0, 0.0 } );
                }
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }

        Cell cell2 = table.GetCellAt(0, 1);
        cell2.SetColSpan( 2 );
        cell2.SetTextAlignment(Align.CENTER);

        SetBgColorForRow(table, 0, new double[] { 0.85, 0.85, 0.85 });
        SetBgColorForRow(table, 1, new double[] { 0.85, 0.85, 0.85 });

        table.SetColumnWidth(3, 10);
        blankOutColumn(table, 3);

        table.SetColumnWidth(8, 10);
        blankOutColumn(table, 8);

        Page page = new Page(pdf, Letter.PORTRAIT);
        int numOfPages = table.GetNumberOfPages(page);
        int pageNumber = 1;
        while (true) {
            table.DrawOn(page);

            TextLine text = new TextLine(f1);
            text.SetText("Page " + pageNumber++ + " of " + numOfPages);
            text.SetPosition(300.0, 780.0);
            text.DrawOn(page);

            if (!table.HasMoreData()) break;
            page = new Page(pdf, Letter.PORTRAIT);
        }

        pdf.Flush();
        bos.Close();
    }