/// <summary> /// Convert the unicode string and actual image byte data to bytes /// These bytes are then written to the file to create PDF image details /// Output is formed by concatenating the first part of the object plus the actual byte data for the jpg /// plus the closing tag of the object /// </summary> protected byte[] GetImageBytes(String startStr, String endStr, byte[] ImageByteStream, long filePos, out int size) { ObjectList objList = new ObjectList(objectNum, filePos); byte[] s; byte[] e; try { //turn the start and end sections of image object to bytes Encoding enc = Encoding.GetEncoding(1252); s = Encoding.Unicode.GetBytes(startStr); s = Encoding.Convert(Encoding.Unicode, enc, s); e = Encoding.Unicode.GetBytes(endStr); e = Encoding.Convert(Encoding.Unicode, enc, e); XrefEntries.offsetArray.Add(objList); //get the length of the byte stream to create a new byte for concatenation size = s.Length + ImageByteStream.Length + e.Length; } catch (Exception ex) { string str1 = string.Format("{0},In PdfObjects.GetImageBytes()", objectNum); Exception error = new Exception(ex.Message + str1); throw error; } byte[] abuf = new byte[size]; try { int count = 0; int i = 0; //create the bytes for the image object up to and including the 'stream' command. while (count < s.Length) { abuf[i] = s[count]; count++; i++; } //add the bytes that represent the actual image data count = 0; while (count < ImageByteStream.Length) { abuf[i] = ImageByteStream[count]; count++; i++; } //add the 'endstream' and 'endobj' commands count = 0; while (count < e.Length) { abuf[i] = e[count]; count++; i++; } } catch (Exception ex1) { string str2 = string.Format("{0},In PdfObjects.GetImageBytes()", objectNum); Exception error = new Exception(ex1.Message + str2); throw error; } return abuf; }
/// <summary> /// Convert the unicode string 16 bits to unicode bytes. /// These bytes are then written to the file to create PDF details. /// This is used for general parts of the pdf. Other similar pieces of code return other byte streams for images etc /// </summary> /// <param name="str"></param> /// <returns></returns> protected byte[] GetUTF8Bytes(string str, long filePos, out int size) { ObjectList objList = new ObjectList(objectNum, filePos); byte[] abuf; try { byte[] ubuf = Encoding.Unicode.GetBytes(str); Encoding enc = Encoding.GetEncoding(1252); abuf = Encoding.Convert(Encoding.Unicode, enc, ubuf); size = abuf.Length; XrefEntries.offsetArray.Add(objList); } catch (Exception e) { string str1 = string.Format("{0},In PdfObjects.GetBytes()", objectNum); Exception error = new Exception(e.Message + str1); throw error; } return abuf; }
/// <summary> /// Creates the xref table using the byte offsets in the array. /// </summary> /// <param name="array"></param> /// <param name="size"></param> /// <returns></returns> public byte[] CreateXrefTable(long fileOffset, out int size) { //Store the Offset of the Xref table for startxRef try { ObjectList objList = new ObjectList(0, fileOffset); XrefEntries.offsetArray.Add(objList); XrefEntries.offsetArray.Sort(); numTableEntries = (uint)XrefEntries.offsetArray.Count; table = string.Format("xref\r\n{0} {1}\r\n0000000000 65535 f\r\n", 0, numTableEntries); for (int entries = 1; entries < numTableEntries; entries++) { ObjectList obj = (ObjectList)XrefEntries.offsetArray[entries]; table += obj.offset.ToString().PadLeft(10, '0'); table += " 00000 n\r\n"; } } catch (Exception e) { Exception error = new Exception(e.Message + " In Utility.CreateXrefTable()"); throw error; } return GetUTF8Bytes(table, out size); }