Extract embedded PDF fonts to an external ttf file using some utility or script

后端 未结 5 1539
小蘑菇
小蘑菇 2020-12-17 07:26

Is it possible to extract fonts that are embedded in a PDF file to an external ttf file using some utility or script?

  1. If the fonts that are embedded (or not

5条回答
  •  佛祖请我去吃肉
    2020-12-17 08:03

    I know it's been a while since you asked this, but I figured I might be able to help.

    I don't know if there is any utility that will allow you to extract the Font files, but you can do it manually.

    Basically a PDF file is a text file with different objects. You can open it with any text editor and look for the fonts.

    The fonts are specified in FontDescriptor objects, e.g:

    <>
    

    This basically says, a font with the name Algerian is specified on the object 24. You can search the document for the object 24 with the line "24 0 obj", after this line, it displays the properties of the stream with the font file and after the "stream" keyword it starts (its length is defined in the line after the obj).

    This stream contains the ttf file, compressed, to decompress it you can use this method:

      private static byte[] DecodeFlateDecodeData(byte[] data)
      {
         MemoryStream outputStream;
         using (outputStream = new MemoryStream())
         {
            using (var compressedDataStream = new MemoryStream(data))
            {
               // Remove the first two bytes to skip the header (it isn't recognized by the DeflateStream class)
               compressedDataStream.ReadByte();
               compressedDataStream.ReadByte();
    
               var deflateStream = new DeflateStream(compressedDataStream, CompressionMode.Decompress, true);
    
               var decompressedBuffer = new byte[1024];
               int read;
               while ((read = deflateStream.Read(decompressedBuffer, 0, decompressedBuffer.Length)) != 0)
               {
                  outputStream.Write(decompressedBuffer, 0, read);
               }
               outputStream.Flush();
               compressedDataStream.Close();
            }
            return GetStreamBytes(outputStream);
         }
      }
    

    I hope this helps you... or helps somebody else

提交回复
热议问题