using it.stefanochizzolini.clown.documents; using it.stefanochizzolini.clown.documents.contents.composition; using it.stefanochizzolini.clown.documents.contents.fonts; using it.stefanochizzolini.clown.documents.contents.xObjects; using it.stefanochizzolini.clown.files; using System.Drawing; namespace it.stefanochizzolini.clown.samples { /** This sample demonstrates how to reuse a page as a form (precisely: form XObject [PDF:1.6:4.9]). Form XObjects are a convenient way to represent contents multiple times on multiple pages as templates. */ public class PageToFormSample : ISample { #region dynamic #region interface #region public #region ISample public void Run( PDFClownSampleLoader loader ) { // (boilerplate user choice -- ignore it) string filePath = loader.GetPdfFileChoice("Please select a PDF file to use as form"); // 1. Instantiate the form source file! File formFile = new File(filePath); // 2. Instantiate a new PDF file! /* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */ File file = new File(); // Get its corresponding document! /* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */ Document document = file.Document; // 3. Convert the first page of the source file into a form inside the new document! XObject form = formFile.Document.Pages[0].ToXObject(document); // 4. Insert the contents into the new document! Populate(document,form); // (boilerplate metadata insertion -- ignore it) loader.BuildAccessories(document,this.GetType(),"Page-to-form","converting a page to a reusable form"); // 5. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)! loader.Serialize(file,this.GetType().Name,false); } #endregion #endregion #region private /** Populates a PDF file with contents. */ private void Populate( Document document, XObject form ) { // 1. 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. // 2. Create a content builder for the content stream! PrimitiveFilter builder = new PrimitiveFilter(page); // 3. Inserting contents... Size pageSize = page.Size.Value; // 3.1. Showing the form on the page... { // Form 1. builder.ShowXObject( form, new PointF(pageSize.Width/2,pageSize.Height/2), new SizeF(300,0), AlignmentXEnum.Center, AlignmentYEnum.Middle, 45 ); // Form 2. builder.ShowXObject( form, new PointF(0,pageSize.Height), new SizeF(0,300), AlignmentXEnum.Left, AlignmentYEnum.Bottom, 0 ); // Form 3. builder.ShowXObject( form, new PointF(pageSize.Width,pageSize.Height), new SizeF(80,200), AlignmentXEnum.Right, AlignmentYEnum.Bottom, 0 ); } // 3.2. Showing the comments on the page... { BlockFilter blockFilter = new BlockFilter(builder); RectangleF frame = new RectangleF( 18, 18, pageSize.Width * .5f, pageSize.Height * .5f ); blockFilter.Begin(frame,AlignmentXEnum.Justify,AlignmentYEnum.Top); StandardType1Font bodyFont = new StandardType1Font( document, StandardType1Font.FamilyNameEnum.Courier, true, false ); builder.SetFont(bodyFont,24); blockFilter.ShowText("Page-to-form sample"); SizeF breakSize = new SizeF(0,8); blockFilter.ShowBreak(breakSize); builder.SetFont(bodyFont,8); blockFilter.ShowText("This sample shows how to convert a page to a reusable form that can be placed multiple times on other pages scaling, rotating, anchoring and aligning it."); blockFilter.ShowBreak(breakSize); blockFilter.ShowText("On this page you can see some of the above-mentioned transformations:"); breakSize.Width = 8; blockFilter.ShowBreak(breakSize); blockFilter.ShowText("1. anchored to the center of the page, rotated by 45 degrees counterclockwise, 300 point wide (preserved proportions);"); blockFilter.ShowBreak(breakSize); blockFilter.ShowText("2. anchored to the bottom-left corner of the page, 300 point high (preserved proportions);"); blockFilter.ShowBreak(breakSize); blockFilter.ShowText("3. anchored to the bottom-right of the page, 80 point wide and 200 point high (altered proportions)."); blockFilter.End(); } // 4. Flush the contents into the content stream! builder.Flush(); } #endregion #endregion #endregion } }