using it.stefanochizzolini.clown.documents; using it.stefanochizzolini.clown.documents.contents.colorSpaces; using it.stefanochizzolini.clown.documents.contents.composition; using fonts = it.stefanochizzolini.clown.documents.contents.fonts; using files = it.stefanochizzolini.clown.files; using System; using System.Drawing; using System.IO; namespace it.stefanochizzolini.clown.samples { /** This sample concentrates on proper fitting of styled text within a given page area (block frame), from the beginning of "Alice in Wonderland", Chapter 1 ("Down the Rabbit-Hole"). */ public class TypesettingSample : ISample { #region static #region fields private static readonly int Margin_X = 50; private static readonly int Margin_Y = 50; #endregion #endregion #region dynamic #region interface #region public #region ISample public void Run( PDFClownSampleLoader loader ) { // 1. PDF file instantiation. files::File file = new files::File(); // 2. Content creation. // Get the document associated to the file! Document document = file.Document; // Set default page size (A4)! document.PageSize = PageFormat.GetSize(); // Populate the document! Build(document,loader); // (boilerplate metadata insertion -- ignore it) loader.BuildAccessories(document,this.GetType(),"Typesetting","demonstrating how to add style to contents"); // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)! loader.Serialize(file,this.GetType().Name,false); } #endregion #endregion #region private private void Build( Document document, PDFClownSampleLoader loader ) { // Add a page to the document! Page page = new Page(document); // Instantiates the page inside the document context. document.Pages.Add(page); // Puts the page in the pages collection. Size pageSize = page.Size.Value; // Create a content builder for the content stream! /* NOTE: There are several ways to add contents to a content stream: - adding content objects directly to the Contents collection; - invoking basic drawing functions through a PrimitiveFilter instance; - invoking advanced static-positioning functions through a BlockFilter instance; - invoking advanced dynamic-positioning functions through a FlowFilter instance (currently not implemented yet). */ PrimitiveFilter builder = new PrimitiveFilter(page); // Wrap the content builder within a block filter! /* NOTE: The block filter is a basic typesetter. It exposes higher-level graphical functionalities (horizontal/vertical alignment, indentation, paragraph composition etc.) leveraging the content builder primitives. It's important to note that this is just an intermediate abstraction layer of the typesetting stack: further abstract levels could sit upon it, allowing the convenient treatment of typographic entities like titles, paragraphs, columns, tables, headers, footers etc. When such further abstract levels are available, the final user (developer of consuming applications) won't care any more of the details you can see here in the following code lines (such as bothering to select the first-letter font...). */ BlockFilter blockFilter = new BlockFilter(builder); builder.BeginLocalState(); // Define the block frame that will constrain our contents on the page canvas! RectangleF frame = new RectangleF( Margin_X, Margin_Y, (float)pageSize.Width - Margin_X * 2, (float)pageSize.Height - Margin_Y * 2 ); // Begin the title block! blockFilter.Begin(frame,AlignmentXEnum.Left,AlignmentYEnum.Top); fonts::OpenTypeFont decorativeFont = new fonts::OpenTypeFont( document, loader.InputPath + Path.DirectorySeparatorChar + "fonts" + Path.DirectorySeparatorChar + "Ruritania-Outline.ttf" ); builder.SetFont(decorativeFont,56); blockFilter.ShowText("Chapter 1"); blockFilter.ShowBreak(); builder.SetFont(decorativeFont,32); blockFilter.ShowText("Down the Rabbit-Hole"); // End the title block! blockFilter.End(); // Update the block frame in order to begin after the title! frame = new RectangleF( (float)blockFilter.BoundBox.X, (float)blockFilter.BoundBox.Y + blockFilter.BoundBox.Height, (float)blockFilter.BoundBox.Width, (float)pageSize.Height - Margin_Y - (blockFilter.BoundBox.Y + blockFilter.BoundBox.Height) ); // Begin the body block! blockFilter.Begin(frame,AlignmentXEnum.Justify,AlignmentYEnum.Bottom); fonts::OpenTypeFont bodyFont = new fonts::OpenTypeFont( document, loader.InputPath + Path.DirectorySeparatorChar + "fonts" + Path.DirectorySeparatorChar + "TravelingTypewriter.otf" ); builder.SetFont(bodyFont,14); builder.BeginLocalState(); builder.SetFont(decorativeFont,28); blockFilter.ShowText("A"); builder.End(); blockFilter.ShowText("lice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversation?'"); // Define new-paragraph first-line offset! SizeF breakSize = new SizeF(24,8); // Indentation (24pt) and top margin (8pt). // Begin a new paragraph! blockFilter.ShowBreak(breakSize); blockFilter.ShowText("So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her."); // Begin a new paragraph! blockFilter.ShowBreak(breakSize); blockFilter.ShowText("There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge."); // End the body block! blockFilter.End(); builder.End(); builder.BeginLocalState(); builder.SetFont(bodyFont,8); builder.Translate(pageSize.Width - 20, 20); builder.Rotate(90); builder.ShowText("Generated by PDF Clown on " + System.DateTime.Now); builder.Translate(0,-8); builder.ShowText("For more info, visit http://clown.stefanochizzolini.it"); builder.End(); builder.Flush(); } #endregion #endregion #endregion } }