Exemple #1
0
        /**
         * add a filled rectangle to the current stream
         *
         * @param x the x position of left edge in millipoints
         * @param y the y position of top edge in millipoints
         * @param w the width in millipoints
         * @param h the height in millipoints
         * @param fill the fill color/gradient
         * @param stroke the stroke color/gradient
         */

        private void AddRect(int x, int y, int w, int h,
                             PdfColor stroke,
                             PdfColor fill)
        {
            CloseText();
            currentStream.DrawAndFillRect(x, y, w, h, stroke, fill);
        }
Exemple #2
0
        /**
         * add a rectangle to the current stream
         *
         * @param x the x position of left edge in millipoints
         * @param y the y position of top edge in millipoints
         * @param w the width in millipoints
         * @param h the height in millipoints
         * @param stroke the stroke color/gradient
         */

        private void AddRect(int x, int y, int w, int h, PdfColor stroke)
        {
            CloseText();
            currentStream.Write("ET\nq\n" + stroke.getColorSpaceOut(false)
                                + PdfNumber.doubleOut(x / 1000f) + " " + PdfNumber.doubleOut(y / 1000f) + " "
                                + PdfNumber.doubleOut(w / 1000f) + " " + PdfNumber.doubleOut(h / 1000f) + " re s\n"
                                + "Q\nBT\n");
        }
Exemple #3
0
        /**
         * add a filled rectangle to the current stream
         *
         * @param x the x position of left edge in millipoints
         * @param y the y position of top edge in millipoints
         * @param w the width in millipoints
         * @param h the height in millipoints
         * @param fill the fill color/gradient
         */

        private void AddFilledRect(int x, int y, int w, int h,
                                   PdfColor fill)
        {
            CloseText();
            currentStream.Write("ET\nq\n" + fill.getColorSpaceOut(true)
                                + PdfNumber.doubleOut(x / 1000f) + " " + PdfNumber.doubleOut(y / 1000f) + " "
                                + PdfNumber.doubleOut(w / 1000f) + " " + PdfNumber.doubleOut(h / 1000f) + " re f\n"
                                + "Q\nBT\n");
        }
Exemple #4
0
        /**
         * add a line to the current stream
         *
         * @param x1 the start x location in millipoints
         * @param y1 the start y location in millipoints
         * @param x2 the end x location in millipoints
         * @param y2 the end y location in millipoints
         * @param th the thickness in millipoints
         * @param rs the rule style
         * @param r the red component
         * @param g the green component
         * @param b the blue component
         */

        private void AddLine(int x1, int y1, int x2, int y2, int th, int rs,
                             PdfColor stroke)
        {
            CloseText();
            currentStream.Write("ET\nq\n" + stroke.getColorSpaceOut(false)
                                + SetRuleStylePattern(rs) + PdfNumber.doubleOut(x1 / 1000f) + " "
                                + PdfNumber.doubleOut(y1 / 1000f) + " m " + PdfNumber.doubleOut(x2 / 1000f) + " "
                                + PdfNumber.doubleOut(y2 / 1000f) + " l " + PdfNumber.doubleOut(th / 1000f) + " w S\n"
                                + "Q\nBT\n");
        }
Exemple #5
0
        void AddLineThrough(int x, int y, int w, int lineH,
                            int fontAscender,
                            PdfColor theAreaColor)
        {
            int yPos = y + fontAscender * 3 / 8;

            AddLine(x, yPos, x + w, yPos, lineH / 14,
                    theAreaColor);
            prevLineThroughXEndPos = x + w;
            prevLineThroughYEndPos = yPos;
            prevLineThroughSize    = lineH / 14;
            prevLineThroughColor   = theAreaColor;
        }
Exemple #6
0
        void AddOverLine(int x, int y, int w, int lineH,
                         int fontAscender,
                         PdfColor theAreaColor)
        {
            int yPos = y + fontAscender + lineH / 10;

            AddLine(x, yPos, x + w, yPos, lineH / 14,
                    theAreaColor);
            prevOverlineXEndPos = x + w;
            prevOverlineYEndPos = yPos;
            prevOverlineSize    = lineH / 14;
            prevOverlineColor   = theAreaColor;
        }
Exemple #7
0
        void AddUnderLine(int x, int y, int w, int lineH,
                          PdfColor theAreaColor)
        {
            int yPos = y - lineH / 10;

            AddLine(x, yPos, x + w, yPos, lineH / 14,
                    theAreaColor);
            // save position for underlining a following InlineSpace
            prevUnderlineXEndPos = x + w;
            prevUnderlineYEndPos = yPos;
            prevUnderlineSize    = lineH / 14;
            prevUnderlineColor   = theAreaColor;
        }
Exemple #8
0
        public void StopRenderer()
        {
            fontSetup.AddToResources(new PdfFontCreator(pdfDoc), pdfDoc.getResources());
            pdfDoc.outputTrailer();

            pdfDoc           = null;
            pdfResources     = null;
            currentStream    = null;
            currentAnnotList = null;
            currentPage      = null;

            idReferences         = null;
            currentFontName      = String.Empty;
            currentFill          = null;
            prevUnderlineColor   = null;
            prevOverlineColor    = null;
            prevLineThroughColor = null;
            fontSetup            = null;
            fontInfo             = null;
        }
Exemple #9
0
        private void AddWordLines(WordArea area, int rx, int bl, int size,
                                  PdfColor theAreaColor)
        {
            if (area.getUnderlined())
            {
                int yPos = bl - size / 10;
                AddLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
                        theAreaColor);
                // save position for underlining a following InlineSpace
                prevUnderlineXEndPos = rx + area.getContentWidth();
                prevUnderlineYEndPos = yPos;
                prevUnderlineSize    = size / 14;
                prevUnderlineColor   = theAreaColor;
            }

            if (area.getOverlined())
            {
                int yPos = bl + area.GetFontState().Ascender + size / 10;
                AddLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
                        theAreaColor);
                prevOverlineXEndPos = rx + area.getContentWidth();
                prevOverlineYEndPos = yPos;
                prevOverlineSize    = size / 14;
                prevOverlineColor   = theAreaColor;
            }

            if (area.getLineThrough())
            {
                int yPos = bl + area.GetFontState().Ascender * 3 / 8;
                AddLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
                        theAreaColor);
                prevLineThroughXEndPos = rx + area.getContentWidth();
                prevLineThroughYEndPos = yPos;
                prevLineThroughSize    = size / 14;
                prevLineThroughColor   = theAreaColor;
            }
        }
Exemple #10
0
        /**
         * render inline area to PDF
         *
         * @param area inline area to render
         */
        public void RenderWordArea(WordArea area)
        {
            FontState fontState = area.GetFontState();
            String    name      = fontState.FontName;
            int       size      = fontState.FontSize;
            // This assumes that *all* CIDFonts use a /ToUnicode mapping
            Font font = (Font)fontState.FontInfo.GetFontByName(name);

            if ((!name.Equals(this.currentFontName)) ||
                (size != this.currentFontSize))
            {
                CloseText();

                this.currentFontName = name;
                this.currentFontSize = size;

                currentStream.SetFont(name, size);
            }

            // Do letter spacing (must be outside of [...] TJ]
            float letterspacing = ((float)fontState.LetterSpacing) / 1000f;

            if (letterspacing != this.currentLetterSpacing)
            {
                this.currentLetterSpacing = letterspacing;
                CloseText(); //?
                currentStream.SetLetterSpacing(letterspacing);
            }

            //--------------------------------------------
            PdfColor?a_color       = this.currentFill;
            PdfColor areaObj_color = area.GetColor();

            if (a_color == null || !areaObj_color.IsEq(a_color.Value))
            {
                //change area color
                a_color = areaObj_color;

                CloseText(); //?
                this.currentFill = a_color;
                currentStream.SetFontColor(a_color.Value);
            }
            //--------------------------------------------

            int rx           = this.currentXPosition;
            int bl           = this.currentYPosition;
            int areaContentW = area.getContentWidth();

            if (area.getUnderlined())
            {
                AddUnderLine(rx, bl, areaContentW, size, a_color.Value);
            }
            if (area.getOverlined())
            {
                AddOverLine(rx, bl, areaContentW, size, fontState.Ascender, a_color.Value);
            }
            if (area.getLineThrough())
            {
                AddLineThrough(rx, bl, areaContentW, size, fontState.Ascender, a_color.Value);
            }
            //--------------------------------------------


            _textPrinter.Reset(fontState, options != null && options.Kerning);
            if (!textOpen || bl != prevWordY)
            {
                CloseText();
                //set text matrix


                _textPrinter.SetTextPos(rx, bl);
                //pdf.Append("1 0 0 1 " + PdfNumber.doubleOut(rx / 1000f) +
                //    " " + PdfNumber.doubleOut(bl / 1000f) + " Tm [" + startText);
                prevWordY = bl;
                textOpen  = true; //***
            }
            else
            {
                // express the space between words in thousandths of an em
                int   space  = prevWordX - rx + prevWordWidth;
                float emDiff = (float)space / (float)currentFontSize * 1000f;
                // this prevents a problem in Acrobat Reader where large
                // numbers cause text to disappear or default to a limit
                if (emDiff < -33000)
                {
                    CloseText();
                    _textPrinter.SetTextPos(rx, bl);
                    //pdf.Append("1 0 0 1 " + PdfNumber.doubleOut(rx / 1000f) +
                    //    " " + PdfNumber.doubleOut(bl / 1000f) + " Tm [" + startText);
                    textOpen = true;//***
                }
                else
                {
                    _textPrinter.SetEmDiff(emDiff);
                    //pdf.Append(PdfNumber.doubleOut(emDiff));
                    //pdf.Append(" ");
                    //pdf.Append(startText);
                }
            }

            prevWordWidth = areaContentW;
            prevWordX     = rx;

            string s = area.GetTextContent();

            if (area is PageNumberInlineArea)
            {
                //need to resolve to page number
                s = idReferences.getPageNumber(s);
            }
            _textPrinter.WriteText(s);
            //-------
            _textPrinter.PrintContentTo(currentStream);
            //-------
            this.currentXPosition += area.getContentWidth();
        }
Exemple #11
0
        /**
         * add a filled rectangle to the current stream
         *
         * @param x the x position of left edge in millipoints
         * @param y the y position of top edge in millipoints
         * @param w the width in millipoints
         * @param h the height in millipoints
         * @param fill the fill color/gradient
         */

        private void AddFilledRect(int x, int y, int w, int h,
                                   PdfColor fill)
        {
            CloseText();
            currentStream.FillRect(x, y, w, h, fill);
        }
Exemple #12
0
        /**
         * add a rectangle to the current stream
         *
         * @param x the x position of left edge in millipoints
         * @param y the y position of top edge in millipoints
         * @param w the width in millipoints
         * @param h the height in millipoints
         * @param stroke the stroke color/gradient
         */

        private void AddRect(int x, int y, int w, int h, PdfColor stroke)
        {
            CloseText();
            currentStream.DrawRect(x, y, w, h, stroke);
        }
Exemple #13
0
        /**
         * add a line to the current stream
         *
         * @param x1 the start x location in millipoints
         * @param y1 the start y location in millipoints
         * @param x2 the end x location in millipoints
         * @param y2 the end y location in millipoints
         * @param th the thickness in millipoints
         * @param rs the rule style
         * @param r the red component
         * @param g the green component
         * @param b the blue component
         */

        private void AddLine(int x1, int y1, int x2, int y2, int th, RuleStyle rs,
                             PdfColor stroke)
        {
            CloseText();
            currentStream.DrawLine(x1, y1, x2, y2, th, rs, stroke);
        }
Exemple #14
0
        /**
         * render page into PDF
         *
         * @param page page to render
         */

        public void RenderPage(Page page)
        {
            BodyAreaContainer body;
            AreaContainer     before, after, start, end;

            currentStream = this.pdfDoc.makeContentStream();
            body          = page.getBody();
            before        = page.getBefore();
            after         = page.getAfter();
            start         = page.getStart();
            end           = page.getEnd();

            this.currentFontName      = "";
            this.currentFontSize      = 0;
            this.currentLetterSpacing = Single.NaN;

            currentStream.Write("BT\n");

            RenderBodyAreaContainer(body);

            if (before != null)
            {
                RenderAreaContainer(before);
            }

            if (after != null)
            {
                RenderAreaContainer(after);
            }

            if (start != null)
            {
                RenderAreaContainer(start);
            }

            if (end != null)
            {
                RenderAreaContainer(end);
            }
            CloseText();

            // Bug fix for issue 1823
            this.currentLetterSpacing = Single.NaN;

            float w = page.getWidth();
            float h = page.GetHeight();

            currentStream.Write("ET\n");

            currentPage = this.pdfDoc.makePage(
                this.pdfResources, currentStream,
                Convert.ToInt32(Math.Round(w / 1000)),
                Convert.ToInt32(Math.Round(h / 1000)), page);

            if (page.hasLinks() || currentAnnotList != null)
            {
                if (currentAnnotList == null)
                {
                    currentAnnotList = this.pdfDoc.makeAnnotList();
                }
                currentPage.SetAnnotList(currentAnnotList);

                ArrayList lsets = page.getLinkSets();
                foreach (LinkSet linkSet in lsets)
                {
                    linkSet.align();
                    String    dest     = linkSet.getDest();
                    int       linkType = linkSet.getLinkType();
                    ArrayList rsets    = linkSet.getRects();
                    foreach (LinkedRectangle lrect in rsets)
                    {
                        currentAnnotList.Add(this.pdfDoc.makeLink(lrect.getRectangle(),
                                                                  dest, linkType).GetReference());
                    }
                }
                currentAnnotList = null;
            }
            else
            {
                // just to be on the safe side
                currentAnnotList = null;
            }

            // ensures that color is properly reset for blocks that carry over pages
            this.currentFill = null;
        }
Exemple #15
0
        /**
         * render inline area to PDF
         *
         * @param area inline area to render
         */

        public void RenderWordArea(WordArea area)
        {
            // TODO: I don't understand why we are locking the private member
            // _wordAreaPDF.  Maybe this string buffer was originally static? (MG)
            lock (_wordAreaPDF)
            {
                StringBuilder pdf = _wordAreaPDF;
                pdf.Length = 0;

                GdiKerningPairs kerning          = null;
                bool            kerningAvailable = false;

                // If no options are supplied, by default we do not enable kerning
                if (options != null && options.Kerning)
                {
                    kerning = area.GetFontState().Kerning;
                    if (kerning != null && (kerning.Count > 0))
                    {
                        kerningAvailable = true;
                    }
                }

                String name = area.GetFontState().FontName;
                int    size = area.GetFontState().FontSize;

                // This assumes that *all* CIDFonts use a /ToUnicode mapping
                Font font         = (Font)area.GetFontState().FontInfo.GetFontByName(name);
                bool useMultiByte = font.MultiByteFont;

                string startText = useMultiByte ? "<" : "(";
                string endText   = useMultiByte ? "> " : ") ";

                if ((!name.Equals(this.currentFontName)) || (size != this.currentFontSize))
                {
                    CloseText();

                    this.currentFontName = name;
                    this.currentFontSize = size;
                    pdf = pdf.Append("/" + name + " " +
                                     PdfNumber.doubleOut(size / 1000f) + " Tf\n");
                }

                // Do letter spacing (must be outside of [...] TJ]
                float letterspacing = ((float)area.GetFontState().LetterSpacing) / 1000f;
                if (letterspacing != this.currentLetterSpacing)
                {
                    this.currentLetterSpacing = letterspacing;
                    CloseText();
                    pdf.Append(PdfNumber.doubleOut(letterspacing));
                    pdf.Append(" Tc\n");
                }

                PdfColor areaColor = this.currentFill;

                if (areaColor == null || areaColor.getRed() != (double)area.getRed() ||
                    areaColor.getGreen() != (double)area.getGreen() ||
                    areaColor.getBlue() != (double)area.getBlue())
                {
                    areaColor = new PdfColor((double)area.getRed(),
                                             (double)area.getGreen(),
                                             (double)area.getBlue());


                    CloseText();
                    this.currentFill = areaColor;
                    pdf.Append(this.currentFill.getColorSpaceOut(true));
                }


                int rx = this.currentXPosition;
                int bl = this.currentYPosition;

                AddWordLines(area, rx, bl, size, areaColor);

                if (!textOpen || bl != prevWordY)
                {
                    CloseText();

                    pdf.Append("1 0 0 1 " + PdfNumber.doubleOut(rx / 1000f) +
                               " " + PdfNumber.doubleOut(bl / 1000f) + " Tm [" + startText);
                    prevWordY = bl;
                    textOpen  = true;
                }
                else
                {
                    // express the space between words in thousandths of an em
                    int   space  = prevWordX - rx + prevWordWidth;
                    float emDiff = (float)space / (float)currentFontSize * 1000f;
                    // this prevents a problem in Acrobat Reader where large
                    // numbers cause text to disappear or default to a limit
                    if (emDiff < -33000)
                    {
                        CloseText();

                        pdf.Append("1 0 0 1 " + PdfNumber.doubleOut(rx / 1000f) +
                                   " " + PdfNumber.doubleOut(bl / 1000f) + " Tm [" + startText);
                        textOpen = true;
                    }
                    else
                    {
                        pdf.Append(PdfNumber.doubleOut(emDiff));
                        pdf.Append(" ");
                        pdf.Append(startText);
                    }
                }
                prevWordWidth = area.getContentWidth();
                prevWordX     = rx;

                string s;
                if (area.getPageNumberID() != null)
                {
                    // This text is a page number, so resolve it
                    s = idReferences.getPageNumber(area.getPageNumberID());
                    if (s == null)
                    {
                        s = String.Empty;
                    }
                }
                else
                {
                    s = area.getText();
                }

                int wordLength = s.Length;
                for (int index = 0; index < wordLength; index++)
                {
                    ushort ch = area.GetFontState().MapCharacter(s[index]);

                    if (!useMultiByte)
                    {
                        if (ch > 127)
                        {
                            pdf.Append("\\");
                            pdf.Append(Convert.ToString((int)ch, 8));
                        }
                        else
                        {
                            switch (ch)
                            {
                            case '(':
                            case ')':
                            case '\\':
                                pdf.Append("\\");
                                break;
                            }
                            pdf.Append((char)ch);
                        }
                    }
                    else
                    {
                        pdf.Append(GetUnicodeString(ch));
                    }

                    if (kerningAvailable && (index + 1) < wordLength)
                    {
                        ushort ch2 = area.GetFontState().MapCharacter(s[index + 1]);
                        AddKerning(pdf, ch, ch2, kerning, startText, endText);
                    }
                }
                pdf.Append(endText);

                currentStream.Write(pdf.ToString());

                this.currentXPosition += area.getContentWidth();
            }
        }