/** Create a PDF page to display the given bitmap image. * We need: * - A page object, that points to the resources and contents of the page. * - A resource object, that defines Im1 (image 1), the image to draw. * - A content object, that says to draw Im1 * - An XObject, that gives the raw image data (zlib-compressed) */ public void AddImage(Bitmap bitmap) { PDFObject page = createPDFObject(PDFType.PDFTypePage); PDFObject pagetree = getPDFObject(PDFType.PDFTypePageTree); PDFObject pageResource = createPDFObject(PDFType.PDFTypePageResource); PDFObject pageContent = createPDFObject(PDFType.PDFTypePageContent); PDFObject pageContentLength = createPDFObject(PDFType.PDFTypeLength); PDFObject pageXObject = createPDFObject(PDFType.PDFTypeXObject); PDFObject pageXObjectLength = createPDFObject(PDFType.PDFTypeLength); page.StringPairs = "<< /Type /Page /Parent " + pagetree.RefString + " /Resources " + pageResource.RefString + " /Contents " + pageContent.RefString + " /MediaBox [0 0 840 1090] >>"; pageResource.StringPairs = "<< /ProcSet [ /PDF /ImageB /ImageC /ImageI ] /XObject << /Im1 " + pageXObject.RefString + " >> >>"; byte[] uncompressedValue = UTF8Encoding.UTF8.GetBytes("q Q q /Perceptual ri q " + bitmap.Width.ToString() + " 0 0 " + bitmap.Height.ToString() + " 0 0 cm /Im1 Do Q Q"); byte[] compressedValue = Compress(uncompressedValue); pageContentLength.StringPairs = compressedValue.Length.ToString(); pageContent.StringPairs = "<< /Length " + pageContentLength.RefString + " /Filter /FlateDecode >>"; pageContent.StreamValue = compressedValue; pageXObject.StringPairs = "<< /Length " + pageXObjectLength.RefString + " /Type /XObject /Subtype /Image /Width " + bitmap.Width.ToString() + " /Height " + bitmap.Height.ToString() + " /Interpolate false /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode >>"; pageXObject.StreamValue = CompressImage(bitmap); pageXObjectLength.StringPairs = pageXObject.StreamValue.Length.ToString(); }
/** Allocate a new PDF Object */ PDFObject createPDFObject(PDFType type) { PDFObject obj = new PDFObject(pdfobjects.Count + 1, type); pdfobjects.Add(obj); return(obj); }
/** Write the PDF trailer */ void WriteTrailer() { TimeSpan epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1)); int timestamp = (int)epoch.TotalSeconds; PDFObject catalog = getPDFObject(PDFType.PDFTypeCatalog); PDFObject info = getPDFObject(PDFType.PDFTypeInfo); WriteString("trailer\n<< /Size " + (pdfobjects.Count + 1).ToString() + " /Root " + catalog.RefString + " /Info " + info.RefString + " /ID [ <" + timestamp.ToString() + "> <" + timestamp.ToString() + "> ] >>\n"); WriteString("startxref\n" + startxref.ToString() + "\n%%EOF\n"); }
/** Create the PDF catalog, info, and page tree objects */ void CreateHeaderObjects() { string datestring = DateTime.Now.ToString("yyyymmddhhmmss-00'00'"); PDFObject docinfo = createPDFObject(PDFType.PDFTypeInfo); docinfo.StringPairs = "<< /Title (" + title + ") /Producer (MidiSheetMusic) /CreationDate (D:" + datestring + ") >>"; PDFObject catalog = createPDFObject(PDFType.PDFTypeCatalog); PDFObject pagetree = createPDFObject(PDFType.PDFTypePageTree); catalog.StringPairs = "<< /Type /Catalog /Pages " + pagetree.RefString + " /Version /1.4 >>"; }
/** Create the page tree object, which contains references to all the page objects */ void initPageTreeValue() { PDFObject pagetree = getPDFObject(PDFType.PDFTypePageTree); string pageTreeValue = "<< /Type /Pages /MediaBox [0 0 840 1090] /Count " + numpages.ToString() + " /Kids [ "; foreach (PDFObject obj in pdfobjects) { if (obj.PDFType == PDFType.PDFTypePage) { pageTreeValue += obj.RefString + " "; } } pageTreeValue += "] >>"; pagetree.StringPairs = pageTreeValue; }