// The main sample driver. public int CreatePDF(Stream stream) { GcPdfDocument doc = new GcPdfDocument(); // This will hold the llst of images so we can dispose them after saving the document: List <IDisposable> disposables = new List <IDisposable>(); // Page footer: var ftrImg = Image.FromFile(Path.Combine("Resources", "ImagesBis", "logo-GC-devsol.png")); disposables.Add(ftrImg); var fx = ftrImg.HorizontalResolution / 72f; var fy = ftrImg.VerticalResolution / 72f; var ftrRc = new RectangleF( doc.PageSize.Width / 2 - ftrImg.Width / fx / 2, doc.PageSize.Height - 40, ftrImg.Width / fx, ftrImg.Height / fy); // Color for the title: var colorBlue = Color.FromArgb(0x3B, 0x5C, 0xAA); // Color for the highlights: var colorRed = Color.Red; // The text layout used to render text: TextLayout tl = new TextLayout(72) { MaxWidth = doc.PageSize.Width, MaxHeight = doc.PageSize.Height, MarginLeft = 72, MarginRight = 72, MarginTop = 72, MarginBottom = 72, }; tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")); tl.DefaultFormat.FontSize = 11; var page = doc.NewPage(); addFtr(); var g = page.Graphics; // Caption: tl.TextAlignment = TextAlignment.Center; tl.Append("Introduction\n", new TextFormat() { FontSize = 16, ForeColor = colorBlue }); tl.Append("The Importance of Wetlands", new TextFormat() { FontSize = 13, ForeColor = colorBlue }); tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); // Move below the caption for the first para: tl.MarginTop = tl.ContentHeight + 72 * 2; tl.Clear(); tl.TextAlignment = TextAlignment.Leading; tl.ParagraphSpacing = 12; // For the first para we want a bigger initial letter, but no first line indent, // so we render it separately from the rest of the text: tl.Append(_paras[0].Substring(0, 1), new TextFormat(tl.DefaultFormat) { FontSize = 22 }); addPara(_paras[0].Substring(1)); tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); // Account for the first para, and set up the text layout // for the rest of the text (a TextLayout allows to render multiple paragraphs, // but they all must have the same paragraph format): tl.MarginTop = tl.ContentRectangle.Bottom; tl.Clear(); tl.FirstLineIndent = 36; // Add remaining paragraphs: foreach (var para in _paras.Skip(1)) { // Paragraphs starting with '::' indicate images to be rendered across the page width: if (para.StartsWith("::")) { var img = Image.FromFile(Path.Combine("Resources", "ImagesBis", para.Substring(2))); disposables.Add(img); var w = tl.MaxWidth.Value - tl.MarginLeft - tl.MarginRight; var h = (float)img.Height / (float)img.Width * w; tl.AppendInlineObject(img, w, h); tl.AppendLine(); } else { addPara(para); } } // Layout the paragraphs: tl.PerformLayout(true); // Text split options allow to implement widow and orphan control: var tso = new TextSplitOptions(tl) { RestMarginTop = 72, MinLinesInFirstParagraph = 2, MinLinesInLastParagraph = 2, }; // Image alignment used to render the pictures: var ia = new ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, true, true, true, false, false) { BestFit = true }; // In a loop, split and render the text: while (true) { var splitResult = tl.Split(tso, out TextLayout rest); g = doc.Pages.Last.Graphics; doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty); // Render all images that occurred on this page: foreach (var io in tl.InlineObjects) { doc.Pages.Last.Graphics.DrawImage((Image)io.Object, io.ObjectRect.ToRectangleF(), null, ia); } // Break unless there is more to render: if (splitResult != SplitResult.Split) { break; } // Assign the remaining text to the 'main' TextLayout, add a new page and continue: tl = rest; doc.Pages.Add(); addFtr(); } // Save the PDF: doc.Save(stream); // Dispose images (can be done only after saving the document): disposables.ForEach(d_ => d_.Dispose()); // Done: return(doc.Pages.Count); void addPara(string para) { // We implement a primitive markup to highlight some fragments in red: var txt = para.Split(new string[] { "<red>", "</red>" }, StringSplitOptions.None); for (int i = 0; i < txt.Length; ++i) { if (i % 2 == 0) { tl.Append(txt[i]); } else { tl.Append(txt[i], new TextFormat(tl.DefaultFormat) { ForeColor = colorRed }); } } tl.AppendLine(); } void addFtr() { doc.Pages.Last.Graphics.DrawImage(ftrImg, ftrRc, null, ImageAlign.StretchImage); } }