Beispiel #1
0
        internal void RegisterAsComposite(
            PDF pdf,
            Font font,
            Stream inputStream,
            bool embed)
        {
            otf = new OTF(inputStream);
            if (embed) {
            EmbedFontFile(pdf, font, otf, false);
            }

            AddFontDescriptorObject(pdf, font, otf, embed);
            AddCIDFontDictionaryObject(pdf, font, otf);
            AddToUnicodeCMapObject(pdf, font, otf);

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(otf.fontName);
            pdf.Append('\n');
            pdf.Append("/Encoding /Identity-H\n");
            pdf.Append("/DescendantFonts [");
            pdf.Append(font.GetCidFontDictObjNumber());
            pdf.Append(" 0 R]\n");
            pdf.Append("/ToUnicode ");
            pdf.Append(font.GetToUnicodeCMapObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.objNumber = pdf.objNumber;
        }
Beispiel #2
0
 public Page(PDF pdf, double[] pageSize)
 {
     this.pdf = pdf;
     annots = new List<Annotation>();
     width = pageSize[0];
     height = pageSize[1];
     buf = new MemoryStream(8192);
     pdf.pages.Add(this);
 }
Beispiel #3
0
        internal void RegisterAsSimple(
            PDF pdf,
            Font font,
            Stream inputStream,
            int codePage,
            bool embed)
        {
            otf = new OTF(inputStream);
            if (embed) {
            EmbedFontFile(pdf, font, otf, true);
            }

            AddFontDescriptorObject(pdf, font, otf, embed);
            AddWidthsArrayObject(pdf, font, otf, codePage);
            AddEncodingObject(pdf, font, codePage);

            // Simple font object
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            if (otf.cff) {
            pdf.Append("/Subtype /Type1\n");
            } else {
            pdf.Append("/Subtype /TrueType\n");
            }
            pdf.Append("/BaseFont /");
            pdf.Append(otf.fontName);
            pdf.Append('\n');
            pdf.Append("/FirstChar ");
            pdf.Append(otf.firstChar);
            pdf.Append('\n');
            pdf.Append("/LastChar ");
            pdf.Append(255);
            pdf.Append('\n');
            pdf.Append("/Encoding ");
            pdf.Append(font.GetEncodingObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append("/Widths ");
            pdf.Append(font.GetWidthsArrayObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append("/FontDescriptor ");
            pdf.Append(font.GetFontDescriptorObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.objNumber = pdf.objNumber;
        }
Beispiel #4
0
        /**
         *  The main constructor for the Image class.
         *
         *  @param pdf the page to draw this image on.
         *  @param inputStream the input stream to read the image from.
         *  @param imageType ImageType.JPG, ImageType.PNG or ImageType.BMP.
         *
         */
        public Image(PDF pdf, Stream inputStream, int imageType)
        {
            byte[] data;
            if (imageType == ImageType.JPG) {
            JPGImage jpg = new JPGImage(inputStream);
            data = jpg.GetData();
            w = jpg.GetWidth();
            h = jpg.GetHeight();
            if (jpg.GetColorComponents() == 1) {
                AddImage(pdf, data, null, imageType, "DeviceGray", 8);
            }
            else if (jpg.GetColorComponents() == 3) {
                AddImage(pdf, data, null, imageType, "DeviceRGB", 8);
            }
            else if (jpg.GetColorComponents() == 4) {
                AddImage(pdf, data, null, imageType, "DeviceCMYK", 8);
            }
            }
            else if (imageType == ImageType.PNG) {
            PNGImage png = new PNGImage(inputStream);
            data = png.GetData();
            w = png.GetWidth();
            h = png.GetHeight();
            if (png.GetColorType() == 0) {
                AddImage(pdf, data, null, imageType, "DeviceGray", png.GetBitDepth());
            }
            else {
                if (png.GetBitDepth() == 16) {
                    AddImage(pdf, data, null, imageType, "DeviceRGB", 16);
                }
                else {
                    AddImage(pdf, data, png.GetAlpha(), imageType, "DeviceRGB", 8);
                }
            }
            }
            else if (imageType == ImageType.BMP) {
            BMPImage bmp = new BMPImage(inputStream);
            data = bmp.GetData();
            w = bmp.GetWidth();
            h = bmp.GetHeight();
            AddImage(pdf, data, null, imageType, "DeviceRGB", 8);
            }
            else if (imageType == ImageType.JET) {
            AddImage(pdf, inputStream);
            }

            inputStream.Dispose();
        }
Beispiel #5
0
        //-------------------------------------------------------------------------------------------
        /// <summary>
        /// This method generates a PDF copy of the check to be printed on standard 3 sheet check paper.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="invoice"></param>
        /// <returns>Returns the path to the PDF file on the local file system.</returns>
        public string GeneratePDF()
        {
            string filepath = System.IO.Path.GetTempFileName() + ".pdf";

               FileStream fos = new FileStream(filepath, FileMode.Create);
               BufferedStream bos = new BufferedStream(fos);
               PDF pdf = new PDF(bos);
               Page p = new PDFjet.NET.Page(pdf, Letter.PORTRAIT);

               // these two variables are lazy loaded from the db so we cache them here
               Logistics_Addresses payeeAddress = PayeeAddress;
               string payeeName = PayeeName;
               for (int i = 0; i < 3; i++)
               {
                    int yoffset = i * 251;
                    // these lines draw a seperation line between the 3 parts on a full check sheet which is useful for debugging
                    //Line l = new Line(0, yoffset, 400, yoffset);
                    //l.DrawOn(p);
                    //yoffset += 25;
                    // draw the date
                    DrawText(pdf, p, PostAt.ToString("MM/dd/yy"), 515, yoffset + 70);

                    int xnameoffset = (i == 0) ? 85 : 30;
                    DrawText(pdf, p, payeeName, xnameoffset, yoffset + 105);
                    DrawText(pdf, p, "**" + String.Format("{0:f}", Amount), 500, yoffset + 107);
                    int amountnodecimals = Convert.ToInt32(Math.Truncate(Amount));
                    int decimals = Convert.ToInt32((Amount - amountnodecimals) * 100);
                    DrawText(pdf, p, NumberConvertor.NumberToText(amountnodecimals).ToLower() + " dollar(s) " + NumberConvertor.NumberToText(decimals).ToLower() + " cents *****************", 30, yoffset + 130);

                    // draw the mailing address for windowed envelopes
                    string mailingAddress = (payeeAddress == null) ? "" : payeeAddress.ToString();
                    if (!String.IsNullOrEmpty(mailingAddress))
                    {
                         string[] addressLines = Regex.Split(mailingAddress, "\r\n");
                         for (int a = 0; a < addressLines.Length; a++)
                         {
                              DrawText(pdf, p, addressLines[a], 50, yoffset + 155 + (a * 12));
                         }
                    }

                    // draw the memo
                    DrawText(pdf, p, Memo, 30, yoffset + 215);
               }
               pdf.Flush();
               bos.Close();

               return filepath;
        }
Beispiel #6
0
        public Image(PDF pdf, System.IO.Stream inputStream, int imageType)
        {
            if (imageType == ImageType.JPEG) {
            JPEGImage jpg = new JPEGImage(inputStream);
            data = jpg.GetData();
            w = jpg.GetWidth();
            h = jpg.GetHeight();
            if ( jpg.GetColorComponents() == 1 ) {
                addImage(pdf, data, imageType, "DeviceGray", 8);
            }
            else if ( jpg.GetColorComponents() == 3 ) {
                addImage(pdf, data, imageType, "DeviceRGB", 8);
            }
            }
            else if (imageType == ImageType.PNG) {
            PNGImage png = new PNGImage(inputStream);
            data = png.GetData();
            w = png.GetWidth();
            h = png.GetHeight();
            if ( png.colorType == 0 ) {
                addImage(pdf, data, imageType, "DeviceGray", png.bitDepth);
            }
            else {
                if ( png.bitDepth == 16 ) {
                    addImage(pdf, data, imageType, "DeviceRGB", 16);
                }
                else {
                    addImage(pdf, data, imageType, "DeviceRGB", 8);
                }
            }
            }
            else if (imageType == ImageType.BMP) {
            BMPImage bmp = new BMPImage(inputStream);
            data = bmp.GetData();
            w = bmp.GetWidth();
            h = bmp.GetHeight();
            addImage(pdf, data, imageType, "DeviceRGB", 8);
            }

            inputStream.Close();
        }
Beispiel #7
0
 // Constructor for .ttf.stream fonts:
 public Font(PDF pdf, Stream inputStream, bool flag)
 {
     FontStream1.Register(pdf, this, inputStream);
     SetSize(size);
 }
Beispiel #8
0
 /**
  *  Constructor for the 14 standard fonts.
  *  Creates a font object and adds it to the PDF.
  *
  *  <pre>
  *  Examples:
  *      Font font1 = new Font(pdf, CoreFont.HELVETICA);
  *      Font font2 = new Font(pdf, CoreFont.TIMES_ITALIC);
  *      Font font3 = new Font(pdf, CoreFont.ZAPF_DINGBATS);
  *      ...
  *  </pre>
  *
  *  @param pdf the PDF to add this font to.
  *  @param coreFont the core font. Must be one the names defined in the CoreFont class.
  */
 public Font(PDF pdf, CoreFont coreFont)
 {
     AddCoreFont(pdf, coreFont);
 }
Beispiel #9
0
 private void addImage(
     PDF pdf,
     byte[] data,
     int imageType,
     String colorSpace,
     int bitsPerComponent)
 {
     // Add the image
     pdf.newobj();
     pdf.Append("<<\n");
     pdf.Append("/Type /XObject\n");
     pdf.Append("/Subtype /Image\n");
     if (imageType == ImageType.JPEG) {
     pdf.Append("/Filter /DCTDecode\n");
     }
     else if (imageType == ImageType.PNG || imageType == ImageType.BMP) {
     pdf.Append("/Filter /FlateDecode\n");
     }
     pdf.Append("/Width ");
     pdf.Append(( int ) w);
     pdf.Append('\n');
     pdf.Append("/Height ");
     pdf.Append(( int ) h);
     pdf.Append('\n');
     pdf.Append("/ColorSpace /");
     pdf.Append(colorSpace);
     pdf.Append('\n');
     pdf.Append("/BitsPerComponent ");
     pdf.Append(bitsPerComponent);
     pdf.Append('\n');
     pdf.Append("/Length ");
     pdf.Append(data.Length);
     pdf.Append('\n');
     pdf.Append(">>\n");
     pdf.Append("stream\n");
     pdf.Append(data, 0, data.Length);
     pdf.Append("\nendstream\n");
     pdf.endobj();
     pdf.images.Add(this);
     objNumber = pdf.objNumber;
 }
Beispiel #10
0
        internal static void Register(
            PDF pdf,
            Font font,
            Stream inputStream)
        {
            int len = inputStream.ReadByte();
            byte[] fontName = new byte[len];
            inputStream.Read(fontName, 0, len);
            font.name = System.Text.Encoding.UTF8.GetString(fontName);
            // Console.WriteLine(font.name);

            font.unitsPerEm = GetInt32(inputStream);
            font.bBoxLLx = GetInt32(inputStream);
            font.bBoxLLy = GetInt32(inputStream);
            font.bBoxURx = GetInt32(inputStream);
            font.bBoxURy = GetInt32(inputStream);
            font.ascent = GetInt32(inputStream);
            font.descent = GetInt32(inputStream);
            font.firstChar = GetInt32(inputStream);
            font.lastChar = GetInt32(inputStream);
            font.capHeight = GetInt32(inputStream);
            font.underlinePosition = GetInt32(inputStream);
            font.underlineThickness = GetInt32(inputStream);

            len = GetInt32(inputStream);
            font.advanceWidth = new int[len];
            for (int i = 0; i < len; i++) {
            font.advanceWidth[i] = GetInt16(inputStream);
            }

            len = GetInt32(inputStream);
            font.glyphWidth = new int[len];
            for (int i = 0; i < len; i++) {
            font.glyphWidth[i] = GetInt16(inputStream);
            }

            len = GetInt32(inputStream);
            font.unicodeToGID = new int[len];
            for (int i = 0; i < len; i++) {
            font.unicodeToGID[i] = GetInt16(inputStream);
            }

            font.uncompressed_size = GetInt32(inputStream);
            font.compressed_size = GetInt32(inputStream);

            EmbedFontFile(pdf, font, inputStream);
            AddFontDescriptorObject(pdf, font);
            AddCIDFontDictionaryObject(pdf, font);
            AddToUnicodeCMapObject(pdf, font);

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(font.name);
            pdf.Append('\n');
            pdf.Append("/Encoding /Identity-H\n");
            pdf.Append("/DescendantFonts [");
            pdf.Append(font.GetCidFontDictObjNumber());
            pdf.Append(" 0 R]\n");
            pdf.Append("/ToUnicode ");
            pdf.Append(font.GetToUnicodeCMapObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.objNumber = pdf.objNumber;
        }
Beispiel #11
0
        private void AddFontDescriptorObject(
            PDF pdf,
            Font font,
            OTF otf,
            bool embed)
        {
            float factor = 1000f / otf.unitsPerEm;

            for (int i = 0; i < pdf.fonts.Count; i++) {
            Font f = pdf.fonts[i];
            if (f.name.Equals(otf.fontName) && f.GetFontDescriptorObjNumber() != -1) {
                font.SetFontDescriptorObjNumber(f.GetFontDescriptorObjNumber());
                return;
            }
            }

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /FontDescriptor\n");
            pdf.Append("/FontName /");
            pdf.Append(otf.fontName);
            pdf.Append('\n');
            if (embed) {
            if (otf.cff) {
                pdf.Append("/FontFile3 ");
            } else {
                pdf.Append("/FontFile2 ");
            }
            pdf.Append(font.fileObjNumber);
            pdf.Append(" 0 R\n");
            }
            pdf.Append("/Flags 32\n");
            pdf.Append("/FontBBox [");
            pdf.Append(otf.bBoxLLx * factor);
            pdf.Append(' ');
            pdf.Append(otf.bBoxLLy * factor);
            pdf.Append(' ');
            pdf.Append(otf.bBoxURx * factor);
            pdf.Append(' ');
            pdf.Append(otf.bBoxURy * factor);
            pdf.Append("]\n");
            pdf.Append("/Ascent ");
            pdf.Append(otf.ascent * factor);
            pdf.Append('\n');
            pdf.Append("/Descent ");
            pdf.Append(otf.descent * factor);
            pdf.Append('\n');
            pdf.Append("/ItalicAngle 0\n");
            pdf.Append("/CapHeight ");
            pdf.Append(otf.capHeight * factor);
            pdf.Append('\n');
            pdf.Append("/StemV 79\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.SetFontDescriptorObjNumber(pdf.objNumber);
        }
Beispiel #12
0
 public Image(PDF pdf, PDFobj obj)
 {
     pdf.newobj();
     pdf.Append("<<\n");
     pdf.Append("/Type /XObject\n");
     pdf.Append("/Subtype /Image\n");
     pdf.Append("/Filter ");
     pdf.Append(obj.getValue(PDFobj.FILTER));
     pdf.Append('\n');
     pdf.Append("/Width ");
     pdf.Append(obj.getValue(PDFobj.WIDTH));
     pdf.Append('\n');
     pdf.Append("/Height ");
     pdf.Append(obj.getValue(PDFobj.HEIGHT));
     pdf.Append('\n');
     pdf.Append("/ColorSpace ");
     pdf.Append(obj.getValue(PDFobj.COLORSPACE));
     pdf.Append('\n');
     pdf.Append("/BitsPerComponent ");
     pdf.Append(obj.getValue(PDFobj.BITSPERCOMPONENT));
     pdf.Append('\n');
     pdf.Append("/Length ");
     pdf.Append(obj.stream.Length);
     pdf.Append('\n');
     pdf.Append(">>\n");
     pdf.Append("stream\n");
     pdf.Append(obj.stream, 0, obj.stream.Length);
     pdf.Append("\nendstream\n");
     pdf.endobj();
     pdf.images.Add(this);
     objNumber = pdf.objNumber;
 }
Beispiel #13
0
 private void AddImage(
     PDF pdf,
     byte[] data,
     byte[] alpha,
     int imageType,
     String colorSpace,
     int bitsPerComponent)
 {
     if (alpha != null) {
     AddSoftMask(pdf, alpha, "DeviceGray", 8);
     }
     pdf.Newobj();
     pdf.Append("<<\n");
     pdf.Append("/Type /XObject\n");
     pdf.Append("/Subtype /Image\n");
     if (imageType == ImageType.JPG) {
     pdf.Append("/Filter /DCTDecode\n");
     }
     else if (imageType == ImageType.PNG || imageType == ImageType.BMP) {
     pdf.Append("/Filter /FlateDecode\n");
     if (alpha != null) {
         pdf.Append("/SMask ");
         pdf.Append(objNumber);
         pdf.Append(" 0 R\n");
     }
     }
     pdf.Append("/Width ");
     pdf.Append(( int ) w);
     pdf.Append('\n');
     pdf.Append("/Height ");
     pdf.Append(( int ) h);
     pdf.Append('\n');
     pdf.Append("/ColorSpace /");
     pdf.Append(colorSpace);
     pdf.Append('\n');
     pdf.Append("/BitsPerComponent ");
     pdf.Append(bitsPerComponent);
     pdf.Append('\n');
     if (colorSpace.Equals("DeviceCMYK")) {
     // If the image was created with Photoshop - invert the colors:
     pdf.Append("/Decode [1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0]\n");
     }
     pdf.Append("/Length ");
     pdf.Append(data.Length);
     pdf.Append('\n');
     pdf.Append(">>\n");
     pdf.Append("stream\n");
     pdf.Append(data, 0, data.Length);
     pdf.Append("\nendstream\n");
     pdf.Endobj();
     pdf.images.Add(this);
     objNumber = pdf.objNumber;
 }
Beispiel #14
0
        private void AddCIDFontDictionaryObject(
            PDF pdf,
            Font font,
            OTF otf)
        {
            for (int i = 0; i < pdf.fonts.Count; i++) {
            Font f = pdf.fonts[i];
            if (f.name.Equals(otf.fontName) && f.GetCidFontDictObjNumber() != -1) {
                font.SetCidFontDictObjNumber(f.GetCidFontDictObjNumber());
                return;
            }
            }

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            if (otf.cff) {
            pdf.Append("/Subtype /CIDFontType0\n");
            } else {
            pdf.Append("/Subtype /CIDFontType2\n");
            }
            pdf.Append("/BaseFont /");
            pdf.Append(otf.fontName);
            pdf.Append('\n');
            pdf.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>>\n");
            pdf.Append("/FontDescriptor ");
            pdf.Append(font.GetFontDescriptorObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append("/DW ");
            pdf.Append((int)
                ((1000f / otf.unitsPerEm) * otf.advanceWidth[0]));
            pdf.Append('\n');
            pdf.Append("/W [0[\n");
            for (int i = 0; i < otf.advanceWidth.Length; i++) {
            pdf.Append((int)
                    ((1000f / otf.unitsPerEm) * otf.advanceWidth[i]));
            if ((i + 1) % 10 == 0) {
                pdf.Append('\n');
            }
            else {
                pdf.Append(' ');
            }
            }
            pdf.Append("]]\n");
            pdf.Append("/CIDToGIDMap /Identity\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.SetCidFontDictObjNumber(pdf.objNumber);
        }
Beispiel #15
0
        // Constructor for the DejaVuLGCSerif.ttf font.
        public Font(PDF pdf, Stream inputStream)
        {
            this.isStandard = false;
            this.isComposite = true;
            this.codePage = CodePage.UNICODE;

            FastFont.Register(pdf, this, inputStream);

            this.ascent = bBoxURy * size / unitsPerEm;
            this.descent = bBoxLLy * size / unitsPerEm;
            this.body_height = ascent - descent;
            this.underlineThickness = fontUnderlineThickness * size / unitsPerEm;
            this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2f;

            pdf.fonts.Add(this);
        }
Beispiel #16
0
        private void AddImage(PDF pdf, Stream inputStream)
        {
            w = GetInt(inputStream);               // Width
            h = GetInt(inputStream);               // Height
            byte c = (byte)inputStream.ReadByte(); // Color Space
            byte a = (byte)inputStream.ReadByte(); // Alpha

            if (a != 0)
            {
                pdf.Newobj();
                pdf.Append("<<\n");
                pdf.Append("/Type /XObject\n");
                pdf.Append("/Subtype /Image\n");
                pdf.Append("/Filter /FlateDecode\n");
                pdf.Append("/Width ");
                pdf.Append(w);
                pdf.Append('\n');
                pdf.Append("/Height ");
                pdf.Append(h);
                pdf.Append('\n');
                pdf.Append("/ColorSpace /DeviceGray\n");
                pdf.Append("/BitsPerComponent 8\n");
                int length = GetInt(inputStream);
                pdf.Append("/Length ");
                pdf.Append(length);
                pdf.Append('\n');
                pdf.Append(">>\n");
                pdf.Append("stream\n");
                byte[] buf1 = new byte[length];
                inputStream.Read(buf1, 0, length);
                pdf.Append(buf1, 0, length);
                pdf.Append("\nendstream\n");
                pdf.Endobj();
                objNumber = pdf.objNumber;
            }

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /XObject\n");
            pdf.Append("/Subtype /Image\n");
            pdf.Append("/Filter /FlateDecode\n");
            if (a != 0)
            {
                pdf.Append("/SMask ");
                pdf.Append(objNumber);
                pdf.Append(" 0 R\n");
            }
            pdf.Append("/Width ");
            pdf.Append(w);
            pdf.Append('\n');
            pdf.Append("/Height ");
            pdf.Append(h);
            pdf.Append('\n');
            pdf.Append("/ColorSpace /");
            if (c == 1)
            {
                pdf.Append("DeviceGray");
            }
            else if (c == 3 || c == 6)
            {
                pdf.Append("DeviceRGB");
            }
            pdf.Append('\n');
            pdf.Append("/BitsPerComponent 8\n");
            pdf.Append("/Length ");
            pdf.Append(GetInt(inputStream));
            pdf.Append('\n');
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            byte[] buf2 = new byte[2048];
            int    count;

            while ((count = inputStream.Read(buf2, 0, buf2.Length)) > 0)
            {
                pdf.Append(buf2, 0, count);
            }
            pdf.Append("\nendstream\n");
            pdf.Endobj();
            pdf.images.Add(this);
            objNumber = pdf.objNumber;
        }
Beispiel #17
0
        // Constructor for CJK fonts
        public Font(PDF pdf, String fontName, int codePage)
        {
            this.name = fontName;
            this.codePage = codePage;
            isCJK = true;
            isStandard = false;
            isComposite = true;

            firstChar = 0x0020;
            lastChar = 0xFFEE;

            // Font Descriptor
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /FontDescriptor\n");
            pdf.Append("/FontName /");
            pdf.Append(fontName);
            pdf.Append('\n');
            pdf.Append("/Flags 4\n");
            pdf.Append("/FontBBox [0 0 0 0]\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            // CIDFont Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /CIDFontType0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(fontName);
            pdf.Append('\n');
            pdf.Append("/FontDescriptor ");
            pdf.Append(pdf.objNumber - 1);
            pdf.Append(" 0 R\n");
            pdf.Append("/CIDSystemInfo <<\n");
            pdf.Append("/Registry (Adobe)\n");
            if (fontName.StartsWith("AdobeMingStd")) {
            pdf.Append("/Ordering (CNS1)\n");
            pdf.Append("/Supplement 4\n");
            } else if (fontName.StartsWith("AdobeSongStd")
                || fontName.StartsWith("STHeitiSC")) {
            pdf.Append("/Ordering (GB1)\n");
            pdf.Append("/Supplement 4\n");
            } else if (fontName.StartsWith("KozMinPro")) {
            pdf.Append("/Ordering (Japan1)\n");
            pdf.Append("/Supplement 4\n");
            } else if (fontName.StartsWith("AdobeMyungjoStd")) {
            pdf.Append("/Ordering (Korea1)\n");
            pdf.Append("/Supplement 1\n");
            } else {
            throw new Exception("Unsupported font: " + fontName);
            }
            pdf.Append(">>\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            if (fontName.StartsWith("AdobeMingStd")) {
            pdf.Append(fontName + "-UniCNS-UTF16-H\n");
            pdf.Append("/Encoding /UniCNS-UTF16-H\n");
            } else if (fontName.StartsWith("AdobeSongStd")
                || fontName.StartsWith("STHeitiSC")) {
            pdf.Append(fontName + "-UniGB-UTF16-H\n");
            pdf.Append("/Encoding /UniGB-UTF16-H\n");
            } else if (fontName.StartsWith("KozMinPro")) {
            pdf.Append(fontName + "-UniJIS-UCS2-H\n");
            pdf.Append("/Encoding /UniJIS-UCS2-H\n");
            } else if (fontName.StartsWith("AdobeMyungjoStd")) {
            pdf.Append(fontName + "-UniKS-UCS2-H\n");
            pdf.Append("/Encoding /UniKS-UCS2-H\n");
            } else {
            throw new Exception("Unsupported font: " + fontName);
            }
            pdf.Append("/DescendantFonts [");
            pdf.Append(pdf.objNumber - 1);
            pdf.Append(" 0 R]\n");
            pdf.Append(">>\n");
            pdf.Endobj();
            objNumber = pdf.objNumber;

            ascent = size;
            descent = -ascent/4;
            body_height = ascent - descent;

            pdf.fonts.Add(this);
        }
Beispiel #18
0
        /**
         *  Constructor for the 14 standard fonts.
         *  Creates a font object and adds it to the PDF.
         *
         *  <pre>
         *  Examples:
         *      Font font1 = new Font(pdf, CoreFont.HELVETICA);
         *      Font font2 = new Font(pdf, CoreFont.TIMES_ITALIC);
         *      Font font3 = new Font(pdf, CoreFont.ZAPF_DINGBATS);
         *      ...
         *  </pre>
         *
         *  @param pdf the PDF to add this font to.
         *  @param coreFont the core font. Must be one the names defined in the CoreFont class.
         */
        public Font(PDF pdf, CoreFont coreFont)
        {
            StandardFont font = StandardFont.GetInstance(coreFont);
            this.name = font.name;
            this.bBoxLLx = font.bBoxLLx;
            this.bBoxLLy = font.bBoxLLy;
            this.bBoxURx = font.bBoxURx;
            this.bBoxURy = font.bBoxURy;
            this.fontUnderlinePosition = font.underlinePosition;
            this.fontUnderlineThickness = font.underlineThickness;
            this.metrics = font.metrics;
            this.ascent = bBoxURy * size / unitsPerEm;
            this.descent = bBoxLLy * size / unitsPerEm;
            this.body_height = ascent - descent;
            this.underlineThickness = fontUnderlineThickness * size / unitsPerEm;
            this.underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0f;

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type1\n");
            pdf.Append("/BaseFont /");
            pdf.Append(this.name);
            pdf.Append('\n');
            if (!this.name.Equals("Symbol") && !this.name.Equals("ZapfDingbats")) {
            pdf.Append("/Encoding /WinAnsiEncoding\n");
            }
            pdf.Append(">>\n");
            pdf.Endobj();
            objNumber = pdf.objNumber;

            pdf.fonts.Add(this);
        }
Beispiel #19
0
        // Constructor for standard fonts
        public Font(PDF pdf, String fontName)
        {
            this.pdf = pdf;
            this.name = fontName;

            pdf.newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type1\n");
            pdf.Append("/BaseFont /");
            pdf.Append(fontName);
            pdf.Append('\n');
            if (fontName.Equals("Symbol") || fontName.Equals("ZapfDingbats")) {
            // Use the built-in encoding
            } else {
            pdf.Append("/Encoding /WinAnsiEncoding\n");
            }
            pdf.Append(">>\n");
            pdf.endobj();
            objNumber = pdf.objNumber;

            CoreFont font = (CoreFont) Activator.CreateInstance(Type.GetType("PDFjet.NET." + name.Replace('-', '_')));
            bBoxLLx = font.getBBoxLLx();
            bBoxLLy = font.getBBoxLLy();
            bBoxURx = font.getBBoxURx();
            bBoxURy = font.getBBoxURy();
            metrics = font.getMetrics();
            ascent = bBoxURy * size / unitsPerEm;
            descent = bBoxLLy * size / unitsPerEm;
            body_height = ascent - descent;

            fontUnderlineThickness = font.getUnderlineThickness();
            fontUnderlinePosition = font.getUnderlinePosition();

            underlineThickness = fontUnderlineThickness * size / unitsPerEm;
            underlinePosition = fontUnderlinePosition * size / -unitsPerEm + underlineThickness / 2.0;

            pdf.fonts.Add(this);
        }
Beispiel #20
0
        // Constructor for CJK fonts
        public Font(PDF pdf, String fontName, int codePage)
        {
            this.name     = fontName;
            this.codePage = codePage;
            isCJK         = true;
            isStandard    = false;
            isComposite   = true;

            firstChar = 0x0020;
            lastChar  = 0xFFEE;

            // Font Descriptor
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /FontDescriptor\n");
            pdf.Append("/FontName /");
            pdf.Append(fontName);
            pdf.Append('\n');
            pdf.Append("/Flags 4\n");
            pdf.Append("/FontBBox [0 0 0 0]\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            // CIDFont Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /CIDFontType0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(fontName);
            pdf.Append('\n');
            pdf.Append("/FontDescriptor ");
            pdf.Append(pdf.objNumber - 1);
            pdf.Append(" 0 R\n");
            pdf.Append("/CIDSystemInfo <<\n");
            pdf.Append("/Registry (Adobe)\n");
            if (fontName.StartsWith("AdobeMingStd"))
            {
                pdf.Append("/Ordering (CNS1)\n");
                pdf.Append("/Supplement 4\n");
            }
            else if (fontName.StartsWith("AdobeSongStd") ||
                     fontName.StartsWith("STHeitiSC"))
            {
                pdf.Append("/Ordering (GB1)\n");
                pdf.Append("/Supplement 4\n");
            }
            else if (fontName.StartsWith("KozMinPro"))
            {
                pdf.Append("/Ordering (Japan1)\n");
                pdf.Append("/Supplement 4\n");
            }
            else if (fontName.StartsWith("AdobeMyungjoStd"))
            {
                pdf.Append("/Ordering (Korea1)\n");
                pdf.Append("/Supplement 1\n");
            }
            else
            {
                throw new Exception("Unsupported font: " + fontName);
            }
            pdf.Append(">>\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            if (fontName.StartsWith("AdobeMingStd"))
            {
                pdf.Append(fontName + "-UniCNS-UTF16-H\n");
                pdf.Append("/Encoding /UniCNS-UTF16-H\n");
            }
            else if (fontName.StartsWith("AdobeSongStd") ||
                     fontName.StartsWith("STHeitiSC"))
            {
                pdf.Append(fontName + "-UniGB-UTF16-H\n");
                pdf.Append("/Encoding /UniGB-UTF16-H\n");
            }
            else if (fontName.StartsWith("KozMinPro"))
            {
                pdf.Append(fontName + "-UniJIS-UCS2-H\n");
                pdf.Append("/Encoding /UniJIS-UCS2-H\n");
            }
            else if (fontName.StartsWith("AdobeMyungjoStd"))
            {
                pdf.Append(fontName + "-UniKS-UCS2-H\n");
                pdf.Append("/Encoding /UniKS-UCS2-H\n");
            }
            else
            {
                throw new Exception("Unsupported font: " + fontName);
            }
            pdf.Append("/DescendantFonts [");
            pdf.Append(pdf.objNumber - 1);
            pdf.Append(" 0 R]\n");
            pdf.Append(">>\n");
            pdf.Endobj();
            objNumber = pdf.objNumber;

            ascent      = size;
            descent     = -ascent / 4;
            body_height = ascent - descent;

            pdf.fonts.Add(this);
        }
Beispiel #21
0
        private void AddImage(PDF pdf, Stream inputStream)
        {
            w = GetInt(inputStream);                // Width
            h = GetInt(inputStream);                // Height
            byte c = (byte) inputStream.ReadByte(); // Color Space
            byte a = (byte) inputStream.ReadByte(); // Alpha

            if (a != 0) {
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /XObject\n");
            pdf.Append("/Subtype /Image\n");
            pdf.Append("/Filter /FlateDecode\n");
            pdf.Append("/Width ");
            pdf.Append(w);
            pdf.Append('\n');
            pdf.Append("/Height ");
            pdf.Append(h);
            pdf.Append('\n');
            pdf.Append("/ColorSpace /DeviceGray\n");
            pdf.Append("/BitsPerComponent 8\n");
            int length = GetInt(inputStream);
            pdf.Append("/Length ");
            pdf.Append(length);
            pdf.Append('\n');
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            byte[] buf1 = new byte[length];
            inputStream.Read(buf1, 0, length);
            pdf.Append(buf1, 0, length);
            pdf.Append("\nendstream\n");
            pdf.Endobj();
            objNumber = pdf.objNumber;
            }

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /XObject\n");
            pdf.Append("/Subtype /Image\n");
            pdf.Append("/Filter /FlateDecode\n");
            if (a != 0) {
            pdf.Append("/SMask ");
            pdf.Append(objNumber);
            pdf.Append(" 0 R\n");
            }
            pdf.Append("/Width ");
            pdf.Append(w);
            pdf.Append('\n');
            pdf.Append("/Height ");
            pdf.Append(h);
            pdf.Append('\n');
            pdf.Append("/ColorSpace /");
            if (c == 1) {
            pdf.Append("DeviceGray");
            }
            else if (c == 3 || c == 6) {
            pdf.Append("DeviceRGB");
            }
            pdf.Append('\n');
            pdf.Append("/BitsPerComponent 8\n");
            pdf.Append("/Length ");
            pdf.Append(GetInt(inputStream));
            pdf.Append('\n');
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            byte[] buf2 = new byte[2048];
            int count;
            while ((count = inputStream.Read(buf2, 0, buf2.Length)) > 0) {
            pdf.Append(buf2, 0, count);
            }
            pdf.Append("\nendstream\n");
            pdf.Endobj();
            pdf.images.Add(this);
            objNumber = pdf.objNumber;
        }
Beispiel #22
0
        /**
         *  Use this constructor to create font from data found in PDFobj.
         *
         *  @param pdf the PDF we want to add this font to.
         *  @param obj the PDFobj containing the font data.
         */
        public Font(PDF pdf, PDFobj obj)
        {
            String fontName = obj.GetValue("/BaseFont").Substring(1);

            if (fontName.Equals(Helvetica.name))
            {
                AddCoreFont(pdf, CoreFont.HELVETICA);
            }
            else if (fontName.Equals(Helvetica_Bold.name))
            {
                AddCoreFont(pdf, CoreFont.HELVETICA_BOLD);
            }
            else if (fontName.Equals(Helvetica_Oblique.name))
            {
                AddCoreFont(pdf, CoreFont.HELVETICA_OBLIQUE);
            }
            else if (fontName.Equals(Helvetica_BoldOblique.name))
            {
                AddCoreFont(pdf, CoreFont.HELVETICA_BOLD_OBLIQUE);
            }
            else if (fontName.Equals(Times_Roman.name))
            {
                AddCoreFont(pdf, CoreFont.TIMES_ROMAN);
            }
            else if (fontName.Equals(Times_Bold.name))
            {
                AddCoreFont(pdf, CoreFont.TIMES_BOLD);
            }
            else if (fontName.Equals(Times_Italic.name))
            {
                AddCoreFont(pdf, CoreFont.TIMES_ITALIC);
            }
            else if (fontName.Equals(Times_BoldItalic.name))
            {
                AddCoreFont(pdf, CoreFont.TIMES_BOLD_ITALIC);
            }
            else if (fontName.Equals(Courier.name))
            {
                AddCoreFont(pdf, CoreFont.COURIER);
            }
            else if (fontName.Equals(Courier_Bold.name))
            {
                AddCoreFont(pdf, CoreFont.COURIER_BOLD);
            }
            else if (fontName.Equals(Courier_Oblique.name))
            {
                AddCoreFont(pdf, CoreFont.COURIER_OBLIQUE);
            }
            else if (fontName.Equals(Courier_BoldOblique.name))
            {
                AddCoreFont(pdf, CoreFont.COURIER_BOLD_OBLIQUE);
            }
            else if (fontName.Equals(Symbol.name))
            {
                AddCoreFont(pdf, CoreFont.SYMBOL);
            }
            else if (fontName.Equals(ZapfDingbats.name))
            {
                AddCoreFont(pdf, CoreFont.ZAPF_DINGBATS);
            }
        }
        private static void AddToUnicodeCMapObject(PDF pdf, Font font)
        {
            for (int i = 0; i < pdf.fonts.Count; i++)
            {
                Font f = pdf.fonts[i];
                if (f.name.Equals(font.name) && f.GetToUnicodeCMapObjNumber() != -1)
                {
                    font.SetToUnicodeCMapObjNumber(f.GetToUnicodeCMapObjNumber());
                    return;
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("/CIDInit /ProcSet findresource begin\n");
            sb.Append("12 dict begin\n");
            sb.Append("begincmap\n");
            sb.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n");
            sb.Append("/CMapName /Adobe-Identity def\n");
            sb.Append("/CMapType 2 def\n");

            sb.Append("1 begincodespacerange\n");
            sb.Append("<0000> <FFFF>\n");
            sb.Append("endcodespacerange\n");

            List <String> list = new List <String>();
            StringBuilder buf  = new StringBuilder();

            for (int cid = 0; cid <= 0xffff; cid++)
            {
                int gid = font.unicodeToGID[cid];
                if (gid > 0)
                {
                    buf.Append('<');
                    buf.Append(ToHexString(gid));
                    buf.Append("> <");
                    buf.Append(ToHexString(cid));
                    buf.Append(">\n");
                    list.Add(buf.ToString());
                    buf.Length = 0;
                    if (list.Count == 100)
                    {
                        WriteListToBuffer(list, sb);
                    }
                }
            }
            if (list.Count > 0)
            {
                WriteListToBuffer(list, sb);
            }

            sb.Append("endcmap\n");
            sb.Append("CMapName currentdict /CMap defineresource pop\n");
            sb.Append("end\nend");

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Length ");
            pdf.Append(sb.Length);
            pdf.Append("\n");
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            pdf.Append(sb.ToString());
            pdf.Append("\nendstream\n");
            pdf.Endobj();

            font.SetToUnicodeCMapObjNumber(pdf.objNumber);
        }
Beispiel #24
0
        private void AddEncodingObject(
            PDF pdf,
            Font font,
            int codePage)
        {
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Encoding\n");
            pdf.Append("/BaseEncoding /WinAnsiEncoding\n");
            pdf.Append("/Differences [127\n");
            for (int i = 0; i < 129; i++) {
            if (codePage == 0) {
                pdf.Append(CP1250.names[i]);
            }
            else if (codePage == 1) {
                pdf.Append(CP1251.names[i]);
            }
            else if (codePage == 2) {
                pdf.Append(CP1252.names[i]);
            }
            else if (codePage == 3) {
                pdf.Append(CP1253.names[i]);
            }
            else if (codePage == 4) {
                pdf.Append(CP1254.names[i]);
            }
            else if (codePage == 7) {
                pdf.Append(CP1257.names[i]);
            }
            pdf.Append(' ');
            }
            pdf.Append("]\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.SetEncodingObjNumber(pdf.objNumber);
        }
Beispiel #25
0
        private void EmbedFontFile(
            PDF pdf,
            Font font,
            OTF otf,
            bool simpleFont)
        {
            // Check if the font file is already embedded
            for (int i = 0; i < pdf.fonts.Count; i++) {
            Font f = pdf.fonts[i];
            if (f.name.Equals(otf.fontName) && f.fileObjNumber != -1) {
                font.fileObjNumber = f.fileObjNumber;
                return;
            }
            }

            int metadataObjNumber = -1;
            if (otf.fontName.ToLower().IndexOf("droid") != -1
                || otf.fontName.ToLower().IndexOf("roboto") != -1) {
            metadataObjNumber = pdf.AddMetadataObject(AndroidFontsCopyright.NOTICE, true);
            }

            pdf.Newobj();
            pdf.Append("<<\n");
            if (otf.cff) {
            if (simpleFont) {
                pdf.Append("/Subtype /Type1C\n");
            }
            else {
                pdf.Append("/Subtype /CIDFontType0C\n");
            }
            }
            pdf.Append("/Filter /FlateDecode\n");

            pdf.Append("/Length ");
            pdf.Append(otf.baos.Length);
            pdf.Append("\n");

            if (!otf.cff) {
            pdf.Append("/Length1 ");
            pdf.Append(otf.buf.Length);
            pdf.Append('\n');
            }

            if (metadataObjNumber != -1) {
            pdf.Append("/Metadata ");
            pdf.Append(metadataObjNumber);
            pdf.Append(" 0 R\n");
            }

            pdf.Append(">>\n");
            pdf.Append("stream\n");
            pdf.Append(otf.baos);
            pdf.Append("\nendstream\n");
            pdf.Endobj();

            font.fileObjNumber = pdf.objNumber;
        }
Beispiel #26
0
        private void AddToUnicodeCMapObject(
            PDF pdf,
            Font font,
            OTF otf)
        {
            for (int i = 0; i < pdf.fonts.Count; i++) {
            Font f = pdf.fonts[i];
            if (f.name.Equals(otf.fontName) && f.GetToUnicodeCMapObjNumber() != -1) {
                font.SetToUnicodeCMapObjNumber(f.GetToUnicodeCMapObjNumber());
                return;
            }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("/CIDInit /ProcSet findresource begin\n");
            sb.Append("12 dict begin\n");
            sb.Append("begincmap\n");
            sb.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n");
            sb.Append("/CMapName /Adobe-Identity def\n");
            sb.Append("/CMapType 2 def\n");

            sb.Append("1 begincodespacerange\n");
            sb.Append("<0000> <FFFF>\n");
            sb.Append("endcodespacerange\n");

            List<String> list = new List<String>();
            StringBuilder buf = new StringBuilder();
            for (int cid = 0; cid <= 0xffff; cid++) {
            int gid = otf.unicodeToGID[cid];
            if (gid > 0) {
                buf.Append('<');
                buf.Append(ToHexString(gid));
                buf.Append("> <");
                buf.Append(ToHexString(cid));
                buf.Append(">\n");
                list.Add(buf.ToString());
                buf.Length = 0;
                if (list.Count == 100) {
                    WriteListToBuffer(list, sb);
                }
            }
            }
            if (list.Count > 0) {
            WriteListToBuffer(list, sb);
            }

            sb.Append("endcmap\n");
            sb.Append("CMapName currentdict /CMap defineresource pop\n");
            sb.Append("end\nend");

            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Length ");
            pdf.Append(sb.Length);
            pdf.Append("\n");
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            pdf.Append(sb.ToString());
            pdf.Append("\nendstream\n");
            pdf.Endobj();

            font.SetToUnicodeCMapObjNumber(pdf.objNumber);
        }
Beispiel #27
0
        private static void AddToUnicodeCMapObject(PDF pdf, Font font)
        {
            for (int i = 0; i < pdf.fonts.Count; i++)
            {
                Font font2 = pdf.fonts[i];
                if (font2.name.Equals(font.name) && font2.GetToUnicodeCMapObjNumber() != -1)
                {
                    font.SetToUnicodeCMapObjNumber(font2.GetToUnicodeCMapObjNumber());
                    return;
                }
            }
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("/CIDInit /ProcSet findresource begin\n");
            stringBuilder.Append("12 dict begin\n");
            stringBuilder.Append("begincmap\n");
            stringBuilder.Append("/CIDSystemInfo <</Registry (Adobe) /Ordering (Identity) /Supplement 0>> def\n");
            stringBuilder.Append("/CMapName /Adobe-Identity def\n");
            stringBuilder.Append("/CMapType 2 def\n");
            stringBuilder.Append("1 begincodespacerange\n");
            stringBuilder.Append("<0000> <FFFF>\n");
            stringBuilder.Append("endcodespacerange\n");
            List <string> list           = new List <string>();
            StringBuilder stringBuilder2 = new StringBuilder();

            for (int j = 0; j <= 65535; j++)
            {
                int num = font.unicodeToGID[j];
                if (num > 0)
                {
                    stringBuilder2.Append('<');
                    stringBuilder2.Append(FastFont.ToHexString(num));
                    stringBuilder2.Append("> <");
                    stringBuilder2.Append(FastFont.ToHexString(j));
                    stringBuilder2.Append(">\n");
                    list.Add(stringBuilder2.ToString());
                    stringBuilder2.Length = 0;
                    if (list.Count == 100)
                    {
                        FastFont.WriteListToBuffer(list, stringBuilder);
                    }
                }
            }
            if (list.Count > 0)
            {
                FastFont.WriteListToBuffer(list, stringBuilder);
            }
            stringBuilder.Append("endcmap\n");
            stringBuilder.Append("CMapName currentdict /CMap defineresource pop\n");
            stringBuilder.Append("end\nend");
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Length ");
            pdf.Append(stringBuilder.Length);
            pdf.Append("\n");
            pdf.Append(">>\n");
            pdf.Append("stream\n");
            pdf.Append(stringBuilder.ToString());
            pdf.Append("\nendstream\n");
            pdf.Endobj();
            font.SetToUnicodeCMapObjNumber(pdf.objNumber);
        }
Beispiel #28
0
        private void AddWidthsArrayObject(
            PDF pdf,
            Font font,
            OTF otf,
            int codePage)
        {
            pdf.Newobj();
            pdf.Append("[ ");
            int n = 1;
            for (int c = otf.firstChar; c < 256; c++) {
            if (c < 127) {
                pdf.Append((int)
                        ((1000.0f / otf.unitsPerEm) * otf.glyphWidth[c]));
            } else {
                if (codePage == 0) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1250.codes[c - 127]]));
                } else if (codePage == 1) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1251.codes[c - 127]]));
                } else if (codePage == 2) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1252.codes[c - 127]]));
                } else if (codePage == 3) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1253.codes[c - 127]]));
                } else if (codePage == 4) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1254.codes[c - 127]]));
                } else if (codePage == 7) {
                    pdf.Append((int) ((1000.0f / otf.unitsPerEm)
                            * otf.glyphWidth[CP1257.codes[c - 127]]));
                }
            }
            if (n < 10) {
                pdf.Append(' ');
                ++n;
            }
            else {
                pdf.Append('\n');
                n = 1;
            }
            }
            pdf.Append("]\n");
            pdf.Endobj();

            font.SetWidthsArrayObjNumber(pdf.objNumber);
        }
 public FileAttachment(PDF pdf, EmbeddedFile file)
 {
     this.pdf          = pdf;
     this.embeddedFile = file;
 }
Beispiel #30
0
 private void AddSoftMask(
     PDF pdf,
     byte[] data,
     String colorSpace,
     int bitsPerComponent)
 {
     pdf.Newobj();
     pdf.Append("<<\n");
     pdf.Append("/Type /XObject\n");
     pdf.Append("/Subtype /Image\n");
     pdf.Append("/Filter /FlateDecode\n");
     pdf.Append("/Width ");
     pdf.Append(( int ) w);
     pdf.Append('\n');
     pdf.Append("/Height ");
     pdf.Append(( int ) h);
     pdf.Append('\n');
     pdf.Append("/ColorSpace /");
     pdf.Append(colorSpace);
     pdf.Append('\n');
     pdf.Append("/BitsPerComponent ");
     pdf.Append(bitsPerComponent);
     pdf.Append('\n');
     pdf.Append("/Length ");
     pdf.Append(data.Length);
     pdf.Append('\n');
     pdf.Append(">>\n");
     pdf.Append("stream\n");
     pdf.Append(data, 0, data.Length);
     pdf.Append("\nendstream\n");
     pdf.Endobj();
     objNumber = pdf.objNumber;
 }
        /// <summary>
        /// Populate a voter card with the information of a given voter.
        /// </summary>
        /// <param name="page">The page containing the card.</param>
        /// <param name="pdf">The pdf containing the page.</param>
        /// <param name="xO">The horizontal offset of the card in points.</param>
        /// <param name="yO">The vertical offset of the card in points.</param>
        /// <param name="voter">The voter whose information to be populated onto the card.</param>
        private static void PopulateCard(Page page, PDF pdf, double xO, double yO, VoterDO voter)
        {
            // ----- POPULATE: POLLING STATION -----
            PollingStationDO ps = voter.PollingStation;

            var font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(9);

            var t = new TextLine(font, ps.Name);
            t.SetPosition(xO + 9 * U, yO + 27.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, ps.Address);
            t.SetPosition(xO + 9 * U, yO + 32 * U);
            t.DrawOn(page);

            t = new TextLine(font, "valgfrit");
            t.SetPosition(xO + 29 * U, yO + 48.8 * U);
            t.DrawOn(page);

            t = new TextLine(font, "02-04861");
            t.SetPosition(xO + 29 * U, yO + 58.7 * U);
            t.DrawOn(page);

            t = new TextLine(font, "09:00 - 20:00");
            t.SetPosition(xO + 29 * U, yO + 68.2 * U);
            t.DrawOn(page);

            // ----- POPULATE: VOTER -----
            MunicipalityDO mun = voter.PollingStation.Municipality;

            font = new Font(pdf, CoreFont.COURIER);
            font.SetSize(10);

            // Add top voter number 'Vælgernr.'
            t = new TextLine(font, "02-04861");
            t.SetPosition(xO + 102 * U, yO + 12 * U);
            t.DrawOn(page);

            // Add sender 'Afsender'
            t = new TextLine(font, mun.Name);
            t.SetPosition(xO + 102 * U, yO + 32.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, mun.Address);
            t.SetPosition(xO + 102 * U, yO + 36.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, mun.City);
            t.SetPosition(xO + 102 * U, yO + 40.5 * U);
            t.DrawOn(page);

            // Add reciever 'Modtager'
            t = new TextLine(font, voter.Name);
            t.SetPosition(xO + 102 * U, yO + 62.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, voter.Address);
            t.SetPosition(xO + 102 * U, yO + 66.5 * U);
            t.DrawOn(page);

            t = new TextLine(font, voter.City);
            t.SetPosition(xO + 102 * U, yO + 70.5 * U);
            t.DrawOn(page);

            // Add CPR barcode
            string barcode = BarCodeHashing.Hash(voter.PrimaryKey.Value).ToString();
            var b = new BarCode(BarCode.CODE128, barcode);
            b.SetPosition(xO + 160 * U, yO + 60 * U);
            b.DrawOn(page);

            t = new TextLine(font, barcode);
            t.SetPosition(xO + 160 * U, yO + 72 * U);
            t.DrawOn(page);
        }
        internal static void Register(
            PDF pdf,
            Font font,
            Stream inputStream)
        {
            int len = inputStream.ReadByte();

            byte[] fontName = new byte[len];
            inputStream.Read(fontName, 0, len);
            font.name = System.Text.Encoding.UTF8.GetString(fontName, 0, len);
            // Console.WriteLine(font.name);

            len = GetInt24(inputStream);
            byte[] fontInfo = new byte[len];
            inputStream.Read(fontInfo, 0, len);
            font.info = System.Text.Encoding.UTF8.GetString(fontInfo, 0, len);
            // Console.WriteLine(font.info);

            byte[] buf = new byte[GetInt32(inputStream)];
            inputStream.Read(buf, 0, buf.Length);
            Decompressor decompressor = new Decompressor(buf);
            MemoryStream stream       =
                new MemoryStream(decompressor.GetDecompressedData());

            font.unitsPerEm         = GetInt32(stream);
            font.bBoxLLx            = GetInt32(stream);
            font.bBoxLLy            = GetInt32(stream);
            font.bBoxURx            = GetInt32(stream);
            font.bBoxURy            = GetInt32(stream);
            font.ascent             = GetInt32(stream);
            font.descent            = GetInt32(stream);
            font.firstChar          = GetInt32(stream);
            font.lastChar           = GetInt32(stream);
            font.capHeight          = GetInt32(stream);
            font.underlinePosition  = GetInt32(stream);
            font.underlineThickness = GetInt32(stream);

            len = GetInt32(stream);
            font.advanceWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.advanceWidth[i] = GetInt16(stream);
            }

            len             = GetInt32(stream);
            font.glyphWidth = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.glyphWidth[i] = GetInt16(stream);
            }

            len = GetInt32(stream);
            font.unicodeToGID = new int[len];
            for (int i = 0; i < len; i++)
            {
                font.unicodeToGID[i] = GetInt16(stream);
            }

            font.cff = (inputStream.ReadByte() == 'Y') ? true : false;
            font.uncompressed_size = GetInt32(inputStream);
            font.compressed_size   = GetInt32(inputStream);

            EmbedFontFile(pdf, font, inputStream);
            AddFontDescriptorObject(pdf, font);
            AddCIDFontDictionaryObject(pdf, font);
            AddToUnicodeCMapObject(pdf, font);

            // Type0 Font Dictionary
            pdf.Newobj();
            pdf.Append("<<\n");
            pdf.Append("/Type /Font\n");
            pdf.Append("/Subtype /Type0\n");
            pdf.Append("/BaseFont /");
            pdf.Append(font.name);
            pdf.Append('\n');
            pdf.Append("/Encoding /Identity-H\n");
            pdf.Append("/DescendantFonts [");
            pdf.Append(font.GetCidFontDictObjNumber());
            pdf.Append(" 0 R]\n");
            pdf.Append("/ToUnicode ");
            pdf.Append(font.GetToUnicodeCMapObjNumber());
            pdf.Append(" 0 R\n");
            pdf.Append(">>\n");
            pdf.Endobj();

            font.objNumber = pdf.objNumber;
        }
Beispiel #33
0
 /**
  *  Creates page object and add it to the PDF document.
  *
  *  Please note:
  *  <pre>
  *  The coordinate (0.0, 0.0) is the top left corner of the page.
  *  The size of the pages are represented in points.
  *  1 point is 1/72 inches.
  *  </pre>
  *
  *  @param pdf the pdf object.
  *  @param pageSize the page size of this page.
  */
 public Page(PDF pdf, float[] pageSize)
     : this(pdf, pageSize, true)
 {
 }
        /// <summary>
        /// Generate lines, boxes, watermark and default text of a single voter card.
        /// </summary>
        /// <param name="page">The page containing the card.</param>
        /// <param name="pdf">The pdf containing the page.</param>
        /// <param name="xO">The horizontal offset of the card in points.</param>
        /// <param name="yO">The vertical offset of the card in points.</param>
        private static void GenerateCard(Page page, PDF pdf, double xO, double yO)
        {
            // Add watermark.
            var font = new Font(pdf, CoreFont.HELVETICA_BOLD);
            font.SetSize(55);
            var t = new TextLine(font, "FAKE VALGKORT");
            var lightBlue = new[] { 0.647, 0.812, 0.957 };
            t.SetColor(lightBlue);
            t.SetPosition(xO + 20 * U, yO + 50 * U);
            t.DrawOn(page);

            // Add 'Afstemningssted' box.
            var b = new Box(xO + 6 * U, yO + 20 * U, 80 * U, 22 * U);
            b.DrawOn(page);

            // Add 'Valgbord' box.
            b = new Box(xO + 6 * U, yO + 44.5 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add 'Vælgernr' box.
            b = new Box(xO + 6 * U, yO + 54 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add 'Afstemningstid' box.
            b = new Box(xO + 6 * U, yO + 63.5 * U, 80 * U, 7 * U);
            b.DrawOn(page);

            // Add lines.
            var l = new Line(xO + 96 * U, yO + 5 * U, xO + 96 * U, yO + 85 * U);
            l.DrawOn(page);
            l = new Line(xO + 96 * U, yO + 25 * U, xO + 205 * U, yO + 25 * U);
            l.DrawOn(page);
            l = new Line(xO + 96 * U, yO + 45 * U, xO + 205 * U, yO + 45 * U);
            l.DrawOn(page);
            l = new Line(xO + 6 * U, yO + 85 * U, xO + 205 * U, yO + 85 * U);
            l.DrawOn(page);

            // Add default text.
            font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(7);
            t = new TextLine(font, "Afstemningssted:");
            t.SetPosition(xO + 7 * U, yO + 23 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Vælgernr.:");
            t.SetPosition(xO + 7 * U, yO + 58 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Afstemningstid:");
            t.SetPosition(xO + 7 * U, yO + 68 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Afsender:");
            t.SetPosition(xO + 98 * U, yO + 29 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Modtager:");
            t.SetPosition(xO + 98 * U, yO + 49 * U);
            t.DrawOn(page);

            font = new Font(pdf, CoreFont.HELVETICA);
            font.SetSize(9);
            t = new TextLine(font, "Folketingsvalg");
            t.SetPosition(xO + 7 * U, yO + 10 * U);
            t.DrawOn(page);
            t = new TextLine(font, "20. november 2001");
            t.SetPosition(xO + 7 * U, yO + 14 * U);
            t.DrawOn(page);
            t = new TextLine(font, "Valgbord:");
            t.SetPosition(xO + 7 * U, yO + 49 * U);
            t.DrawOn(page);

            font = new Font(pdf, CoreFont.HELVETICA_OBLIQUE);
            font.SetSize(7);
            t = new TextLine(font, "Medbring kortet ved afstemningen");
            t.SetPosition(xO + 6.5 * U, yO + 18.5 * U);
            t.DrawOn(page);
        }
Beispiel #35
0
        /**
         *  Creates page object and add it to the PDF document.
         *
         *  Please note:
         *  <pre>
         *  The coordinate (0.0, 0.0) is the top left corner of the page.
         *  The size of the pages are represented in points.
         *  1 point is 1/72 inches.
         *  </pre>
         *
         *  @param pdf the pdf object.
         *  @param pageSize the page size of this page.
         *  @param addPageToPDF bool flag.
         */
        public Page(PDF pdf, float[] pageSize, bool addPageToPDF)
        {
            this.pdf = pdf;
            contents = new List<Int32>();
            annots = new List<Annotation>();
            destinations = new List<Destination>();
            width = pageSize[0];
            height = pageSize[1];
            buf = new MemoryStream(8192);

            if (addPageToPDF) {
            pdf.AddPage(this);
            }
        }
        /// <summary>
        /// Generate a single batch (pdf-file) containing one or more voter cards.
        /// </summary>
        /// <param name="sender">The worker which has ordered the generation.</param>
        /// <param name="e">Arguments describing the current DoWork event.</param>
        /// <param name="voters">The voter(s) to be on the voter card(s) in the file.</param>
        /// <param name="fileName">The name of the file.</param>
        private void GenerateFile(object sender, DoWorkEventArgs e, IEnumerable<VoterDO> voters, String fileName)
        {
            var worker = sender as BackgroundWorker;

            String path = destination + "\\" + fileName + ".pdf";
            var fos = new FileStream(path, FileMode.Create);
            var bos = new BufferedStream(fos);

            var pdf = new PDF(bos); // PDF in file

            var page = new Page(pdf, A4.PORTRAIT); // First page in PDF

            int pageCount = 0;
            foreach (var voter in voters)
            {
                GenerateCard(page, pdf, 0, pageCount * U);
                PopulateCard(page, pdf, 0, pageCount * U, voter);
                if (pageCount != 200) pageCount += 100;
                else
                {
                    pageCount = 0;
                    page = new Page(pdf, A4.PORTRAIT);
                }

                // Update progress percentage?
                voterDoneCount++;
                double perc = (Convert.ToDouble(voterDoneCount) / Convert.ToDouble(voterCount)) * 100;
                int iperc = (int)perc;
                if (iperc != VoterDonePerc) worker.ReportProgress(iperc);

                // Cancelled?
                // If yes, flag cancelled and break, but make sure file stream is closed properly.
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
            }

            pdf.Flush();
            bos.Close();
        }
Beispiel #37
0
        private static void EmbedFontFile(PDF pdf, Font font, Stream inputStream)
        {
            // Check if the font file is already embedded
            for (int i = 0; i < pdf.fonts.Count; i++) {
            Font f = pdf.fonts[i];
            if (f.name.Equals(font.name) && f.fileObjNumber != -1) {
                font.fileObjNumber = f.fileObjNumber;
                return;
            }
            }
            /*
            int metadataObjNumber = pdf.AddMetadataObject(DejaVu.FONT_LICENSE, true);
            */
            pdf.Newobj();
            pdf.Append("<<\n");
            /*
            pdf.Append("/Metadata ");
            pdf.Append(metadataObjNumber);
            pdf.Append(" 0 R\n");
            */
            pdf.Append("/Filter /FlateDecode\n");
            pdf.Append("/Length ");
            pdf.Append(font.compressed_size);
            pdf.Append("\n");

            pdf.Append("/Length1 ");
            pdf.Append(font.uncompressed_size);
            pdf.Append('\n');

            pdf.Append(">>\n");
            pdf.Append("stream\n");
            byte[] buf = new byte[2048];
            int len;
            while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) {
            pdf.Append(buf, 0, len);
            }
            inputStream.Dispose();
            pdf.Append("\nendstream\n");
            pdf.Endobj();

            font.fileObjNumber = pdf.objNumber;
        }