AddToBody() public méthode

public AddToBody ( PdfObject objecta ) : PdfIndirectObject
objecta PdfObject
Résultat PdfIndirectObject
Exemple #1
0
 internal override void WriteFont(PdfWriter writer, PdfIndirectReference piRef, Object[] oParams)
 {
     if (this.writer != writer)
         throw new ArgumentException("Type3 font used with the wrong PdfWriter");
     if (char2byte.Size != widths3.Size)
         throw new DocumentException("Not all the glyphs in the Type3 font are defined");
     IntHashtable inv = new IntHashtable();
     for (IntHashtable.IntHashtableIterator it = char2byte.GetEntryIterator(); it.HasNext();) {
         IntHashtable.IntHashtableEntry entry = it.Next();
         inv[entry.Value] = entry.Key;
     }
     int[] invOrd = inv.ToOrderedKeys();
     int firstChar = invOrd[0];
     int lastChar = invOrd[invOrd.Length - 1];
     int[] widths = new int[lastChar - firstChar + 1];
     for (int k = 0; k < widths.Length; ++k) {
         if (inv.ContainsKey(k + firstChar))
             widths[k] = widths3[inv[k + firstChar]];
     }
     PdfArray diffs = new PdfArray();
     PdfDictionary charprocs = new PdfDictionary();
     int last = -1;
     for (int k = 0; k < invOrd.Length; ++k) {
         int c = invOrd[k];
         if (c > last) {
             last = c;
             diffs.Add(new PdfNumber(last));
         }
         ++last;
         int c2 = inv[c];
         String s = GlyphList.UnicodeToName(c2);
         if (s == null)
             s = "a" + c2;
         PdfName n = new PdfName(s);
         diffs.Add(n);
         Type3Glyph glyph = (Type3Glyph)char2glyph[(char)c2];
         PdfStream stream = new PdfStream(glyph.ToPdf(null));
         stream.FlateCompress();
         PdfIndirectReference refp = writer.AddToBody(stream).IndirectReference;
         charprocs.Put(n, refp);
     }
     PdfDictionary font = new PdfDictionary(PdfName.FONT);
     font.Put(PdfName.SUBTYPE, PdfName.TYPE3);
     if (colorized)
         font.Put(PdfName.FONTBBOX, new PdfRectangle(0, 0, 0, 0));
     else
         font.Put(PdfName.FONTBBOX, new PdfRectangle(llx, lly, urx, ury));
     font.Put(PdfName.FONTMATRIX, new PdfArray(new float[]{0.001f, 0, 0, 0.001f, 0, 0}));
     font.Put(PdfName.CHARPROCS, writer.AddToBody(charprocs).IndirectReference);
     PdfDictionary encoding = new PdfDictionary();
     encoding.Put(PdfName.DIFFERENCES, diffs);
     font.Put(PdfName.ENCODING, writer.AddToBody(encoding).IndirectReference);
     font.Put(PdfName.FIRSTCHAR, new PdfNumber(firstChar));
     font.Put(PdfName.LASTCHAR, new PdfNumber(lastChar));
     font.Put(PdfName.WIDTHS, writer.AddToBody(new PdfArray(widths)).IndirectReference);
     if (pageResources.HasResources())
         font.Put(PdfName.RESOURCES, writer.AddToBody(pageResources.Resources).IndirectReference);
     writer.AddToBody(font, piRef);
 }
 public static PdfDictionary OutputNamedDestinationAsStrings(Dictionary<string,string> names, PdfWriter writer)
 {
     Dictionary<string,PdfObject> n2 = new Dictionary<string,PdfObject>(names.Count);
     foreach (String key in names.Keys) {
         try {
             String value = names[key];
             PdfArray ar = CreateDestinationArray(value, writer);
             n2[key] = writer.AddToBody(ar).IndirectReference;
         }
         catch {
             // empty on purpose
         }
     }
     return PdfNameTree.WriteTree(n2, writer);
 }
Exemple #3
0
        /**
        * Creates a file specification with the file embedded. The file may
        * come from the file system or from a byte array.
        * @param writer the <CODE>PdfWriter</CODE>
        * @param filePath the file path
        * @param fileDisplay the file information that is presented to the user
        * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
        * it takes precedence over <CODE>filePath</CODE>
        * @param mimeType the optional mimeType
        * @param fileParameter the optional extra file parameters such as the creation or modification date
        * @param compressionLevel the level of compression
        * @throws IOException on error
        * @return the file specification
        * @since   2.1.3
        */    
        public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) {
            PdfFileSpecification fs = new PdfFileSpecification();
            fs.writer = writer;
            fs.Put(PdfName.F, new PdfString(fileDisplay));
            fs.SetUnicodeFileName(fileDisplay, false);
            PdfEFStream stream;
            Stream inp = null;
            PdfIndirectReference refi;
            PdfIndirectReference refFileLength = null;
            try {
                if (fileStore == null) {
                    refFileLength = writer.PdfIndirectReference;
                    if (File.Exists(filePath)) {
                        inp = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    }
                    else {
                        if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) {
                            WebRequest wr = WebRequest.Create(filePath);
                            wr.Credentials = CredentialCache.DefaultCredentials;
                            inp = wr.GetResponse().GetResponseStream();
                        }
                        else {
                            inp = StreamUtil.GetResourceStream(filePath);
                            if (inp == null)
                                throw new IOException(MessageLocalization.GetComposedMessage("1.not.found.as.file.or.resource", filePath));
                        }
                    }
                    stream = new PdfEFStream(inp, writer);
                }
                else
                    stream = new PdfEFStream(fileStore);
                stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
                stream.FlateCompress(compressionLevel);

                PdfDictionary param = new PdfDictionary();
                if (fileParameter != null)
                    param.Merge(fileParameter);
                if (!param.Contains(PdfName.MODDATE)) {
                    param.Put(PdfName.MODDATE, new PdfDate());
                }
                if (fileStore != null) {
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    stream.Put(PdfName.PARAMS, param);
                }
                else {
                    stream.Put(PdfName.PARAMS, refFileLength);
                }

                if (mimeType != null)
                    stream.Put(PdfName.SUBTYPE, new PdfName(mimeType));

                refi = writer.AddToBody(stream).IndirectReference;
                if (fileStore == null) {
                    stream.WriteLength();
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    writer.AddToBody(param, refFileLength);
                }
            }
            finally {
                if (inp != null)
                    try{inp.Close();}catch{}
            }
            PdfDictionary f = new PdfDictionary();
            f.Put(PdfName.F, refi);
            f.Put(PdfName.UF, refi);
            fs.Put(PdfName.EF, f);
            return fs;
        }
Exemple #4
0
 /**
 * Adds the names of the named destinations to the catalog.
 * @param localDestinations the local destinations
 * @param documentJavaScript the javascript used in the document
 * @param writer the writer the catalog applies to
 */
 internal void AddNames(OrderedTree localDestinations, Hashtable documentLevelJS, Hashtable documentFileAttachment, PdfWriter writer) {
     if (localDestinations.Count == 0 && documentLevelJS.Count == 0 && documentFileAttachment.Count == 0)
         return;
     PdfDictionary names = new PdfDictionary();
     if (localDestinations.Count > 0) {
         PdfArray ar = new PdfArray();
         foreach (String name in localDestinations.Keys) {
             Object[] obj = (Object[])localDestinations[name];
             if (obj[2] == null) //no destination
                 continue;
             PdfIndirectReference refi = (PdfIndirectReference)obj[1];
             ar.Add(new PdfString(name, null));
             ar.Add(refi);
         }
         if (ar.Size > 0) {
             PdfDictionary dests = new PdfDictionary();
             dests.Put(PdfName.NAMES, ar);
             names.Put(PdfName.DESTS, writer.AddToBody(dests).IndirectReference);
         }
     }
     if (documentLevelJS.Count > 0) {
         PdfDictionary tree = PdfNameTree.WriteTree(documentLevelJS, writer);
         names.Put(PdfName.JAVASCRIPT, writer.AddToBody(tree).IndirectReference);
     }
     if (documentFileAttachment.Count > 0) {
         names.Put(PdfName.EMBEDDEDFILES, writer.AddToBody(PdfNameTree.WriteTree(documentFileAttachment, writer)).IndirectReference);
     }
     if (names.Size > 0)
         Put(PdfName.NAMES, writer.AddToBody(names).IndirectReference);
 }
Exemple #5
0
 /** Outputs to the writer the font dictionaries and streams.
  * @param writer the writer for this document
  * @param ref the font indirect reference
  * @param parms several parameters that depend on the font type
  * @throws IOException on error
  * @throws DocumentException error in generating the object
  */
 internal override void WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms) {
     int firstChar = (int)parms[0];
     int lastChar = (int)parms[1];
     byte[] shortTag = (byte[])parms[2];
     bool subsetp = (bool)parms[3] && subset;
     if (!subsetp) {
         firstChar = 0;
         lastChar = shortTag.Length - 1;
         for (int k = 0; k < shortTag.Length; ++k)
             shortTag[k] = 1;
     }
     PdfIndirectReference ind_font = null;
     PdfObject pobj = null;
     PdfIndirectObject obj = null;
     pobj = GetFullFontStream();
     if (pobj != null){
         obj = writer.AddToBody(pobj);
         ind_font = obj.IndirectReference;
     }
     pobj = GetFontDescriptor(ind_font);
     if (pobj != null){
         obj = writer.AddToBody(pobj);
         ind_font = obj.IndirectReference;
     }
     pobj = GetFontBaseType(ind_font, firstChar, lastChar, shortTag);
     writer.AddToBody(pobj, piref);
 }
        internal override void WriteFont(PdfWriter writer, PdfIndirectReference piRef, Object[] oParams)
        {
            if (this.writer != writer)
                throw new ArgumentException(MessageLocalization.GetComposedMessage("type3.font.used.with.the.wrong.pdfwriter"));
            // Get first & lastchar ...
            int firstChar = 0;
            while( firstChar < usedSlot.Length && !usedSlot[firstChar] ) firstChar++;

            if ( firstChar == usedSlot.Length ) {
                throw new DocumentException(MessageLocalization.GetComposedMessage("no.glyphs.defined.for.type3.font"));
            }
            int lastChar = usedSlot.Length - 1;
            while( lastChar >= firstChar && !usedSlot[lastChar] ) lastChar--;

            int[] widths = new int[lastChar - firstChar + 1];
            int[] invOrd = new int[lastChar - firstChar + 1];

            int invOrdIndx = 0, w = 0;
            for( int u = firstChar; u<=lastChar; u++, w++ ) {
                if ( usedSlot[u] ) {
                    invOrd[invOrdIndx++] = u;
                    widths[w] = widths3[u];
                }
            }
            PdfArray diffs = new PdfArray();
            PdfDictionary charprocs = new PdfDictionary();
            int last = -1;
            for (int k = 0; k < invOrdIndx; ++k) {
                int c = invOrd[k];
                if (c > last) {
                    last = c;
                    diffs.Add(new PdfNumber(last));
                }
                ++last;
                int c2 = invOrd[k];
                String s = GlyphList.UnicodeToName(c2);
                if (s == null)
                    s = "a" + c2;
                PdfName n = new PdfName(s);
                diffs.Add(n);
                Type3Glyph glyph;
                char2glyph.TryGetValue((char)c2, out glyph);
                PdfStream stream = new PdfStream(glyph.ToPdf(null));
                stream.FlateCompress(compressionLevel);
                PdfIndirectReference refp = writer.AddToBody(stream).IndirectReference;
                charprocs.Put(n, refp);
            }
            PdfDictionary font = new PdfDictionary(PdfName.FONT);
            font.Put(PdfName.SUBTYPE, PdfName.TYPE3);
            if (colorized)
                font.Put(PdfName.FONTBBOX, new PdfRectangle(0, 0, 0, 0));
            else
                font.Put(PdfName.FONTBBOX, new PdfRectangle(llx, lly, urx, ury));
            font.Put(PdfName.FONTMATRIX, new PdfArray(new float[]{0.001f, 0, 0, 0.001f, 0, 0}));
            font.Put(PdfName.CHARPROCS, writer.AddToBody(charprocs).IndirectReference);
            PdfDictionary encoding = new PdfDictionary();
            encoding.Put(PdfName.DIFFERENCES, diffs);
            font.Put(PdfName.ENCODING, writer.AddToBody(encoding).IndirectReference);
            font.Put(PdfName.FIRSTCHAR, new PdfNumber(firstChar));
            font.Put(PdfName.LASTCHAR, new PdfNumber(lastChar));
            font.Put(PdfName.WIDTHS, writer.AddToBody(new PdfArray(widths)).IndirectReference);
            if (pageResources.HasResources())
                font.Put(PdfName.RESOURCES, writer.AddToBody(pageResources.Resources).IndirectReference);
            writer.AddToBody(font, piRef);
        }
 public static Object[] IterateOutlines(PdfWriter writer, PdfIndirectReference parent, IList<Dictionary<String, Object>> kids, bool namedAsNames) {
     PdfIndirectReference[] refs = new PdfIndirectReference[kids.Count];
     for (int k = 0; k < refs.Length; ++k)
         refs[k] = writer.PdfIndirectReference;
     int ptr = 0;
     int count = 0;
     foreach (Dictionary<String, Object> map in kids) {
         Object[] lower = null;
         IList<Dictionary<String, Object>> subKid = null;
         if (map.ContainsKey("Kids"))
             subKid = (IList<Dictionary<String, Object>>)map["Kids"];
         if (subKid != null && subKid.Count > 0)
             lower = IterateOutlines(writer, refs[ptr], subKid, namedAsNames);
         PdfDictionary outline = new PdfDictionary();
         ++count;
         if (lower != null) {
             outline.Put(PdfName.FIRST, (PdfIndirectReference)lower[0]);
             outline.Put(PdfName.LAST, (PdfIndirectReference)lower[1]);
             int n = (int)lower[2];
             if (map.ContainsKey("Open") && "false".Equals(map["Open"])) {
                 outline.Put(PdfName.COUNT, new PdfNumber(-n));
             }
             else {
                 outline.Put(PdfName.COUNT, new PdfNumber(n));
                 count += n;
             }
         }
         outline.Put(PdfName.PARENT, parent);
         if (ptr > 0)
             outline.Put(PdfName.PREV, refs[ptr - 1]);
         if (ptr < refs.Length - 1)
             outline.Put(PdfName.NEXT, refs[ptr + 1]);
         outline.Put(PdfName.TITLE, new PdfString((String)map["Title"], PdfObject.TEXT_UNICODE));
         String color = null;
         if (map.ContainsKey("Color"))
             color = (String)map["Color"];
         if (color != null) {
             try {
                 PdfArray arr = new PdfArray();
                 StringTokenizer tk = new StringTokenizer(color);
                 for (int k = 0; k < 3; ++k) {
                     float f = float.Parse(tk.NextToken(), System.Globalization.NumberFormatInfo.InvariantInfo);
                     if (f < 0) f = 0;
                     if (f > 1) f = 1;
                     arr.Add(new PdfNumber(f));
                 }
                 outline.Put(PdfName.C, arr);
             } catch {} //in case it's malformed
         }
         String style = GetVal(map, "Style");
         if (style != null) {
             style = style.ToLower(System.Globalization.CultureInfo.InvariantCulture);
             int bits = 0;
             if (style.IndexOf("italic") >= 0)
                 bits |= 1;
             if (style.IndexOf("bold") >= 0)
                 bits |= 2;
             if (bits != 0)
                 outline.Put(PdfName.F, new PdfNumber(bits));
         }
         CreateOutlineAction(outline, map, writer, namedAsNames);
         writer.AddToBody(outline, refs[ptr]);
         ++ptr;
     }
     return new Object[]{refs[0], refs[refs.Length - 1], count};
 }
        /** Outputs to the writer the font dictionaries and streams.
         * @param writer the writer for this document
         * @param ref the font indirect reference
         * @param parms several parameters that depend on the font type
         * @throws IOException on error
         * @throws DocumentException error in generating the object
         */
        internal override void WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms)
        {
            Hashtable longTag = (Hashtable)parms[0];
            AddRangeUni(longTag, true, subset);
            ArrayList tmp = new ArrayList();
            foreach (object o in longTag.Values) {
                tmp.Add(o);
            }
            Object[] metrics = tmp.ToArray();
            Array.Sort(metrics, this);
            PdfIndirectReference ind_font = null;
            PdfObject pobj = null;
            PdfIndirectObject obj = null;
            // sivan: cff
            if (cff) {
                RandomAccessFileOrArray rf2 = new RandomAccessFileOrArray(rf);
                byte[] b = new byte[cffLength];
                try {
                    rf2.ReOpen();
                    rf2.Seek(cffOffset);
                    rf2.ReadFully(b);
                } finally {
                    try {
                        rf2.Close();
                    } catch  {
                        // empty on purpose
                    }
                }
                if (subset || subsetRanges != null) {
                    CFFFontSubset cffs = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag);
                    b = cffs.Process( (cffs.GetNames())[0] );
                }

                pobj = new StreamFont(b, "CIDFontType0C");
                obj = writer.AddToBody(pobj);
                ind_font = obj.IndirectReference;
            } else {
                byte[] b;
                if (subset || directoryOffset != 0) {
                    TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), longTag, directoryOffset, false, false);
                    b = sb.Process();
                }
                else {
                    b = GetFullFont();
                }
                int[] lengths = new int[]{b.Length};
                pobj = new StreamFont(b, lengths);
                obj = writer.AddToBody(pobj);
                ind_font = obj.IndirectReference;
            }
            String subsetPrefix = "";
            if (subset)
                subsetPrefix = CreateSubsetPrefix();
            PdfDictionary dic = GetFontDescriptor(ind_font, subsetPrefix);
            obj = writer.AddToBody(dic);
            ind_font = obj.IndirectReference;

            pobj = GetCIDFontType2(ind_font, subsetPrefix, metrics);
            obj = writer.AddToBody(pobj);
            ind_font = obj.IndirectReference;

            pobj = GetToUnicode(metrics);
            PdfIndirectReference toUnicodeRef = null;
            if (pobj != null) {
                obj = writer.AddToBody(pobj);
                toUnicodeRef = obj.IndirectReference;
            }

            pobj = GetFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
            writer.AddToBody(pobj, piref);
        }
Exemple #9
0
 /** Creates a JavaScript action. If the JavaScript is smaller than
  * 50 characters it will be placed as a string, otherwise it will
  * be placed as a compressed stream.
  * @param code the JavaScript code
  * @param writer the writer for this action
  * @param unicode select JavaScript unicode. Note that the internal
  * Acrobat JavaScript engine does not support unicode,
  * so this may or may not work for you
  * @return the JavaScript action
  */    
 public static PdfAction JavaScript(string code, PdfWriter writer, bool unicode) {
     PdfAction js = new PdfAction();
     js.Put(PdfName.S, PdfName.JAVASCRIPT);
     if (unicode && code.Length < 50) {
         js.Put(PdfName.JS, new PdfString(code, PdfObject.TEXT_UNICODE));
     }
     else if (!unicode && code.Length < 100) {
         js.Put(PdfName.JS, new PdfString(code));
     }
     else {
         try {
             byte[] b = PdfEncodings.ConvertToBytes(code, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING);
             PdfStream stream = new PdfStream(b);
             stream.FlateCompress(writer.CompressionLevel);
             js.Put(PdfName.JS, writer.AddToBody(stream).IndirectReference);
         }
         catch {
             js.Put(PdfName.JS, new PdfString(code));
         }
     }
     return js;
 }
Exemple #10
0
 virtual public PdfArray RotateAnnotations(PdfWriter writer, Rectangle pageSize) {
     PdfArray array = new PdfArray();
     int rotation = pageSize.Rotation % 360;
     int currentPage = writer.CurrentPageNumber;
     for (int k = 0; k < annotations.Count; ++k) {
         PdfAnnotation dic = annotations[k];
         int page = dic.PlaceInPage;
         if (page > currentPage) {
             delayedAnnotations.Add(dic);
             continue;
         }
         if (dic.IsForm()) {
             if (!dic.IsUsed()) {
                 Dictionary<PdfTemplate,object> templates = dic.Templates;
                 if (templates != null)
                     acroForm.AddFieldTemplates(templates);
             }
             PdfFormField field = (PdfFormField)dic;
             if (field.Parent == null)
                 acroForm.AddDocumentField(field.IndirectReference);
         }
         if (dic.IsAnnotation()) {
             array.Add(dic.IndirectReference);
             if (!dic.IsUsed()) {
                 PdfArray tmp = dic.GetAsArray(PdfName.RECT);
                 PdfRectangle rect;
                 if (tmp.Size == 4)
                 {
                     rect = new PdfRectangle(tmp.GetAsNumber(0).FloatValue, tmp.GetAsNumber(1).FloatValue, tmp.GetAsNumber(2).FloatValue, tmp.GetAsNumber(3).FloatValue);
                 } else {
                     rect = new PdfRectangle(tmp.GetAsNumber(0).FloatValue, tmp.GetAsNumber(1).FloatValue);
                 }
                 if (rect != null) {
                     switch (rotation) {
                         case 90:
                             dic.Put(PdfName.RECT, new PdfRectangle(
                                     pageSize.Top - rect.Bottom,
                                     rect.Left,
                                     pageSize.Top - rect.Top,
                                     rect.Right));
                             break;
                         case 180:
                             dic.Put(PdfName.RECT, new PdfRectangle(
                                     pageSize.Right - rect.Left,
                                     pageSize.Top - rect.Bottom,
                                     pageSize.Right - rect.Right,
                                     pageSize.Top - rect.Top));
                             break;
                         case 270:
                             dic.Put(PdfName.RECT, new PdfRectangle(
                                     rect.Bottom,
                                     pageSize.Right - rect.Left,
                                     rect.Top,
                                     pageSize.Right - rect.Right));
                             break;
                     }
                 }
             }
         }
         if (!dic.IsUsed()) {
             dic.SetUsed();
             writer.AddToBody(dic, dic.IndirectReference);
         }
     }
     return array;
 }
 public static PdfDictionary OutputNamedDestinationAsStrings(Hashtable names, PdfWriter writer) {
     Hashtable n2 = new Hashtable();
     foreach (String key in names.Keys) {
         try {
             String value = (String)names[key];
             PdfArray ar = CreateDestinationArray(value, writer);
             n2[key] = writer.AddToBody(ar).IndirectReference;
         }
         catch {
             // empty on purpose
         }
     }
     return PdfNameTree.WriteTree(n2, writer);
 }
Exemple #12
0
 /**
 * Sets the XFA key from a byte array. The old XFA is erased.
 * @param xfaData the data
 * @param reader the reader
 * @param writer the writer
 * @throws java.io.IOException on error
 */
 public static void SetXfa(byte[] xfaData, PdfReader reader, PdfWriter writer)
 {
     PdfDictionary af = (PdfDictionary)PdfReader.GetPdfObjectRelease(reader.Catalog.Get(PdfName.ACROFORM));
     if (af == null) {
         return;
     }
     reader.KillXref(af.Get(PdfName.XFA));
     PdfStream str = new PdfStream(xfaData);
     str.FlateCompress();
     PdfIndirectReference refe = writer.AddToBody(str).IndirectReference;
     af.Put(PdfName.XFA, refe);
 }
        /** Outputs to the writer the font dictionaries and streams.
         * @param writer the writer for this document
         * @param ref the font indirect reference
         * @param parms several parameters that depend on the font type
         * @throws IOException on error
         * @throws DocumentException error in generating the object
         */
        internal override void WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms) {
            Hashtable longTag = (Hashtable)parms[0];
            AddRangeUni(longTag, true, subset);
            ArrayList tmp = new ArrayList();
            foreach (object o in longTag.Values) {
                tmp.Add(o);
            }
            Object[] metrics = tmp.ToArray();
            Array.Sort(metrics, this);
            PdfIndirectReference ind_font = null;
            PdfObject pobj = null;
            PdfIndirectObject obj = null;
            PdfIndirectReference cidset = null;
            if (writer.PDFXConformance == PdfWriter.PDFA1A || writer.PDFXConformance == PdfWriter.PDFA1B) {
                PdfStream stream;
                if (metrics.Length == 0) {
                    stream = new PdfStream(new byte[]{(byte)0x80});
                }
                else {
                    int top = ((int[])metrics[metrics.Length - 1])[0];
                    byte[] bt = new byte[top / 8 + 1];
                    for (int k = 0; k < metrics.Length; ++k) {
                        int v = ((int[])metrics[k])[0];
                        bt[v / 8] |= rotbits[v % 8];
                    }
                    stream = new PdfStream(bt);
                    stream.FlateCompress(compressionLevel);
                }
                cidset = writer.AddToBody(stream).IndirectReference;
            }
            // sivan: cff
            if (cff) {
                byte[] b = ReadCffFont();
                if (subset || subsetRanges != null) {
                    CFFFontSubset cffs = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag);
                    b = cffs.Process((cffs.GetNames())[0] );
                }
                
                pobj = new StreamFont(b, "CIDFontType0C", compressionLevel);
                obj = writer.AddToBody(pobj);
                ind_font = obj.IndirectReference;
            } else {
                byte[] b;
                if (subset || directoryOffset != 0) {
                    TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), longTag, directoryOffset, false, false);
                    b = sb.Process();
                }
                else {
                    b = GetFullFont();
                }
                int[] lengths = new int[]{b.Length};
                pobj = new StreamFont(b, lengths, compressionLevel);
                obj = writer.AddToBody(pobj);
                ind_font = obj.IndirectReference;
            }
            String subsetPrefix = "";
            if (subset)
                subsetPrefix = CreateSubsetPrefix();
            PdfDictionary dic = GetFontDescriptor(ind_font, subsetPrefix, cidset);
            obj = writer.AddToBody(dic);
            ind_font = obj.IndirectReference;

            pobj = GetCIDFontType2(ind_font, subsetPrefix, metrics);
            obj = writer.AddToBody(pobj);
            ind_font = obj.IndirectReference;

            pobj = GetToUnicode(metrics);
            PdfIndirectReference toUnicodeRef = null;
            if (pobj != null) {
                obj = writer.AddToBody(pobj);
                toUnicodeRef = obj.IndirectReference;
            }

            pobj = GetFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
            writer.AddToBody(pobj, piref);
        }
 /**
 * Creates a screen PdfAnnotation
 * @param writer
 * @param rect
 * @param clipTitle
 * @param fs
 * @param mimeType
 * @param playOnDisplay
 * @return a screen PdfAnnotation
 * @throws IOException
 */
 public static PdfAnnotation CreateScreen(PdfWriter writer, Rectangle rect, String clipTitle, PdfFileSpecification fs,
                                         String mimeType, bool playOnDisplay)
 {
     PdfAnnotation ann = new PdfAnnotation(writer, rect);
     ann.Put(PdfName.SUBTYPE, PdfName.SCREEN);
     ann.Put (PdfName.F, new PdfNumber(FLAGS_PRINT));
     ann.Put(PdfName.TYPE, PdfName.ANNOT);
     ann.SetPage();
     PdfIndirectReference refi = ann.IndirectReference;
     PdfAction action = PdfAction.Rendition(clipTitle,fs,mimeType, refi);
     PdfIndirectReference actionRef = writer.AddToBody(action).IndirectReference;
     // for play on display add trigger event
     if (playOnDisplay)
     {
         PdfDictionary aa = new PdfDictionary();
         aa.Put(new PdfName("PV"), actionRef);
         ann.Put(PdfName.AA, aa);
     }
     ann.Put(PdfName.A, actionRef);
     return ann;
 }
Exemple #15
0
 /**
 * Creates a name tree.
 * @param items the item of the name tree. The key is a <CODE>String</CODE>
 * and the value is a <CODE>PdfObject</CODE>. Note that although the
 * keys are strings only the lower byte is used and no check is made for chars
 * with the same lower byte and different upper byte. This will generate a wrong
 * tree name.
 * @param writer the writer
 * @throws IOException on error
 * @return the dictionary with the name tree. This dictionary is the one
 * generally pointed to by the key /Dests, for example
 */    
 public static PdfDictionary WriteTree(Hashtable items, PdfWriter writer) {
     if (items.Count == 0)
         return null;
     String[] names = new String[items.Count];
     items.Keys.CopyTo(names, 0);
     Array.Sort(names, new CompareSrt());
     if (names.Length <= leafSize) {
         PdfDictionary dic = new PdfDictionary();
         PdfArray ar = new PdfArray();
         for (int k = 0; k < names.Length; ++k) {
             ar.Add(new PdfString(names[k], null));
             ar.Add((PdfObject)items[names[k]]);
         }
         dic.Put(PdfName.NAMES, ar);
         return dic;
     }
     int skip = leafSize;
     PdfIndirectReference[] kids = new PdfIndirectReference[(names.Length + leafSize - 1) / leafSize];
     for (int k = 0; k < kids.Length; ++k) {
         int offset = k * leafSize;
         int end = Math.Min(offset + leafSize, names.Length);
         PdfDictionary dic = new PdfDictionary();
         PdfArray arr = new PdfArray();
         arr.Add(new PdfString(names[offset], null));
         arr.Add(new PdfString(names[end - 1], null));
         dic.Put(PdfName.LIMITS, arr);
         arr = new PdfArray();
         for (; offset < end; ++offset) {
             arr.Add(new PdfString(names[offset], null));
             arr.Add((PdfObject)items[names[offset]]);
         }
         dic.Put(PdfName.NAMES, arr);
         kids[k] = writer.AddToBody(dic).IndirectReference;
     }
     int top = kids.Length;
     while (true) {
         if (top <= leafSize) {
             PdfArray arr = new PdfArray();
             for (int k = 0; k < top; ++k)
                 arr.Add(kids[k]);
             PdfDictionary dic = new PdfDictionary();
             dic.Put(PdfName.KIDS, arr);
             return dic;
         }
         skip *= leafSize;
         int tt = (names.Length + skip - 1 )/ skip;
         for (int k = 0; k < tt; ++k) {
             int offset = k * leafSize;
             int end = Math.Min(offset + leafSize, top);
             PdfDictionary dic = new PdfDictionary();
             PdfArray arr = new PdfArray();
             arr.Add(new PdfString(names[k * skip], null));
             arr.Add(new PdfString(names[Math.Min((k + 1) * skip, names.Length) - 1], null));
             dic.Put(PdfName.LIMITS, arr);
             arr = new PdfArray();
             for (; offset < end; ++offset) {
                 arr.Add(kids[offset]);
             }
             dic.Put(PdfName.KIDS, arr);
             kids[k] = writer.AddToBody(dic).IndirectReference;
         }
         top = tt;
     }
 }
Exemple #16
0
 internal override void WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms) {
     IntHashtable cjkTag = (IntHashtable)parms[0];
     PdfIndirectReference ind_font = null;
     PdfObject pobj = null;
     PdfIndirectObject obj = null;
     pobj = GetFontDescriptor();
     if (pobj != null){
         obj = writer.AddToBody(pobj);
         ind_font = obj.IndirectReference;
     }
     pobj = GetCIDFont(ind_font, cjkTag);
     if (pobj != null){
         obj = writer.AddToBody(pobj);
         ind_font = obj.IndirectReference;
     }
     pobj = GetFontBaseType(ind_font);
     writer.AddToBody(pobj, piref);
 }
 /**
 * Creates a file specification with the file embedded. The file may
 * come from the file system or from a byte array.
 * @param writer the <CODE>PdfWriter</CODE>
 * @param filePath the file path
 * @param fileDisplay the file information that is presented to the user
 * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
 * it takes precedence over <CODE>filePath</CODE>
 * @param mimeType the optional mimeType
 * @param fileParameter the optional extra file parameters such as the creation or modification date
 * @param compressionLevel the level of compression
 * @throws IOException on error
 * @return the file specification
 * @since   2.1.3
 */    
 public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) {
     PdfFileSpecification fs = new PdfFileSpecification();
     fs.writer = writer;
     fs.Put(PdfName.F, new PdfString(fileDisplay));
     PdfEFStream stream;
     Stream inp = null;
     PdfIndirectReference refi;
     PdfIndirectReference refFileLength;
     try {
         refFileLength = writer.PdfIndirectReference;
         if (fileStore == null) {
             if (File.Exists(filePath)) {
                 inp = new FileStream(filePath, FileMode.Open, FileAccess.Read);
             }
             else {
                 if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) {
                     WebRequest w = WebRequest.Create(filePath);
                     inp = w.GetResponse().GetResponseStream();
                 }
                 else {
                     inp = BaseFont.GetResourceStream(filePath);
                     if (inp == null)
                         throw new IOException(filePath + " not found as file or resource.");
                 }
             }
             stream = new PdfEFStream(inp, writer);
         }
         else
             stream = new PdfEFStream(fileStore);
         stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
         stream.FlateCompress(compressionLevel);
         stream.Put(PdfName.PARAMS, refFileLength);
         if (mimeType != null)
             stream.Put(PdfName.SUBTYPE, new PdfName(mimeType));
         refi = writer.AddToBody(stream).IndirectReference;
         if (fileStore == null) {
             stream.WriteLength();
         }
         PdfDictionary param = new PdfDictionary();
         if (fileParameter != null)
             param.Merge(fileParameter);
         param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
         writer.AddToBody(param, refFileLength);
     }
     finally {
         if (inp != null)
             try{inp.Close();}catch{}
     }
     PdfDictionary f = new PdfDictionary();
     f.Put(PdfName.F, refi);
     fs.Put(PdfName.EF, f);
     return fs;
 }
Exemple #18
0
 /**
 * Sets the XFA key from a byte array. The old XFA is erased.
 * @param form the data
 * @param reader the reader
 * @param writer the writer
 * @throws java.io.IOException on error
 */
 public static void SetXfa(XfaForm form, PdfReader reader, PdfWriter writer)
 {
     PdfDictionary af = (PdfDictionary)PdfReader.GetPdfObjectRelease(reader.Catalog.Get(PdfName.ACROFORM));
     if (af == null) {
         return;
     }
     PdfObject xfa = GetXfaObject(reader);
     if (xfa.IsArray()) {
         PdfArray ar = (PdfArray)xfa;
         int t = -1;
         int d = -1;
         for (int k = 0; k < ar.Size; k += 2) {
             PdfString s = ar.GetAsString(k);
             if ("template".Equals(s.ToString())) {
                 t = k + 1;
             }
             if ("datasets".Equals(s.ToString())) {
                 d = k + 1;
             }
         }
         if (t > -1 && d > -1) {
             reader.KillXref(ar.GetAsIndirectObject(t));
             reader.KillXref(ar.GetAsIndirectObject(d));
             PdfStream tStream = new PdfStream(SerializeDoc(form.templateNode));
             tStream.FlateCompress(writer.CompressionLevel);
             ar[t] = writer.AddToBody(tStream).IndirectReference;
             PdfStream dStream = new PdfStream(SerializeDoc(form.datasetsNode));
             dStream.FlateCompress(writer.CompressionLevel);
             ar[d] = writer.AddToBody(dStream).IndirectReference;
             af.Put(PdfName.XFA, new PdfArray(ar));
             return;
         }
     }
     reader.KillXref(af.Get(PdfName.XFA));
     PdfStream str = new PdfStream(SerializeDoc(form.domDocument));
     str.FlateCompress(writer.CompressionLevel);
     PdfIndirectReference refe = writer.AddToBody(str).IndirectReference;
     af.Put(PdfName.XFA, refe);
 }
Exemple #19
0
 /** Outputs to the writer the font dictionaries and streams.
  * @param writer the writer for this document
  * @param ref the font indirect reference
  * @param params several parameters that depend on the font type
  * @throws IOException on error
  * @throws DocumentException error in generating the object
  */
 internal override void WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms)
 {
     int firstChar = (int)parms[0];
     int lastChar = (int)parms[1];
     byte[] shortTag = (byte[])parms[2];
     bool subsetp = (bool)parms[3] && subset;
     if (!subsetp) {
         firstChar = 0;
         lastChar = shortTag.Length - 1;
         for (int k = 0; k < shortTag.Length; ++k)
             shortTag[k] = 1;
     }
     PdfIndirectReference ind_font = null;
     PdfObject pobj = null;
     PdfIndirectObject obj = null;
     string subsetPrefix = "";
     if (embedded) {
         if (cff) {
             pobj = new StreamFont(ReadCffFont(), "Type1C", compressionLevel);
             obj = writer.AddToBody(pobj);
             ind_font = obj.IndirectReference;
         }
         else {
             if (subsetp)
                 subsetPrefix = CreateSubsetPrefix();
             Hashtable glyphs = new Hashtable();
             for (int k = firstChar; k <= lastChar; ++k) {
                 if (shortTag[k] != 0) {
                     int[] metrics = null;
                     if (specialMap != null) {
                         int[] cd = GlyphList.NameToUnicode(differences[k]);
                         if (cd != null)
                             metrics = GetMetricsTT(cd[0]);
                     }
                     else {
                         if (fontSpecific)
                             metrics = GetMetricsTT(k);
                         else
                             metrics = GetMetricsTT(unicodeDifferences[k]);
                     }
                     if (metrics != null)
                         glyphs[metrics[0]] = null;
                 }
             }
             AddRangeUni(glyphs, false, subsetp);
             byte[] b = null;
             if (subsetp || directoryOffset != 0 || subsetRanges != null) {
                 TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), glyphs, directoryOffset, true, !subsetp);
                 b = sb.Process();
             }
             else {
                 b = GetFullFont();
             }
             int[] lengths = new int[]{b.Length};
             pobj = new StreamFont(b, lengths, compressionLevel);
             obj = writer.AddToBody(pobj);
             ind_font = obj.IndirectReference;
         }
     }
     pobj = GetFontDescriptor(ind_font, subsetPrefix, null);
     if (pobj != null){
         obj = writer.AddToBody(pobj);
         ind_font = obj.IndirectReference;
     }
     pobj = GetFontBaseType(ind_font, subsetPrefix, firstChar, lastChar, shortTag);
     writer.AddToBody(pobj, piref);
 }
Exemple #20
0
 /**
 * Creates a new layer.
 * @param name the name of the layer
 * @param writer the writer
 */    
 public PdfLayer(String name, PdfWriter writer) : base(PdfName.OCG) {
     Name = name;
     if (writer is PdfStamperImp)
         refi = writer.AddToBody(this).IndirectReference;
     else
         refi = writer.PdfIndirectReference;
     writer.RegisterLayer(this);
 }
 /**
 * Adds the names of the named destinations to the catalog.
 * @param localDestinations the local destinations
 * @param documentJavaScript the javascript used in the document
 * @param writer the writer the catalog applies to
 */
 internal void AddNames(SortedDictionary<string,Destination> localDestinations, Dictionary<String, PdfObject> documentLevelJS, Dictionary<String, PdfObject> documentFileAttachment, PdfWriter writer) {
     if (localDestinations.Count == 0 && documentLevelJS.Count == 0 && documentFileAttachment.Count == 0)
         return;
     PdfDictionary names = new PdfDictionary();
     if (localDestinations.Count > 0) {
         PdfArray ar = new PdfArray();
         foreach (String name in localDestinations.Keys) {
             Destination dest;
             if (!localDestinations.TryGetValue(name, out dest))
                 continue;
             PdfIndirectReference refi = dest.reference;
             ar.Add(new PdfString(name, null));
             ar.Add(refi);
         }
         if (ar.Size > 0) {
             PdfDictionary dests = new PdfDictionary();
             dests.Put(PdfName.NAMES, ar);
             names.Put(PdfName.DESTS, writer.AddToBody(dests).IndirectReference);
         }
     }
     if (documentLevelJS.Count > 0) {
         PdfDictionary tree = PdfNameTree.WriteTree(documentLevelJS, writer);
         names.Put(PdfName.JAVASCRIPT, writer.AddToBody(tree).IndirectReference);
     }
     if (documentFileAttachment.Count > 0) {
         names.Put(PdfName.EMBEDDEDFILES, writer.AddToBody(PdfNameTree.WriteTree(documentFileAttachment, writer)).IndirectReference);
     }
     if (names.Size > 0)
         Put(PdfName.NAMES, writer.AddToBody(names).IndirectReference);
 }
Exemple #22
0
 /**
 * Adds the names of the named destinations to the catalog.
 * @param localDestinations the local destinations
 * @param documentJavaScript the javascript used in the document
 * @param writer the writer the catalog applies to
 */
 internal void AddNames(k_Tree localDestinations, ArrayList documentJavaScript, Hashtable documentFileAttachment, PdfWriter writer)
 {
     if (localDestinations.Count == 0 && documentJavaScript.Count == 0 && documentFileAttachment.Count == 0)
         return;
     PdfDictionary names = new PdfDictionary();
     if (localDestinations.Count > 0) {
         PdfArray ar = new PdfArray();
         foreach (DictionaryEntry entry in localDestinations) {
             String name = (String)entry.Key;
             Object[] obj = (Object[])entry.Value;
             PdfIndirectReference refi = (PdfIndirectReference)obj[1];
             ar.Add(new PdfString(name));
             ar.Add(refi);
         }
         PdfDictionary dests = new PdfDictionary();
         dests.Put(PdfName.NAMES, ar);
         names.Put(PdfName.DESTS, writer.AddToBody(dests).IndirectReference);
     }
     if (documentJavaScript.Count > 0) {
         String[] s = new String[documentJavaScript.Count];
         for (int k = 0; k < s.Length; ++k)
             s[k] = k.ToString("X");
         Array.Sort(s);
         PdfArray ar = new PdfArray();
         for (int k = 0; k < s.Length; ++k) {
             ar.Add(new PdfString(s[k]));
             ar.Add((PdfIndirectReference)documentJavaScript[k]);
         }
         PdfDictionary js = new PdfDictionary();
         js.Put(PdfName.NAMES, ar);
         names.Put(PdfName.JAVASCRIPT, writer.AddToBody(js).IndirectReference);
     }
     if (documentFileAttachment.Count > 0) {
         names.Put(PdfName.EMBEDDEDFILES, writer.AddToBody(PdfNameTree.WriteTree(documentFileAttachment, writer)).IndirectReference);
     }
     Put(PdfName.NAMES, writer.AddToBody(names).IndirectReference);
 }