Пример #1
0
        public virtual void ChunkEndsAfterOrBeforeTabPosition()
        {
            String      outFileName   = destinationFolder + "chunkEndsAfterOrBeforeTabPosition.pdf";
            String      cmpFileName   = sourceFolder + "cmp_chunkEndsAfterOrBeforeTabPosition.pdf";
            PdfDocument pdfDoc        = new PdfDocument(new PdfWriter(outFileName));
            Document    doc           = new Document(pdfDoc);
            String      textBeforeTab = "a";
            String      textAfterTab  = "tab stop's position = ";
            Paragraph   paragraph;

            for (int i = 0; i < 20; i++)
            {
                paragraph = new Paragraph();
                paragraph.Add(new Text(textBeforeTab));
                TabStop[] tabStop = new TabStop[1];
                tabStop[0] = new TabStop(i);
                paragraph.AddTabStops(tabStop);
                paragraph.Add(new Tab());
                paragraph.Add(new Text(textAfterTab));
                paragraph.Add(JavaUtil.IntegerToString(i));
                doc.Add(paragraph);
            }
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Пример #2
0
 /// <summary>Sets the code to generate.</summary>
 /// <remarks>
 /// Sets the code to generate. If it's an UCC code and starts with '(' it will
 /// be split by the AI. This code in UCC mode is valid:
 /// <br />
 /// <c>(01)00000090311314(10)ABC123(15)060916</c>
 /// </remarks>
 /// <param name="code">the code to generate</param>
 public override void SetCode(String code)
 {
     if (GetCodeType() == Barcode128.CODE128_UCC && code.StartsWith("("))
     {
         int           idx = 0;
         StringBuilder ret = new StringBuilder("");
         while (idx >= 0)
         {
             int end = code.IndexOf(')', idx);
             if (end < 0)
             {
                 throw new ArgumentException("Badly formed ucc string");
             }
             String sai = code.JSubstring(idx + 1, end);
             if (sai.Length < 2)
             {
                 throw new ArgumentException("AI is too short");
             }
             int ai  = Convert.ToInt32(sai);
             int len = (int)ais.Get(ai);
             if (len == 0)
             {
                 throw new ArgumentException("AI not found");
             }
             sai = JavaUtil.IntegerToString(Convert.ToInt32(ai));
             if (sai.Length == 1)
             {
                 sai = "0" + sai;
             }
             idx = code.IndexOf('(', end);
             int next = (idx < 0 ? code.Length : idx);
             ret.Append(sai).Append(code.JSubstring(end + 1, next));
             if (len < 0)
             {
                 if (idx >= 0)
                 {
                     ret.Append(FNC1);
                 }
             }
             else
             {
                 if (next - end - 1 + sai.Length != len)
                 {
                     throw new ArgumentException("Invalid AI length");
                 }
             }
         }
         base.SetCode(ret.ToString());
     }
     else
     {
         base.SetCode(code);
     }
 }
Пример #3
0
        /// <summary>Extracts the names of the font in all the languages available.</summary>
        private void ReadNameTable()
        {
            int[] table_location = tables.Get("name");
            if (table_location == null)
            {
                if (fileName != null)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TableDoesNotExistsIn).SetMessageParams("name", fileName
                                                                                                               );
                }
                else
                {
                    throw new iText.IO.IOException(iText.IO.IOException.TableDoesNotExist).SetMessageParams("name");
                }
            }
            allNameEntries = new LinkedDictionary <int, IList <String[]> >();
            raf.Seek(table_location[0] + 2);
            int numRecords     = raf.ReadUnsignedShort();
            int startOfStorage = raf.ReadUnsignedShort();

            for (int k = 0; k < numRecords; ++k)
            {
                int platformID         = raf.ReadUnsignedShort();
                int platformEncodingID = raf.ReadUnsignedShort();
                int languageID         = raf.ReadUnsignedShort();
                int nameID             = raf.ReadUnsignedShort();
                int length             = raf.ReadUnsignedShort();
                int offset             = raf.ReadUnsignedShort();
                IList <String[]> names;
                if (allNameEntries.ContainsKey(nameID))
                {
                    names = allNameEntries.Get(nameID);
                }
                else
                {
                    allNameEntries.Put(nameID, names = new List <String[]>());
                }
                int pos = (int)raf.GetPosition();
                raf.Seek(table_location[0] + startOfStorage + offset);
                String name;
                if (platformID == 0 || platformID == 3 || platformID == 2 && platformEncodingID == 1)
                {
                    name = ReadUnicodeString(length);
                }
                else
                {
                    name = ReadStandardString(length);
                }
                names.Add(new String[] { JavaUtil.IntegerToString(platformID), JavaUtil.IntegerToString(platformEncodingID
                                                                                                        ), JavaUtil.IntegerToString(languageID), name });
                raf.Seek(pos);
            }
        }
Пример #4
0
 // it is StructTreeRoot
 // should never happen as we always should have only one root tag and we don't remove it
 private String ComposeExceptionBasedOnNamespacePresence(String role, PdfNamespace @namespace, String withoutNsEx
                                                         , String withNsEx)
 {
     if (@namespace == null)
     {
         return(String.Format(withoutNsEx, role));
     }
     else
     {
         String nsName             = @namespace.GetNamespaceName();
         PdfIndirectReference @ref = @namespace.GetPdfObject().GetIndirectReference();
         if (@ref != null)
         {
             nsName = nsName + " (" + JavaUtil.IntegerToString(@ref.GetObjNumber()) + " " + JavaUtil.IntegerToString(@ref
                                                                                                                     .GetGenNumber()) + " obj)";
         }
         return(String.Format(withNsEx, role, nsName));
     }
 }
Пример #5
0
        public override String ToString()
        {
            StringBuilder states = new StringBuilder(" ");

            if (CheckState(FREE))
            {
                states.Append("Free; ");
            }
            if (CheckState(MODIFIED))
            {
                states.Append("Modified; ");
            }
            if (CheckState(MUST_BE_FLUSHED))
            {
                states.Append("MustBeFlushed; ");
            }
            if (CheckState(READING))
            {
                states.Append("Reading; ");
            }
            if (CheckState(FLUSHED))
            {
                states.Append("Flushed; ");
            }
            if (CheckState(ORIGINAL_OBJECT_STREAM))
            {
                states.Append("OriginalObjectStream; ");
            }
            if (CheckState(FORBID_RELEASE))
            {
                states.Append("ForbidRelease; ");
            }
            if (CheckState(READ_ONLY))
            {
                states.Append("ReadOnly; ");
            }
            return(MessageFormatUtil.Format("{0} {1} R{2}", JavaUtil.IntegerToString(GetObjNumber()), JavaUtil.IntegerToString
                                                (GetGenNumber()), states.JSubstring(0, states.Length - 1)));
        }
Пример #6
0
        /// <summary>Places the barcode in a <CODE>PdfCanvas</CODE>.</summary>
        /// <remarks>
        /// Places the barcode in a <CODE>PdfCanvas</CODE>. The
        /// barcode is always placed at coordinates (0, 0). Use the
        /// translation matrix to move it elsewhere.
        /// <p>
        /// The bars and text are written in the following colors:
        /// <p>
        /// <P><TABLE BORDER=1 summary="">
        /// <TR>
        /// <TH><P><CODE>barColor</CODE></TH>
        /// <TH><P><CODE>textColor</CODE></TH>
        /// <TH><P>Result</TH>
        /// </TR>
        /// <TR>
        /// <TD><P><CODE>null</CODE></TD>
        /// <TD><P><CODE>null</CODE></TD>
        /// <TD><P>bars and text painted with current fill color</TD>
        /// </TR>
        /// <TR>
        /// <TD><P><CODE>barColor</CODE></TD>
        /// <TD><P><CODE>null</CODE></TD>
        /// <TD><P>bars and text painted with <CODE>barColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><P><CODE>null</CODE></TD>
        /// <TD><P><CODE>textColor</CODE></TD>
        /// <TD><P>bars painted with current color<br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// <TR>
        /// <TD><P><CODE>barColor</CODE></TD>
        /// <TD><P><CODE>textColor</CODE></TD>
        /// <TD><P>bars painted with <CODE>barColor</CODE><br />text painted with <CODE>textColor</CODE></TD>
        /// </TR>
        /// </TABLE>
        /// </remarks>
        /// <param name="canvas">the <CODE>PdfCanvas</CODE> where the barcode will be placed</param>
        /// <param name="barColor">the color of the bars. It can be <CODE>null</CODE></param>
        /// <param name="textColor">the color of the text. It can be <CODE>null</CODE></param>
        /// <returns>the dimensions the barcode occupies</returns>
        public override Rectangle PlaceBarcode(PdfCanvas canvas, Color barColor, Color textColor)
        {
            String fullCode = this.code;

            if (this.checksumText)
            {
                fullCode = fullCode + JavaUtil.IntegerToString(GetChecksum(this.code));
            }
            float fontX = 0.0f;

            if (this.font != null)
            {
                String var10001 = this.altText != null ? this.altText : fullCode;
                fullCode = this.altText != null ? this.altText : fullCode;
                fontX    = this.font.GetWidth(var10001, this.size);
            }
            String bCode = this.code;

            if (this.generateChecksum)
            {
                bCode += GetChecksum(bCode);
            }
            int idx;

            idx = bCode.Length;
            float fullWidth  = (float)((idx + 2) * 11) * this.x + 2.0f * this.x;
            float barStartX  = 0.0f;
            float textStartX = 0.0f;

            switch (this.textAlignment)
            {
            case 1: {
                break;
            }

            case 2: {
                if (fontX > fullWidth)
                {
                    barStartX = fontX - fullWidth;
                }
                else
                {
                    textStartX = fullWidth - fontX;
                }
                break;
            }

            default: {
                if (fontX > fullWidth)
                {
                    barStartX = (fontX - fullWidth) / 2.0f;
                }
                else
                {
                    textStartX = (fullWidth - fontX) / 2.0f;
                }
                break;
            }
            }
            float barStartY  = 0.0f;
            float textStartY = 0.0f;

            if (this.font != null)
            {
                if (this.baseline <= 0.0f)
                {
                    textStartY = this.barHeight - this.baseline;
                }
                else
                {
                    textStartY = -this.GetDescender();
                    barStartY  = textStartY + this.baseline;
                }
            }
            byte[] bars = GetBarsMSI(bCode);
            if (barColor != null)
            {
                canvas.SetFillColor(barColor);
            }
            for (int k = 0; k < bars.Length; ++k)
            {
                float w = (float)bars[k] * this.x;
                if (bars[k] == 1)
                {
                    canvas.Rectangle((double)barStartX, (double)barStartY, (double)(w - this.inkSpreading), (double)this.barHeight
                                     );
                }
                barStartX += this.x;
            }
            canvas.Fill();
            if (this.font != null)
            {
                if (textColor != null)
                {
                    canvas.SetFillColor(textColor);
                }
                canvas.BeginText();
                canvas.SetFontAndSize(this.font, this.size);
                canvas.SetTextMatrix(textStartX, textStartY);
                canvas.ShowText(fullCode);
                canvas.EndText();
            }
            return(this.GetBarcodeSize());
        }
Пример #7
0
        /// <summary>This method checks if the image is a valid JPEG and processes some parameters.</summary>
        /// <exception cref="iText.IO.IOException"/>
        /// <exception cref="System.IO.IOException"/>
        private static void ProcessParameters(Stream jpegStream, String errorID, ImageData image)
        {
            byte[][] icc = null;
            if (jpegStream.Read() != 0xFF || jpegStream.Read() != 0xD8)
            {
                throw new iText.IO.IOException(iText.IO.IOException._1IsNotAValidJpegFile).SetMessageParams(errorID);
            }
            bool firstPass = true;
            int  len;

            while (true)
            {
                int v = jpegStream.Read();
                if (v < 0)
                {
                    throw new iText.IO.IOException(iText.IO.IOException.PrematureEofWhileReadingJpeg);
                }
                if (v == 0xFF)
                {
                    int marker = jpegStream.Read();
                    if (firstPass && marker == M_APP0)
                    {
                        firstPass = false;
                        len       = GetShort(jpegStream);
                        if (len < 16)
                        {
                            StreamUtil.Skip(jpegStream, len - 2);
                            continue;
                        }
                        byte[] bcomp = new byte[JFIF_ID.Length];
                        int    r     = jpegStream.Read(bcomp);
                        if (r != bcomp.Length)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1CorruptedJfifMarker).SetMessageParams(errorID);
                        }
                        bool found = true;
                        for (int k = 0; k < bcomp.Length; ++k)
                        {
                            if (bcomp[k] != JFIF_ID[k])
                            {
                                found = false;
                                break;
                            }
                        }
                        if (!found)
                        {
                            StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length);
                            continue;
                        }
                        StreamUtil.Skip(jpegStream, 2);
                        int units = jpegStream.Read();
                        int dx    = GetShort(jpegStream);
                        int dy    = GetShort(jpegStream);
                        if (units == 1)
                        {
                            image.SetDpi(dx, dy);
                        }
                        else
                        {
                            if (units == 2)
                            {
                                image.SetDpi((int)(dx * 2.54f + 0.5f), (int)(dy * 2.54f + 0.5f));
                            }
                        }
                        StreamUtil.Skip(jpegStream, len - 2 - bcomp.Length - 7);
                        continue;
                    }
                    if (marker == M_APPE)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteappe = new byte[len];
                        for (int k = 0; k < len; ++k)
                        {
                            byteappe[k] = (byte)jpegStream.Read();
                        }
                        if (byteappe.Length >= 12)
                        {
                            String appe = iText.IO.Util.JavaUtil.GetStringForBytes(byteappe, 0, 5, "ISO-8859-1");
                            if (appe.Equals("Adobe"))
                            {
                                image.SetInverted(true);
                            }
                        }
                        continue;
                    }
                    if (marker == M_APP2)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteapp2 = new byte[len];
                        for (int k = 0; k < len; ++k)
                        {
                            byteapp2[k] = (byte)jpegStream.Read();
                        }
                        if (byteapp2.Length >= 14)
                        {
                            String app2 = iText.IO.Util.JavaUtil.GetStringForBytes(byteapp2, 0, 11, "ISO-8859-1");
                            if (app2.Equals("ICC_PROFILE"))
                            {
                                int order = byteapp2[12] & 0xff;
                                int count = byteapp2[13] & 0xff;
                                // some jpeg producers don't know how to count to 1
                                if (order < 1)
                                {
                                    order = 1;
                                }
                                if (count < 1)
                                {
                                    count = 1;
                                }
                                if (icc == null)
                                {
                                    icc = new byte[count][];
                                }
                                icc[order - 1] = byteapp2;
                            }
                        }
                        continue;
                    }
                    if (marker == M_APPD)
                    {
                        len = GetShort(jpegStream) - 2;
                        byte[] byteappd = new byte[len];
                        for (int k = 0; k < len; k++)
                        {
                            byteappd[k] = (byte)jpegStream.Read();
                        }
                        // search for '8BIM Resolution' marker
                        int k_1;
                        for (k_1 = 0; k_1 < len - PS_8BIM_RESO.Length; k_1++)
                        {
                            bool found = true;
                            for (int j = 0; j < PS_8BIM_RESO.Length; j++)
                            {
                                if (byteappd[k_1 + j] != PS_8BIM_RESO[j])
                                {
                                    found = false;
                                    break;
                                }
                            }
                            if (found)
                            {
                                break;
                            }
                        }
                        k_1 += PS_8BIM_RESO.Length;
                        if (k_1 < len - PS_8BIM_RESO.Length)
                        {
                            // "PASCAL String" for name, i.e. string prefix with length byte
                            // padded to be even length; 2 null bytes if empty
                            byte namelength = byteappd[k_1];
                            // add length byte
                            namelength++;
                            // add padding
                            if (namelength % 2 == 1)
                            {
                                namelength++;
                            }
                            // just skip name
                            k_1 += namelength;
                            // size of the resolution data
                            int resosize = (byteappd[k_1] << 24) + (byteappd[k_1 + 1] << 16) + (byteappd[k_1 + 2] << 8) + byteappd[k_1
                                                                                                                                   + 3];
                            // should be 16
                            if (resosize != 16)
                            {
                                // fail silently, for now
                                //System.err.println("DEBUG: unsupported resolution IRB size");
                                continue;
                            }
                            k_1 += 4;
                            int dx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int unitsx = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int dy = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            k_1 += 2;
                            // skip 2 unknown bytes
                            k_1 += 2;
                            int unitsy = (byteappd[k_1] << 8) + (byteappd[k_1 + 1] & 0xff);
                            if (unitsx == 1 || unitsx == 2)
                            {
                                dx = (unitsx == 2 ? (int)(dx * 2.54f + 0.5f) : dx);
                                // make sure this is consistent with JFIF data
                                if (image.GetDpiX() != 0 && image.GetDpiX() != dx)
                                {
                                    ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
                                    logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiX: {0} vs {1})", image.GetDpiX(), dx));
                                }
                                else
                                {
                                    image.SetDpi(dx, image.GetDpiY());
                                }
                            }
                            if (unitsy == 1 || unitsy == 2)
                            {
                                dy = (unitsy == 2 ? (int)(dy * 2.54f + 0.5f) : dy);
                                // make sure this is consistent with JFIF data
                                if (image.GetDpiY() != 0 && image.GetDpiY() != dy)
                                {
                                    ILog logger = LogManager.GetLogger(typeof(JpegImageHelper));
                                    logger.Debug(MessageFormatUtil.Format("Inconsistent metadata (dpiY: {0} vs {1})", image.GetDpiY(), dy));
                                }
                                else
                                {
                                    image.SetDpi(image.GetDpiX(), dx);
                                }
                            }
                        }
                        continue;
                    }
                    firstPass = false;
                    int markertype = Marker(marker);
                    if (markertype == VALID_MARKER)
                    {
                        StreamUtil.Skip(jpegStream, 2);
                        if (jpegStream.Read() != 0x08)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1MustHave8BitsPerComponent).SetMessageParams(errorID);
                        }
                        image.SetHeight(GetShort(jpegStream));
                        image.SetWidth(GetShort(jpegStream));
                        image.SetColorSpace(jpegStream.Read());
                        image.SetBpc(8);
                        break;
                    }
                    else
                    {
                        if (markertype == UNSUPPORTED_MARKER)
                        {
                            throw new iText.IO.IOException(iText.IO.IOException._1UnsupportedJpegMarker2).SetMessageParams(errorID, JavaUtil.IntegerToString
                                                                                                                               (marker));
                        }
                        else
                        {
                            if (markertype != NOPARAM_MARKER)
                            {
                                StreamUtil.Skip(jpegStream, GetShort(jpegStream) - 2);
                            }
                        }
                    }
                }
            }
            if (icc != null)
            {
                int total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    if (icc[k] == null)
                    {
                        icc = null;
                        return;
                    }
                    total += icc[k].Length - 14;
                }
                byte[] ficc = new byte[total];
                total = 0;
                for (int k = 0; k < icc.Length; ++k)
                {
                    Array.Copy(icc[k], 14, ficc, total, icc[k].Length - 14);
                    total += icc[k].Length - 14;
                }
                try {
                    image.SetProfile(IccProfile.GetInstance(ficc, image.GetColorSpace()));
                }
                catch (ArgumentException) {
                }
            }
        }