Ejemplo n.º 1
0
 public static byte[] DecodeStringToByte(PdfString s)
 {
     byte[] b  = s.GetBytes();
     byte[] br = new byte[b.Length];
     System.Array.Copy(b, 0, br, 0, b.Length);
     return(br);
 }
Ejemplo n.º 2
0
 virtual public String DecodeStringToUnicode(PdfString ps)
 {
     if (ps.IsHexWriting())
     {
         return(PdfEncodings.ConvertToString(ps.GetBytes(), "UnicodeBigUnmarked"));
     }
     else
     {
         return(ps.ToUnicodeString());
     }
 }
Ejemplo n.º 3
0
 protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1)
 {
     if (obj1 is PdfNumber)
     {
         PdfNumber number = (PdfNumber)obj1;
         if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains("."))
         {
             throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range"));
         }
     }
     else if (obj1 is PdfString)
     {
         PdfString str = (PdfString)obj1;
         if (str.GetBytes().Length > maxStringLength)
         {
             throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long"));
         }
     }
     else if (obj1 is PdfArray)
     {
         PdfArray array = (PdfArray)obj1;
         if (array.Size > maxArrayLength)
         {
             throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds"));
         }
     }
     else if (obj1 is PdfDictionary)
     {
         PdfDictionary dictionary = (PdfDictionary)obj1;
         if (dictionary.Size > maxDictionaryLength)
         {
             throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds"));
         }
         if (PdfName.CATALOG.Equals(dictionary.GetAsName(PdfName.TYPE)))
         {
             if (dictionary.Contains(PdfName.AA))
             {
                 throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry"));
             }
             if (CheckStructure(conformanceLevel))
             {
                 PdfDictionary markInfo = dictionary.GetAsDict(PdfName.MARKINFO);
                 if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false)
                 {
                     throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true"));
                 }
                 if (!dictionary.Contains(PdfName.LANG))
                 {
                     throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry"));
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
        internal override void AddChar(PdfString mark, PdfObject code)
        {
            byte[] src  = mark.GetBytes();
            String dest = CreateStringFromBytes(code.GetBytes());

            if (src.Length == 1)
            {
                singleByteMappings[src[0] & 0xff] = dest;
            }
            else if (src.Length == 2)
            {
                int intSrc = src[0] & 0xFF;
                intSrc <<= 8;
                intSrc  |= src[1] & 0xFF;
                doubleByteMappings[intSrc] = dest;
            }
            else
            {
                throw new IOException(MessageLocalization.GetComposedMessage("mapping.code.should.be.1.or.two.bytes.and.not.1", src.Length));
            }
        }
Ejemplo n.º 5
0
        protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1)
        {
            if (obj1 is PdfNumber)
            {
                PdfNumber number = (PdfNumber)obj1;
                if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains("."))
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range"));
                }
            }
            else if (obj1 is PdfString)
            {
                PdfString str = (PdfString)obj1;
                if (str.GetBytes().Length > maxStringLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long"));
                }
            }
            else if (obj1 is PdfArray)
            {
                PdfArray array = (PdfArray)obj1;
                if (array.Size > maxArrayLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds"));
                }
            }
            else if (obj1 is PdfDictionary)
            {
                PdfDictionary dictionary = (PdfDictionary)obj1;
                if (dictionary.Size > maxDictionaryLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds"));
                }
                PdfName type = dictionary.GetAsName(PdfName.TYPE);
                if (PdfName.CATALOG.Equals(type))
                {
                    if (!dictionary.Contains(PdfName.METADATA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.contain.metadata"));
                    }

                    if (dictionary.Contains(PdfName.AA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry"));
                    }

                    if (dictionary.Contains(PdfName.NAMES))
                    {
                        PdfDictionary names = GetDirectDictionary(dictionary.Get(PdfName.NAMES));
                        if (names != null && names.Contains(PdfName.EMBEDDEDFILES))
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.embeddedfiles.names.entry"));
                        }
                    }

                    if (CheckStructure(conformanceLevel))
                    {
                        PdfDictionary markInfo = GetDirectDictionary(dictionary.Get(PdfName.MARKINFO));
                        if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true"));
                        }
                        if (!dictionary.Contains(PdfName.LANG))
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry"));
                        }
                    }
                }
                else if (PdfName.PAGE.Equals(type))
                {
                    if (dictionary.Contains(PdfName.AA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("page.dictionary.shall.not.include.aa.entry"));
                    }
                }
                else if (PdfName.OUTPUTINTENT.Equals(type))
                {
                    PdfObject destOutputIntent = dictionary.Get(PdfName.DESTOUTPUTPROFILE);
                    if (destOutputIntent != null && pdfaDestOutputIntent != null)
                    {
                        if (pdfaDestOutputIntent.IndRef != destOutputIntent.IndRef)
                        {
                            throw new PdfAConformanceException(obj1,
                                                               MessageLocalization.GetComposedMessage(
                                                                   "if.outputintents.array.more.than.one.entry.the.same.indirect.object"));
                        }
                    }
                    else
                    {
                        pdfaDestOutputIntent = destOutputIntent;
                    }

                    PdfName gts = dictionary.GetAsName(PdfName.S);
                    if (pdfaDestOutputIntent != null)
                    {
                        if (PdfName.GTS_PDFA1.Equals(gts))
                        {
                            if (pdfaOutputIntentColorSpace != null)
                            {
                                throw new PdfAConformanceException(obj1,
                                                                   MessageLocalization.GetComposedMessage("a.pdfa.file.may.have.only.one.pdfa.outputintent"));
                            }
                            pdfaOutputIntentColorSpace = "";
                            ICC_Profile icc_profile = writer.ColorProfile;
                            pdfaOutputIntentColorSpace = Encoding.GetEncoding("US-ASCII").GetString(icc_profile.Data, 16, 4);
                        }
                    }
                    else
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent"));
                    }
                }
            }
        }
 /**
  * Decodes a PdfString (which will contain glyph ids encoded in the font's encoding)
  * based on the active font, and determine the unicode equivalent
  * @param in    the String that needs to be encoded
  * @return  the encoded String
  * @since 2.1.7
  */
 private String Decode(PdfString inp)
 {
     byte[] bytes = inp.GetBytes();
     return(Gs().font.Decode(bytes, 0, bytes.Length));
 }
 private bool ObjectsIsEquals(PdfString outString, PdfString cmpString)
 {
     return(Util.ArraysAreEqual(cmpString.GetBytes(), outString.GetBytes()));
 }
Ejemplo n.º 8
0
 /**
  * Decodes a PdfString (which will contain glyph ids encoded in the font's encoding)
  * based on the active font, and determine the unicode equivalent
  * @param in    the String that needs to be encoded
  * @return  the encoded String
  * @since 2.1.7
  */
 private String Decode(PdfString inp){
     byte[] bytes = inp.GetBytes();
     return Gs().font.Decode(bytes, 0, bytes.Length);
 }
Ejemplo n.º 9
0
 /**
  * Decodes a PdfString (which will contain glyph ids encoded in the font's encoding)
  * based on the active font, and determine the unicode equivalent
  * @param in	the String that needs to be encoded
  * @return	    the encoded String
  */
 private String Decode(PdfString @in)
 {
     byte[] bytes = @in.GetBytes();
     return(gs.font.Decode(bytes, 0, bytes.Length));
 }
Ejemplo n.º 10
0
        IDictionary <string, IList <object> > ParseDAParam(PdfString DA)
        {
            IDictionary <string, IList <object> > commandArguments = new Dictionary <string, IList <object> >();

            PRTokeniser    tokeniser        = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(DA.GetBytes())));
            IList <object> currentArguments = new List <object>();

            while (tokeniser.NextToken())
            {
                if (tokeniser.TokenType == PRTokeniser.TokType.OTHER)
                {
                    String key = tokeniser.StringValue;

                    if (key == "RG" || key == "G" || key == "K")
                    {
                        key = STROKE_COLOR;
                    }
                    else if (key == "rg" || key == "g" || key == "k")
                    {
                        key = FILL_COLOR;
                    }

                    if (commandArguments.ContainsKey(key))
                    {
                        commandArguments[key] = currentArguments;
                    }
                    else
                    {
                        commandArguments.Add(key, currentArguments);
                    }

                    currentArguments = new List <object>();
                }
                else
                {
                    switch (tokeniser.TokenType)
                    {
                    case PRTokeniser.TokType.NUMBER:
                        currentArguments.Add(new PdfNumber(tokeniser.StringValue));
                        break;

                    case PRTokeniser.TokType.NAME:
                        currentArguments.Add(new PdfName(tokeniser.StringValue));
                        break;

                    default:
                        currentArguments.Add(tokeniser.StringValue);
                        break;
                    }
                }
            }

            return(commandArguments);
        }
Ejemplo n.º 11
0
 /**
  * ! .NET SPECIFIC; this method is used to avoid unecessary using of StringBuilder because it is slow in .NET !
  * Decodes a single character PdfString (which will contain glyph ids encoded in the font's encoding)
  * based on the active font, and determine the unicode equivalent
  * @param in	the String that needs to be encoded
  * @return	    the encoded String
  */
 private String DecodeSingleCharacter(PdfString @in) {
     byte[] bytes = @in.GetBytes();
     return gs.font.DecodeSingleCharacter(bytes, 0, bytes.Length);
 }
Ejemplo n.º 12
0
        protected override void CheckPdfObject(PdfWriter writer, int key, Object obj1)
        {
            if (obj1 is PdfNumber)
            {
                PdfNumber number = (PdfNumber)obj1;
                if (Math.Abs(number.DoubleValue) > maxRealValue && number.ToString().Contains("."))
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("real.number.is.out.of.range"));
                }
            }
            else if (obj1 is PdfString)
            {
                PdfString str = (PdfString)obj1;
                if (str.GetBytes().Length > maxStringLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.string.is.too.long"));
                }
            }
            else if (obj1 is PdfArray)
            {
                PdfArray array = (PdfArray)obj1;
                if (array.Size > maxArrayLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.array.is.out.of.bounds"));
                }
            }
            else if (obj1 is PdfDictionary)
            {
                PdfDictionary dictionary = (PdfDictionary)obj1;
                if (dictionary.Size > maxDictionaryLength)
                {
                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("pdf.dictionary.is.out.of.bounds"));
                }
                PdfName type = dictionary.GetAsName(PdfName.TYPE);
                if (PdfName.CATALOG.Equals(type))
                {
                    if (!dictionary.Contains(PdfName.METADATA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.contain.metadata"));
                    }

                    if (dictionary.Contains(PdfName.AA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.an.aa.entry"));
                    }

                    if (dictionary.Contains(PdfName.NAMES))
                    {
                        PdfDictionary names = GetDirectDictionary(dictionary.Get(PdfName.NAMES));
                        if (names != null && names.Contains(PdfName.EMBEDDEDFILES))
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("the.document.catalog.dictionary.shall.not.include.embeddedfiles.names.entry"));
                        }
                    }

                    if (CheckStructure(conformanceLevel))
                    {
                        PdfDictionary markInfo = GetDirectDictionary(dictionary.Get(PdfName.MARKINFO));
                        if (markInfo == null || markInfo.GetAsBoolean(PdfName.MARKED) == null || markInfo.GetAsBoolean(PdfName.MARKED).BooleanValue == false)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.shall.include.a.markinfo.dictionary.whose.entry.marked.shall.have.a.value.of.true"));
                        }
                        if (!dictionary.Contains(PdfName.LANG))
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("document.catalog.dictionary.should.contain.lang.entry"));
                        }
                    }

                    PdfArray outputIntents          = GetDirectArray(dictionary.Get(PdfName.OUTPUTINTENTS));
                    bool     pdfa1OutputIntentFound = false;
                    if (outputIntents != null && outputIntents.Size > 0)
                    {
                        for (int i = 0; i < outputIntents.Size; i++)
                        {
                            PdfDictionary outputIntentDictionary = GetDirectDictionary(outputIntents[i]);
                            PdfName       gts = outputIntentDictionary.GetAsName(PdfName.S);
                            if (PdfName.GTS_PDFA1.Equals(gts))
                            {
                                if (pdfa1OutputIntentFound)
                                {
                                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("a.pdfa.file.may.have.only.one.pdfa.outputintent"));
                                }
                                pdfa1OutputIntentFound = true;
                            }
                            if (outputIntentDictionary != null)
                            {
                                PdfObject destOutputIntent = outputIntentDictionary.Get(PdfName.DESTOUTPUTPROFILE);
                                if (destOutputIntent == null && PdfName.GTS_PDFA1.Equals(gts))
                                {
                                    throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent"));
                                }
                            }
                        }
                    }

                    if ((rgbUsed || cmykUsed || grayUsed) && !pdfa1OutputIntentFound)
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("if.device.rgb.cmyk.gray.used.in.file.that.file.shall.contain.pdfa.outputintent"));
                    }
                }
                else if (PdfName.PAGE.Equals(type))
                {
                    if (dictionary.Contains(PdfName.AA))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("page.dictionary.shall.not.include.aa.entry"));
                    }
                }
                else if (PdfName.OUTPUTINTENT.Equals(type))
                {
                    PdfObject iccProfileStream = dictionary.Get(PdfName.DESTOUTPUTPROFILE);
                    String    inputColorSpace  = "";
                    if (iccProfileStream != null)
                    {
                        ICC_Profile icc_profile = writer.ColorProfile;
                        inputColorSpace = Encoding.GetEncoding("US-ASCII").GetString(icc_profile.Data, 16, 4);
                    }
                    PdfName gts = dictionary.GetAsName(PdfName.S);
                    if (!PdfName.GTS_PDFA1.Equals(gts))
                    {
                        throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("outputintent.shall.have.gtspdfa1.and.destoutputintent"));
                    }
                    if ("RGB ".Equals(inputColorSpace))
                    {
                        if (cmykUsed)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicecmyk.may.be.used.only.if.the.file.has.a.cmyk.pdfa.outputIntent"));
                        }
                    }
                    else if ("CMYK".Equals(inputColorSpace))
                    {
                        if (rgbUsed)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.may.be.used.only.if.the.file.has.a.rgb.pdfa.outputIntent"));
                        }
                    }
                    else
                    {
                        if (cmykUsed)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicecmyk.may.be.used.only.if.the.file.has.a.cmyk.pdfa.outputIntent"));
                        }
                        if (rgbUsed)
                        {
                            throw new PdfAConformanceException(obj1, MessageLocalization.GetComposedMessage("devicergb.may.be.used.only.if.the.file.has.a.rgb.pdfa.outputIntent"));
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public virtual bool CompareStrings(PdfString outString, PdfString cmpString)
 {
     return(Util.ArraysAreEqual(cmpString.GetBytes(), outString.GetBytes()));
 }