/** * Creates an XmpWriter. * @param os * @param utfEncoding * @param extraSpace * @throws IOException */ public XmpWriter(Stream os, string utfEncoding, int extraSpace) { this.extraSpace = extraSpace; writer = new StreamWriter(os, new EncodingNoPreamble(IanaEncodings.GetEncodingEncoding(utfEncoding))); writer.Write(XPACKET_PI_BEGIN); writer.Write("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n"); writer.Write("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"); about = ""; }
/** * Detects encoding from a stream. * * @param inp the stream * @return a Reader with the deduced encoding. * @throws IOException if IO went wrong * @throws UnsupportedEncodingException if unsupported encoding was detected */ virtual public StreamReader DetectEncoding(Stream inp) { byte[] b4 = new byte[4]; int count = inp.Read(b4, 0, b4.Length); if (count != 4) { throw new IOException("Insufficient length"); } String encoding = XMLUtil.GetEncodingName(b4); String decl = null; int bytesNumber = 0; if (encoding.Equals("UTF-8")) { StringBuilder sb = new StringBuilder(); int c; while (bytesNumber < 1028 && ((c = inp.ReadByte()) != -1)) { if (c == '>') { break; } sb.Append((char)c); bytesNumber++; } decl = sb.ToString(); } else if (encoding.Equals("CP037")) { MemoryStream bi = new MemoryStream(); int c; while (bytesNumber < 1028 && ((c = inp.ReadByte()) != -1)) { if (c == 0x6e) // that's '>' in ebcdic { break; } bi.WriteByte((byte)c); bytesNumber++; } decl = Encoding.GetEncoding(37).GetString(bi.ToArray()); } if (decl != null) { decl = EncodingUtil.GetDeclaredEncoding(decl); if (decl != null) { encoding = decl; } } if (inp.CanSeek) { inp.Seek(0, SeekOrigin.Begin); } return(new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding))); }
/// <summary>Serializes an <code>XMPMeta</code>-object as RDF into a string.</summary> /// <remarks> /// Serializes an <code>XMPMeta</code>-object as RDF into a string. /// <em>Note:</em> Encoding is forced to UTF-16 when serializing to a /// string to ensure the correctness of "exact packet size". /// </remarks> /// <param name="xmp">a metadata implementation object</param> /// <param name="options"> /// Options to control the serialization (see /// <see cref="iText.Kernel.XMP.Options.SerializeOptions"/> /// ). /// </param> /// <returns>Returns a string containing the serialized RDF.</returns> public static String SerializeToString(XMPMetaImpl xmp, SerializeOptions options) { // forces the encoding to be UTF-16 to get the correct string length options = options ?? new SerializeOptions(); options.SetEncodeUTF16BE(true); MemoryStream output = new MemoryStream(2048); Serialize(xmp, output, options); try { return(new EncodingNoPreamble(IanaEncodings.GetEncodingEncoding(options.GetEncoding())).GetString(output.GetBuffer())); } catch (Exception) { // cannot happen as UTF-8/16LE/BE is required to be implemented in // Java return(GetString(output.GetBuffer())); } }
/// <summary> /// Exports the destinations to XML. The DTD for this XML is: /// /// /// <?xml version='1.0' encoding='UTF-8'?> /// <!ELEMENT Name (#PCDATA)> /// <!ATTLIST Name /// Page CDATA #IMPLIED /// > /// <!ELEMENT Destination (Name)*> /// /// whatever the encoding /// @throws IOException on error /// </summary> /// <param name="names">the names</param> /// <param name="outp">the export destination. The stream is not closed</param> /// <param name="encoding">the encoding according to IANA conventions</param> /// <param name="onlyAscii">codes above 127 will always be escaped with &#nn; if true ,</param> public static void ExportToXml(Hashtable names, Stream outp, string encoding, bool onlyAscii) { StreamWriter wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXml(names, wrt, encoding, onlyAscii); }
public static string ConvertToString(byte[] bytes, string encoding) { if (bytes == null) { return(PdfObject.NOTHING); } if (encoding == null || encoding.Length == 0) { char[] c = new char[bytes.Length]; for (int k = 0; k < bytes.Length; ++k) { c[k] = (char)(bytes[k] & 0xff); } return(new String(c)); } IExtraEncoding extra; extraEncodings.TryGetValue(encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture), out extra); if (extra != null) { String text = extra.ByteToChar(bytes, encoding); if (text != null) { return(text); } } char[] ch = null; if (encoding.Equals(BaseFont.WINANSI)) { ch = winansiByteToChar; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { ch = pdfEncodingByteToChar; } if (ch != null) { int len = bytes.Length; char[] c = new char[len]; for (int k = 0; k < len; ++k) { c[k] = ch[bytes[k] & 0xff]; } return(new String(c)); } String nameU = encoding.ToUpper(System.Globalization.CultureInfo.InvariantCulture); bool marker = false; bool big = false; int offset = 0; if (bytes.Length >= 2) { if (bytes[0] == (byte)254 && bytes[1] == (byte)255) { marker = true; big = true; offset = 2; } else if (bytes[0] == (byte)255 && bytes[1] == (byte)254) { marker = true; big = false; offset = 2; } } Encoding enc = null; if (nameU.Equals("UNICODEBIGUNMARKED") || nameU.Equals("UNICODEBIG")) { enc = new UnicodeEncoding(marker ? big : true, false); } if (nameU.Equals("UNICODELITTLEUNMARKED") || nameU.Equals("UNICODELITTLE")) { enc = new UnicodeEncoding(marker ? big : false, false); } if (enc != null) { return(enc.GetString(bytes, offset, bytes.Length - offset)); } return(IanaEncodings.GetEncodingEncoding(encoding).GetString(bytes)); }
/** Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according * to the font's encoding. * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding * @param encoding the encoding * @param char1 the <CODE>char</CODE> to be converted */ public static byte[] ConvertToBytes(char char1, String encoding) { if (encoding == null || encoding.Length == 0) { return new byte[] { (byte)char1 } } ; IExtraEncoding extra; extraEncodings.TryGetValue(encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture), out extra); if (extra != null) { byte[] b = extra.CharToByte(char1, encoding); if (b != null) { return(b); } } IntHashtable hash = null; if (encoding.Equals(BaseFont.WINANSI)) { hash = winansi; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { hash = pdfEncoding; } if (hash != null) { int c = 0; if (char1 < 128 || (char1 > 160 && char1 <= 255)) { c = char1; } else { c = hash[char1]; } if (c != 0) { return new byte[] { (byte)c } } ; else { return(new byte[0]); } } Encoding encw = IanaEncodings.GetEncodingEncoding(encoding); byte[] preamble = encw.GetPreamble(); char[] text = new char[] { char1 }; if (preamble.Length == 0) { return(encw.GetBytes(text)); } byte[] encoded = encw.GetBytes(text); byte[] total = new byte[encoded.Length + preamble.Length]; Array.Copy(preamble, 0, total, 0, preamble.Length); Array.Copy(encoded, 0, total, preamble.Length, encoded.Length); return(total); }
/** * Converts a <CODE>string</CODE> to a </CODE>byte</CODE> array according * to the font's encoding. * @param text the <CODE>string</CODE> to be converted * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding */ public static byte[] ConvertToBytes(string text, string encoding) { if (text == null) { return(new byte[0]); } if (encoding == null || encoding.Length == 0) { int len = text.Length; byte[] b = new byte[len]; for (int k = 0; k < len; ++k) { b[k] = (byte)text[k]; } return(b); } IExtraEncoding extra; extraEncodings.TryGetValue(encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture), out extra); if (extra != null) { byte[] b = extra.CharToByte(text, encoding); if (b != null) { return(b); } } IntHashtable hash = null; if (encoding.Equals(BaseFont.CP1252)) { hash = winansi; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { hash = pdfEncoding; } if (hash != null) { char[] cc = text.ToCharArray(); int len = cc.Length; int ptr = 0; byte[] b = new byte[len]; int c = 0; for (int k = 0; k < len; ++k) { char char1 = cc[k]; if (char1 < 128 || (char1 > 160 && char1 <= 255)) { c = char1; } else { c = hash[char1]; } if (c != 0) { b[ptr++] = (byte)c; } } if (ptr == len) { return(b); } byte[] b2 = new byte[ptr]; Array.Copy(b, 0, b2, 0, ptr); return(b2); } Encoding encw = IanaEncodings.GetEncodingEncoding(encoding); byte[] preamble = encw.GetPreamble(); if (preamble.Length == 0) { return(encw.GetBytes(text)); } byte[] encoded = encw.GetBytes(text); byte[] total = new byte[encoded.Length + preamble.Length]; Array.Copy(preamble, 0, total, 0, preamble.Length); Array.Copy(encoded, 0, total, preamble.Length, encoded.Length); return(total); }
/** * Exports the destinations to XML. The DTD for this XML is: * <p> * <pre> * <?xml version='1.0' encoding='UTF-8'?> * <!ELEMENT Name (#PCDATA)> * <!ATTLIST Name * Page CDATA #IMPLIED * > * <!ELEMENT Destination (Name)*> * </pre> * @param names the names * @param outp the export destination. The stream is not closed * @param encoding the encoding according to IANA conventions * @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>, * whatever the encoding * @throws IOException on error */ public static void ExportToXML(Dictionary <string, string> names, Stream outp, String encoding, bool onlyASCII) { StreamWriter wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXML(names, wrt, encoding, onlyASCII); }
/** * Exports the bookmarks to XML. The DTD for this XML is: * <p> * <pre> * <?xml version='1.0' encoding='UTF-8'?> * <!ELEMENT Title (#PCDATA|Title)*> * <!ATTLIST Title * Action CDATA #IMPLIED * Open CDATA #IMPLIED * Page CDATA #IMPLIED * URI CDATA #IMPLIED * File CDATA #IMPLIED * Named CDATA #IMPLIED * NamedN CDATA #IMPLIED * NewWindow CDATA #IMPLIED * Style CDATA #IMPLIED * Color CDATA #IMPLIED * > * <!ELEMENT Bookmark (Title)*> * </pre> * @param list the bookmarks * @param out the export destination. The stream is not closed * @param encoding the encoding according to IANA conventions * @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>, * whatever the encoding * @throws IOException on error */ public static void ExportToXML(IList <Dictionary <String, Object> > list, Stream outp, String encoding, bool onlyASCII) { StreamWriter wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXML(list, wrt, encoding, onlyASCII); }
/// <summary> /// Exports the bookmarks to XML. The DTD for this XML is: /// /// /// <?xml version='1.0' encoding='UTF-8'?> /// <!ELEMENT Title (#PCDATA|Title)*> /// <!ATTLIST Title /// Action CDATA #IMPLIED /// Open CDATA #IMPLIED /// Page CDATA #IMPLIED /// URI CDATA #IMPLIED /// File CDATA #IMPLIED /// Named CDATA #IMPLIED /// NamedN CDATA #IMPLIED /// NewWindow CDATA #IMPLIED /// Style CDATA #IMPLIED /// Color CDATA #IMPLIED /// > /// <!ELEMENT Bookmark (Title)*> /// /// whatever the encoding /// @throws IOException on error /// </summary> /// <param name="list">the bookmarks</param> /// <param name="outp">the export destination. The stream is not closed</param> /// <param name="encoding">the encoding according to IANA conventions</param> /// <param name="onlyAscii">codes above 127 will always be escaped with &#nn; if true ,</param> public static void ExportToXml(ArrayList list, Stream outp, string encoding, bool onlyAscii) { var wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXml(list, wrt, encoding, onlyAscii); }
public static string ConvertToString(byte[] bytes, string encoding) { if (bytes == null) { return(PdfObject.NOTHING); } if (string.IsNullOrEmpty(encoding)) { var c = new char[bytes.Length]; for (var k = 0; k < bytes.Length; ++k) { c[k] = (char)(bytes[k] & 0xff); } return(new string(c)); } var extra = (IExtraEncoding)ExtraEncodings[encoding.ToLowerInvariant()]; if (extra != null) { var text = extra.ByteToChar(bytes, encoding); if (text != null) { return(text); } } char[] ch = null; if (encoding.Equals(BaseFont.WINANSI)) { ch = WinansiByteToChar; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { ch = PdfEncodingByteToChar; } if (ch != null) { var len = bytes.Length; var c = new char[len]; for (var k = 0; k < len; ++k) { c[k] = ch[bytes[k] & 0xff]; } return(new string(c)); } var nameU = encoding.ToUpper(CultureInfo.InvariantCulture); var marker = false; var big = false; var offset = 0; if (bytes.Length >= 2) { if (bytes[0] == 254 && bytes[1] == 255) { marker = true; big = true; offset = 2; } else if (bytes[0] == 255 && bytes[1] == 254) { marker = true; big = false; offset = 2; } } Encoding enc = null; if (nameU.Equals("UNICODEBIGUNMARKED") || nameU.Equals("UNICODEBIG")) { enc = new UnicodeEncoding(marker ? big : true, false); } if (nameU.Equals("UNICODELITTLEUNMARKED") || nameU.Equals("UNICODELITTLE")) { enc = new UnicodeEncoding(marker ? big : false, false); } if (enc != null) { return(enc.GetString(bytes, offset, bytes.Length - offset)); } return(IanaEncodings.GetEncodingEncoding(encoding).GetString(bytes)); }
/// <summary> /// Converts a String to a byte array according /// to the font's encoding. /// </summary> /// <param name="encoding">the encoding</param> /// <param name="char1">the char to be converted</param> /// <returns>an array of byte representing the conversion according to the font's encoding</returns> public static byte[] ConvertToBytes(char char1, string encoding) { if (string.IsNullOrEmpty(encoding)) { return(new[] { (byte)char1 }); } var extra = (IExtraEncoding)ExtraEncodings[encoding.ToLowerInvariant()]; if (extra != null) { var b = extra.CharToByte(char1, encoding); if (b != null) { return(b); } } IntHashtable hash = null; if (encoding.Equals(BaseFont.WINANSI)) { hash = Winansi; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { hash = PdfEncoding; } if (hash != null) { var c = 0; if (char1 < 128 || (char1 > 160 && char1 <= 255)) { c = char1; } else { c = hash[char1]; } if (c != 0) { return(new[] { (byte)c }); } else { return(new byte[0]); } } var encw = IanaEncodings.GetEncodingEncoding(encoding); var preamble = encw.GetPreamble(); char[] text = { char1 }; if (preamble.Length == 0) { return(encw.GetBytes(text)); } var encoded = encw.GetBytes(text); var total = new byte[encoded.Length + preamble.Length]; Array.Copy(preamble, 0, total, 0, preamble.Length); Array.Copy(encoded, 0, total, preamble.Length, encoded.Length); return(total); }
/// <summary> /// Converts a string to a byte array according /// to the font's encoding. /// </summary> /// <param name="text">the string to be converted</param> /// <param name="encoding"></param> /// <returns>an array of byte representing the conversion according to the font's encoding</returns> public static byte[] ConvertToBytes(string text, string encoding) { if (text == null) { return(new byte[0]); } if (string.IsNullOrEmpty(encoding)) { var len = text.Length; var b = new byte[len]; for (var k = 0; k < len; ++k) { b[k] = (byte)text[k]; } return(b); } var extra = (IExtraEncoding)ExtraEncodings[encoding.ToLowerInvariant()]; if (extra != null) { var b = extra.CharToByte(text, encoding); if (b != null) { return(b); } } IntHashtable hash = null; if (encoding.Equals(BaseFont.CP1252)) { hash = Winansi; } else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING)) { hash = PdfEncoding; } if (hash != null) { var cc = text.ToCharArray(); var len = cc.Length; var ptr = 0; var b = new byte[len]; var c = 0; for (var k = 0; k < len; ++k) { var char1 = cc[k]; if (char1 < 128 || (char1 > 160 && char1 <= 255)) { c = char1; } else { c = hash[char1]; } if (c != 0) { b[ptr++] = (byte)c; } } if (ptr == len) { return(b); } var b2 = new byte[ptr]; Array.Copy(b, 0, b2, 0, ptr); return(b2); } var encw = IanaEncodings.GetEncodingEncoding(encoding); var preamble = encw.GetPreamble(); if (preamble.Length == 0) { return(encw.GetBytes(text)); } var encoded = encw.GetBytes(text); var total = new byte[encoded.Length + preamble.Length]; Array.Copy(preamble, 0, total, 0, preamble.Length); Array.Copy(encoded, 0, total, preamble.Length, encoded.Length); return(total); }
/** * Exports the bookmarks to XML. The DTD for this XML is: * <p> * <pre> * <?xml version='1.0' encoding='UTF-8'?> * <!ELEMENT Title (#PCDATA|Title)*> * <!ATTLIST Title * Action CDATA #IMPLIED * Open CDATA #IMPLIED * Page CDATA #IMPLIED * URI CDATA #IMPLIED * File CDATA #IMPLIED * Named CDATA #IMPLIED * NamedN CDATA #IMPLIED * NewWindow CDATA #IMPLIED * Style CDATA #IMPLIED * Color CDATA #IMPLIED * > * <!ELEMENT Bookmark (Title)*> * </pre> * @param list the bookmarks * @param out the export destination. The stream is not closed * @param encoding the encoding according to IANA conventions * @param onlyASCII codes above 127 will always be escaped with &#nn; if <CODE>true</CODE>, * whatever the encoding * @throws IOException on error */ public static void ExportToXML(ArrayList list, Stream outp, String encoding, bool onlyASCII) { StreamWriter wrt = new StreamWriter(outp, IanaEncodings.GetEncodingEncoding(encoding)); ExportToXML(list, wrt, encoding, onlyASCII); }