/** Does the actual document manipulation to encrypt it.
         * @throws DocumentException on error
         * @throws IOException on error
         */
        protected void go()
        {
            body = new PdfBody(HEADER.Length, this, true);
            os.Write(HEADER, 0, HEADER.Length);
            PdfObject[] xb = reader.xrefObj;
            myXref = new int[xb.Length];
            int idx = 1;

            for (int k = 1; k < xb.Length; ++k)
            {
                if (xb[k] != null)
                {
                    myXref[k] = idx++;
                }
            }
            file.reOpen();
            for (int k = 1; k < xb.Length; ++k)
            {
                if (xb[k] != null)
                {
                    addToBody(xb[k]);
                }
            }
            file.close();
            PdfIndirectReference encryption = null;
            PdfLiteral           fileID     = null;

            if (crypto != null)
            {
                PdfIndirectObject encryptionObject = body.Add(crypto.EncryptionDictionary);
                encryptionObject.writeTo(os);
                encryption = encryptionObject.IndirectReference;
                fileID     = crypto.FileID;
            }
            // write the cross-reference table of the body
            os.Write(body.CrossReferenceTable, 0, body.CrossReferenceTable.Length);
            PRIndirectReference  iRoot = (PRIndirectReference)reader.trailer.get(PdfName.ROOT);
            PdfIndirectReference root  = new PdfIndirectReference(0, myXref[iRoot.Number]);
            PRIndirectReference  iInfo = (PRIndirectReference)reader.trailer.get(PdfName.INFO);
            PdfIndirectReference info  = null;

            if (iInfo != null)
            {
                info = new PdfIndirectReference(0, myXref[iInfo.Number]);
            }
            PdfTrailer trailer = new PdfTrailer(body.Size,
                                                body.Offset,
                                                root,
                                                info,
                                                encryption,
                                                fileID);

            byte[] tmp = trailer.toPdf(this);
            os.Write(tmp, 0, tmp.Length);
            os.Close();
        }
 internal void writeAllPages()
 {
     try {
         file.reOpen();
         foreach (PdfImportedPage ip in importedPages.Values)
         {
             writer.addToBody(ip.FormXObject, ip.IndirectReference);
         }
         writeAllVisited();
     }
     finally {
         try {
             file.close();
         }
         catch (Exception e) {
             e.GetType();
             //Empty on purpose
         }
     }
 }
Beispiel #3
0
 /** Does the actual work of subsetting the font.
  * @throws IOException on error
  * @throws DocumentException on error
  * @return the subset font
  */
 internal byte[] process()
 {
     try {
         rf.reOpen();
         createTableDirectory();
         readLoca();
         flatGlyphs();
         createNewGlyphTables();
         locaTobytes();
         assembleFont();
         return(outFont);
     }
     finally {
         try {
             rf.close();
         }
         catch (Exception e) {
             e.GetType();
             // empty on purpose
         }
     }
 }
Beispiel #4
0
 public void close()
 {
     file.close();
 }
Beispiel #5
0
        /** Reads the font metrics
         * @param rf the AFM file
         * @throws DocumentException the AFM file is invalid
         * @throws IOException the AFM file could not be read
         */
        public void process(RandomAccessFileOrArray rf)
        {
            string line;
            bool   isMetrics = false;

            while ((line = rf.readLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.hasMoreTokens())
                {
                    continue;
                }
                string ident = tok.nextToken();
                if (ident.Equals("FontName"))
                {
                    FontName = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FullName"))
                {
                    FullName = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FamilyName"))
                {
                    FamilyName = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("Weight"))
                {
                    Weight = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("ItalicAngle"))
                {
                    ItalicAngle = float.Parse(tok.nextToken());
                }
                else if (ident.Equals("IsFixedPitch"))
                {
                    IsFixedPitch = tok.nextToken().Equals("true");
                }
                else if (ident.Equals("CharacterSet"))
                {
                    CharacterSet = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("FontBBox"))
                {
                    llx = (int)float.Parse(tok.nextToken());
                    lly = (int)float.Parse(tok.nextToken());
                    urx = (int)float.Parse(tok.nextToken());
                    ury = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("UnderlinePosition"))
                {
                    UnderlinePosition = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("UnderlineThickness"))
                {
                    UnderlineThickness = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("EncodingScheme"))
                {
                    EncodingScheme = tok.nextToken("\u00ff").Substring(1);
                }
                else if (ident.Equals("CapHeight"))
                {
                    CapHeight = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("XHeight"))
                {
                    XHeight = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("Ascender"))
                {
                    Ascender = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("Descender"))
                {
                    Descender = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("StdHW"))
                {
                    StdHW = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("StdVW"))
                {
                    StdVW = (int)float.Parse(tok.nextToken());
                }
                else if (ident.Equals("StartCharMetrics"))
                {
                    isMetrics = true;
                    break;
                }
            }
            if (!isMetrics)
            {
                throw new DocumentException("Missing StartCharMetrics in " + fileName);
            }
            while ((line = rf.readLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.hasMoreTokens())
                {
                    continue;
                }
                string ident = tok.nextToken();
                if (ident.Equals("EndCharMetrics"))
                {
                    isMetrics = false;
                    break;
                }
                int    C  = -1;
                int    WX = 250;
                string N  = "";
                tok = new StringTokenizer(line, ";");
                while (tok.hasMoreTokens())
                {
                    StringTokenizer tokc = new StringTokenizer(tok.nextToken());
                    if (!tokc.hasMoreTokens())
                    {
                        continue;
                    }
                    ident = tokc.nextToken();
                    if (ident.Equals("C"))
                    {
                        C = int.Parse(tokc.nextToken());
                    }
                    else if (ident.Equals("WX"))
                    {
                        WX = int.Parse(tokc.nextToken());
                    }
                    else if (ident.Equals("N"))
                    {
                        N = tokc.nextToken();
                    }
                }
                CharMetrics.Add(new Object[] { C, WX, N });
            }
            if (isMetrics)
            {
                throw new DocumentException("Missing EndCharMetrics in " + fileName);
            }
            while ((line = rf.readLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.hasMoreTokens())
                {
                    continue;
                }
                string ident = tok.nextToken();
                if (ident.Equals("EndFontMetrics"))
                {
                    return;
                }
                if (ident.Equals("StartKernPairs"))
                {
                    isMetrics = true;
                    break;
                }
            }
            if (!isMetrics)
            {
                throw new DocumentException("Missing EndFontMetrics in " + fileName);
            }
            while ((line = rf.readLine()) != null)
            {
                StringTokenizer tok = new StringTokenizer(line);
                if (!tok.hasMoreTokens())
                {
                    continue;
                }
                string ident = tok.nextToken();
                if (ident.Equals("KPX"))
                {
                    string   first   = tok.nextToken();
                    string   second  = tok.nextToken();
                    int      width   = (int)float.Parse(tok.nextToken());
                    Object[] relates = (Object[])KernPairs[first];
                    if (relates == null)
                    {
                        KernPairs.Add(first, new Object[] { second, width });
                    }
                    else
                    {
                        int      n        = relates.Length;
                        Object[] relates2 = new Object[n + 2];
                        Array.Copy(relates, 0, relates2, 0, n);
                        relates2[n]     = second;
                        relates2[n + 1] = width;
                        KernPairs.Add(first, relates2);
                    }
                }
                else if (ident.Equals("EndKernPairs"))
                {
                    isMetrics = false;
                    break;
                }
            }
            if (isMetrics)
            {
                throw new DocumentException("Missing EndKernPairs in " + fileName);
            }
            rf.close();
        }
Beispiel #6
0
        /** Creates a new Type1 font.
         * @param ttfAfm the AFM file if the input is made with a <CODE>byte</CODE> array
         * @param pfb the PFB file if the input is made with a <CODE>byte</CODE> array
         * @param afmFile the name of one of the 14 built-in fonts or the location of an AFM file. The file must end in '.afm'
         * @param enc the encoding to be applied to this font
         * @param emb true if the font is to be embedded in the PDF
         * @throws DocumentException the AFM file is invalid
         * @throws IOException the AFM file could not be read
         */
        internal Type1Font(string afmFile, string enc, bool emb, byte[] ttfAfm, byte[] pfb)
        {
            if (emb && ttfAfm != null && pfb == null)
            {
                throw new DocumentException("Two byte arrays are needed if the Type1 font is embedded.");
            }
            if (emb && ttfAfm != null)
            {
                this.pfb = pfb;
            }
            encoding = enc;
            embedded = emb;
            fileName = afmFile;
            FontType = FONT_TYPE_T1;
            RandomAccessFileOrArray rf = null;
            Stream istr = null;

            if (BuiltinFonts14.ContainsKey(afmFile))
            {
                embedded    = false;
                builtinFont = true;
                byte[] buf = new byte[1024];
                try {
                    istr = getResourceStream(afmFile + ".afm");
                    if (istr == null)
                    {
                        Console.Error.WriteLine(afmFile + " not found as resource.");
                        throw new DocumentException(afmFile + " not found as resource.");
                    }
                    MemoryStream ostr = new MemoryStream();
                    while (true)
                    {
                        int size = istr.Read(buf, 0, buf.Length);
                        if (size == 0)
                        {
                            break;
                        }
                        ostr.Write(buf, 0, size);
                    }
                    buf = ostr.ToArray();
                }
                finally {
                    if (istr != null)
                    {
                        try {
                            istr.Close();
                        }
                        catch (Exception e) {
                            e.GetType();
                            // empty on purpose
                        }
                    }
                }
                try {
                    rf = new RandomAccessFileOrArray(buf);
                    process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.close();
                        }
                        catch (Exception e) {
                            e.GetType();
                            // empty on purpose
                        }
                    }
                }
            }
            else if (afmFile.ToLower().EndsWith(".afm"))
            {
                try {
                    if (ttfAfm == null)
                    {
                        rf = new RandomAccessFileOrArray(afmFile);
                    }
                    else
                    {
                        rf = new RandomAccessFileOrArray(ttfAfm);
                    }
                    process(rf);
                }
                finally {
                    if (rf != null)
                    {
                        try {
                            rf.close();
                        }
                        catch (Exception e) {
                            e.GetType();
                            // empty on purpose
                        }
                    }
                }
            }
            else
            {
                throw new DocumentException(afmFile + " is not an AFM font file.");
            }
            try {
                EncodingScheme = EncodingScheme.Trim();
                if (EncodingScheme.Equals("AdobeStandardEncoding") || EncodingScheme.Equals("StandardEncoding"))
                {
                    fontSpecific = false;
                }
                System.Text.Encoding.GetEncoding(enc).GetBytes(" ");
                createEncoding();
            }
            catch (Exception e) {
                throw new DocumentException(e.Message);
            }
        }