public string GetPdfImage(PdfPage p, string imgname, int contentRef, ImageFormat imf, byte[] ba, int width, int height)
		{
			PdfImageEntry ie;
			if (imgname != null)
			{
				ie = (PdfImageEntry) images[imgname];
				if (ie != null)
				{
					p.AddResource(ie, contentRef);
					return ie.name;
				}
			}
			else
				imgname = "I" + (images.Count + 1).ToString();
			ie = new PdfImageEntry(pa, p, contentRef, imgname, imf, ba, width, height);
			images.Add(imgname, ie);
			return ie.name;
		}
		public void RunPages(Pages pgs)	// this does all the work
		{	  
			foreach (Page p in pgs)
			{
				//Create a Page Dictionary representing a visible page
				page=new PdfPage(anchor);
				content=new PdfContent(anchor);

				PdfPageSize pSize=new PdfPageSize((int) r.ReportDefinition.PageWidth.Points, 
									(int) r.ReportDefinition.PageHeight.Points);
				page.CreatePage(pageTree.objectNum,pSize);
				pageTree.AddPage(page.objectNum);

				//Create object that presents the elements in the page
				elements=new PdfElements(page, pSize);

				ProcessPage(pgs, p);

				// after a page
				content.SetStream(elements.EndElements());

				page.AddResource(fonts,content.objectNum);
				page.AddResource(patterns,content.objectNum);
				//get the pattern colorspace...
				PatternObj po = new PatternObj(anchor);
				page.AddResource(po,content.objectNum);

				int size=0;
				tw.Write(page.GetPageDict(filesize,out size),0,size);
				filesize += size;

				tw.Write(content.GetContentDict(filesize,out size),0,size);
				filesize += size;
				
				tw.Write(po.GetPatternObj(filesize, out size),0,size);
				filesize += size;
			}
			return;
		}
		/// <summary>
		/// Create the image Dictionary
		/// </summary>
		public PdfImageEntry(PdfAnchor pa, PdfPage p, int contentRef, string nm, ImageFormat imgf, byte[] im, int width, int height):base(pa)
		{
			name=nm;
			imf = imgf;
			ba=im;

			string filter;
            string colorSpace;
            if (imf == ImageFormat.Jpeg)
            {
                filter = "/DCTDecode";

                switch (JpgParser.GetColorSpace(ref im))
                {   
                    case 0:
                        colorSpace = "/DeviceRGB";
                        break;
                    case 1:
                        colorSpace = "/DeviceGray";
                        break;
                    case 3:
                        colorSpace = "/DeviceRGB";
                        break;
                    case 4:
                        colorSpace = "/DeviceCMYK";
                        break;
                    default:
                        colorSpace = "/DeviceRGB";
                        break;
                }
            }
            else if (imf == ImageFormat.Png)    // TODO: this still doesn't work
            {
                filter = "/FlateDecode /DecodeParms <</Predictor 15 /Colors 3 /BitsPerComponent 8 /Columns 80>>";
                colorSpace = "/DeviceRGB";
            }
            else if (imf == ImageFormat.Gif)    // TODO: this still doesn't work
            {
                filter = "/LZWDecode";
                colorSpace = "/DeviceRGB";
            }
            else
            {
                filter = "";
                colorSpace = "";
            }
			imgDict=string.Format("\r\n{0} 0 obj<</Type/XObject/Subtype /Image /Width {1} /Height {2} /ColorSpace {5} /BitsPerComponent 8 /Length {3} /Filter {4} >>\nstream\n",
				this.objectNum,width, height, ba.Length, filter, colorSpace);
					
			p.AddResource(this, contentRef);
		}
		public PdfElements(PdfPage pg, PdfPageSize pageSize)
		{
			p = pg;
			pSize=pageSize;
			elements = new StringBuilder();
		}