public void RenderDocument(IRenderable renderable, Stream stream) { Document document = new Document(); PdfWriter.GetInstance(document, stream); var reportItems = renderable.GetDocumentOutline(); var headerImage = reportItems.FirstOrDefault(iri => iri is HeaderImage) as HeaderImage; Image img = null; int pages = 1; document.Open(); if (headerImage != null) { // if we can find the file if (File.Exists(headerImage.ImagePath)) { // try and load an image try { img = Image.GetInstance(new FileStream(@headerImage.ImagePath, FileMode.Open)); } catch { img = null; } } // if no valid image, try to get embedded default if (img == null) { // try to get an embedded default try { img = Image.GetInstance(Assembly.GetExecutingAssembly().GetResourceStream(@"Demo.Report.logo-default.png")); } catch (Exception ex) { img = null; } } // if we have a valid image here, try to add it if (img != null) { img.ScaleToFit(document.PageSize.Width - 72f, 150f); img.Alignment = Image.TEXTWRAP | Image.ALIGN_CENTER; document.Add(img); } // otherwise, give up } foreach (IReportItem iri in reportItems) { if (iri is DataTuple) { document.Add((iri as DataTuple).GetElement()); continue; } if (iri is DataSection) { foreach (IElement ie in ((iri as DataSection).GetElements())) { document.Add(ie); } continue; } if (iri is HeaderBase) { document.Add((iri as HeaderBase).GetElement()); continue; } if (iri is TextSection) { document.Add((iri as TextSection).GetElement()); continue; } if (iri is PageBreak) { if (pages > 1 && img != null) { img.ScaleToFit((document.PageSize.Width - 72f) / 2f, 50f); img.SetAbsolutePosition(document.PageSize.Width - img.ScaledWidth - 10f, 10f); document.Add(img); } document.NewPage(); pages++; continue; } if (iri is LineBreak) { int i = (iri as LineBreak).Repeat; do { document.Add(new Chunk(Chunk.NEWLINE)); i--; } while (i > 0); } } document.Close(); }