private void Stamp( Document document ) { // 1. Instantiate the stamper! /* NOTE: The PageStamper is optimized for dealing with pages. */ PageStamper stamper = new PageStamper(); // 2. Numbering each page... StandardType1Font font = new StandardType1Font( document, StandardType1Font.FamilyEnum.Courier, true, false ); DeviceRGBColor redColor = DeviceRGBColor.Get(SKColors.Red); int margin = 32; foreach (Page page in document.Pages) { // 2.1. Associate the page to the stamper! stamper.Page = page; // 2.2. Stamping the page number on the foreground... { PrimitiveComposer foreground = stamper.Foreground; foreground.SetFont(font, 16); foreground.SetFillColor(redColor); SKSize pageSize = page.Size; int pageNumber = page.Number; bool pageIsEven = (pageNumber % 2 == 0); foreground.ShowText( pageNumber.ToString(), new SKPoint( (pageIsEven ? margin : pageSize.Width - margin), pageSize.Height - margin ), (pageIsEven ? XAlignmentEnum.Left : XAlignmentEnum.Right), YAlignmentEnum.Bottom, 0 ); } // 2.3. End the stamping! stamper.Flush(); } }
private void BuildCurvesPage( Document document ) { // 1. Add the 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. SizeF pageSize = page.Size; // 2. Create a content composer for the page! PrimitiveComposer composer = new PrimitiveComposer(page); // 3. Drawing the page contents... composer.SetFont( new fonts::StandardType1Font( document, fonts::StandardType1Font.FamilyEnum.Courier, true, false ), 32 ); { BlockComposer blockComposer = new BlockComposer(composer); blockComposer.Begin(new RectangleF(30, 0, pageSize.Width - 60, 50), XAlignmentEnum.Center, YAlignmentEnum.Middle); blockComposer.ShowText("Curves"); blockComposer.End(); } // 3.1. Arcs. { float y = 100; for ( int rowIndex = 0; rowIndex < 4; rowIndex++ ) { int angleStep = 45; int startAngle = 0; int endAngle = angleStep; float x = 100; float diameterX; float diameterY; switch (rowIndex) { case 0: default: diameterX = 40; diameterY = 40; break; case 1: diameterX = 40; diameterY = 20; break; case 2: diameterX = 20; diameterY = 40; break; case 3: diameterX = 40; diameterY = 40; break; } for ( int index = 0, length = 360 / angleStep; index < length; index++ ) { RectangleF arcFrame = new RectangleF((float)x, (float)y, (float)diameterX, (float)diameterY); // Drawing the arc frame... composer.BeginLocalState(); composer.SetLineWidth(0.25f); composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3)); composer.DrawRectangle(arcFrame); composer.Stroke(); composer.End(); // Draw the arc! composer.DrawArc(arcFrame, startAngle, endAngle); composer.Stroke(); endAngle += angleStep; switch (rowIndex) { case 3: startAngle += angleStep; break; } x += 50; } y += diameterY + 10; } } // 3.2. Circle. { RectangleF arcFrame = new RectangleF(100, 300, 100, 100); // Drawing the circle frame... composer.BeginLocalState(); composer.SetLineWidth(0.25f); composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3)); composer.DrawRectangle(arcFrame); composer.Stroke(); composer.End(); // Drawing the circle... composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Red)); composer.DrawEllipse(arcFrame); composer.FillStroke(); } // 3.3. Horizontal ellipse. { RectangleF arcFrame = new RectangleF(210, 300, 100, 50); // Drawing the ellipse frame... composer.BeginLocalState(); composer.SetLineWidth(0.25f); composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3)); composer.DrawRectangle(arcFrame); composer.Stroke(); composer.End(); // Drawing the ellipse... composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Green)); composer.DrawEllipse(arcFrame); composer.FillStroke(); } // 3.4. Vertical ellipse. { RectangleF arcFrame = new RectangleF(320, 300, 50, 100); // Drawing the ellipse frame... composer.BeginLocalState(); composer.SetLineWidth(0.25f); composer.SetLineDash(new LineDash(new double[] { 5, 5 }, 3)); composer.DrawRectangle(arcFrame); composer.Stroke(); composer.End(); // Drawing the ellipse... composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Blue)); composer.DrawEllipse(arcFrame); composer.FillStroke(); } // 3.5. Spirals. { float y = 500; float spiralWidth = 100; composer.SetLineWidth(.5f); for ( int rowIndex = 0; rowIndex < 3; rowIndex++ ) { float x = 150; float branchWidth = .5f; float branchRatio = 1; for ( int spiralIndex = 0; spiralIndex < 4; spiralIndex++ ) { float spiralTurnsCount; switch (rowIndex) { case 0: default: spiralTurnsCount = spiralWidth / (branchWidth * 8); break; case 1: spiralTurnsCount = (float)(spiralWidth / (branchWidth * 8 * (spiralIndex * 1.15 + 1))); break; } switch (rowIndex) { case 2: composer.SetLineDash(new LineDash(new double[] { 10, 5 })); composer.SetLineCap(LineCapEnum.Round); break; default: break; } composer.DrawSpiral( new PointF((float)x, (float)y), 0, 360 * spiralTurnsCount, branchWidth, branchRatio ); composer.Stroke(); x += spiralWidth + 10; switch (rowIndex) { case 0: default: branchWidth += 1; break; case 1: branchRatio += .035f; break; } switch (rowIndex) { case 2: composer.SetLineWidth(composer.State.LineWidth + .5f); break; } } y += spiralWidth + 10; } } // 4. Flush the contents into the page! composer.Flush(); }
private void Populate(Document document) { Page page = new Page(document); document.Pages.Add(page); PrimitiveComposer composer = new PrimitiveComposer(page); fonts::StandardType1Font font = new fonts::StandardType1Font(document, fonts::StandardType1Font.FamilyEnum.Courier, true, false); composer.SetFont(font, 12); // Sticky note. composer.ShowText("Sticky note annotation:", new SKPoint(35, 35)); new StickyNote(page, new SKPoint(50, 50), "Text of the Sticky note annotation") { IconType = StickyNote.IconTypeEnum.Note, Color = DeviceRGBColor.Get(SKColors.Yellow), Popup = new Popup( page, SKRect.Create(200, 25, 200, 75), "Text of the Popup annotation (this text won't be visible as associating popups to markup annotations overrides the former's properties with the latter's)" ), Author = "Stefano", Subject = "Sticky note", IsOpen = true }; new StickyNote(page, new SKPoint(80, 50), "Text of the Help sticky note annotation") { IconType = StickyNote.IconTypeEnum.Help, Color = DeviceRGBColor.Get(SKColors.Pink), Author = "Stefano", Subject = "Sticky note", Popup = new Popup( page, SKRect.Create(400, 25, 200, 75), "Text of the Popup annotation (this text won't be visible as associating popups to markup annotations overrides the former's properties with the latter's)" ) }; new StickyNote(page, new SKPoint(110, 50), "Text of the Comment sticky note annotation") { IconType = StickyNote.IconTypeEnum.Comment, Color = DeviceRGBColor.Get(SKColors.Green), Author = "Stefano", Subject = "Sticky note" }; new StickyNote(page, new SKPoint(140, 50), "Text of the Key sticky note annotation") { IconType = StickyNote.IconTypeEnum.Key, Color = DeviceRGBColor.Get(SKColors.Blue), Author = "Stefano", Subject = "Sticky note" }; // Callout. composer.ShowText("Callout note annotation:", new SKPoint(35, 85)); new FreeText(page, SKRect.Create(250, 90, 150, 70), "Text of the Callout note annotation") { Line = new FreeText.CalloutLine( page, new SKPoint(100, 100), new SKPoint(150, 125), new SKPoint(250, 125) ), Type = FreeText.TypeEnum.Callout, LineEndStyle = LineEndStyleEnum.OpenArrow, Border = new Border(1), Color = DeviceRGBColor.Get(SKColors.Yellow) }; // File attachment. composer.ShowText("File attachment annotation:", new SKPoint(35, 135)); new FileAttachment( page, SKRect.Create(50, 150, 15, 20), "Text of the File attachment annotation", FileSpecification.Get( EmbeddedFile.Get(document, GetResourcePath("images" + Path.DirectorySeparatorChar + "gnu.jpg")), "happyGNU.jpg") ) { IconType = FileAttachment.IconTypeEnum.PaperClip, Author = "Stefano", Subject = "File attachment" }; composer.ShowText("Line annotation:", new SKPoint(35, 185)); { composer.BeginLocalState(); composer.SetFont(font, 10); // Arrow line. composer.ShowText("Arrow:", new SKPoint(50, 200)); new Line( page, new SKPoint(50, 260), new SKPoint(200, 210), "Text of the Arrow line annotation", DeviceRGBColor.Get(SKColors.Black)) { StartStyle = LineEndStyleEnum.Circle, EndStyle = LineEndStyleEnum.ClosedArrow, CaptionVisible = true, FillColor = DeviceRGBColor.Get(SKColors.Green), Author = "Stefano", Subject = "Arrow line" }; // Dimension line. composer.ShowText("Dimension:", new SKPoint(300, 200)); new Line( page, new SKPoint(300, 220), new SKPoint(500, 220), "Text of the Dimension line annotation", DeviceRGBColor.Get(SKColors.Blue) ) { LeaderLineLength = 20, LeaderLineExtensionLength = 10, StartStyle = LineEndStyleEnum.OpenArrow, EndStyle = LineEndStyleEnum.OpenArrow, Border = new Border(1), CaptionVisible = true, Author = "Stefano", Subject = "Dimension line" }; composer.End(); } var path = new SKPath(); path.MoveTo(new SKPoint(50, 320)); path.LineTo(new SKPoint(70, 305)); path.LineTo(new SKPoint(110, 335)); path.LineTo(new SKPoint(130, 320)); path.LineTo(new SKPoint(110, 305)); path.LineTo(new SKPoint(70, 335)); path.LineTo(new SKPoint(50, 320)); // Scribble. composer.ShowText("Scribble annotation:", new SKPoint(35, 285)); new Scribble( page, new List <SKPath> { path }, "Text of the Scribble annotation", DeviceRGBColor.Get(SKColors.Orange)) { Border = new Border(1, new LineDash(new double[] { 5, 2, 2, 2 })), Author = "Stefano", Subject = "Scribble" }; // Rectangle. composer.ShowText("Rectangle annotation:", new SKPoint(35, 350)); new PdfClown.Documents.Interaction.Annotations.Rectangle( page, SKRect.Create(50, 370, 100, 30), "Text of the Rectangle annotation") { Color = DeviceRGBColor.Get(SKColors.Red), Border = new Border(1, new LineDash(new double[] { 5 })), Author = "Stefano", Subject = "Rectangle", Popup = new Popup( page, SKRect.Create(200, 325, 200, 75), "Text of the Popup annotation (this text won't be visible as associating popups to markup annotations overrides the former's properties with the latter's)" ) }; // Ellipse. composer.ShowText("Ellipse annotation:", new SKPoint(35, 415)); new Ellipse( page, SKRect.Create(50, 440, 100, 30), "Text of the Ellipse annotation") { BorderEffect = new BorderEffect(BorderEffect.TypeEnum.Cloudy, 1), FillColor = DeviceRGBColor.Get(SKColors.Cyan), Color = DeviceRGBColor.Get(SKColors.Black), Author = "Stefano", Subject = "Ellipse" }; // Rubber stamp. composer.ShowText("Rubber stamp annotations:", new SKPoint(35, 505)); { fonts::Font stampFont = fonts::Font.Get(document, GetResourcePath("fonts" + Path.DirectorySeparatorChar + "TravelingTypewriter.otf")); new Stamp( page, new SKPoint(75, 570), "This is a round custom stamp", new StampAppearanceBuilder(document, StampAppearanceBuilder.TypeEnum.Round, "Done", 50, stampFont) .Build() ) { Rotation = -10, Author = "Stefano", Subject = "Custom stamp" }; new Stamp( page, new SKPoint(210, 570), "This is a squared (and round-cornered) custom stamp", new StampAppearanceBuilder(document, StampAppearanceBuilder.TypeEnum.Squared, "Classified", 150, stampFont) { Color = DeviceRGBColor.Get(SKColors.Orange) }.Build() ) { Rotation = 15, Author = "Stefano", Subject = "Custom stamp" }; fonts::Font stampFont2 = fonts::Font.Get(document, GetResourcePath("fonts" + Path.DirectorySeparatorChar + "MgOpenCanonicaRegular.ttf")); new Stamp( page, new SKPoint(350, 570), "This is a striped custom stamp", new StampAppearanceBuilder(document, StampAppearanceBuilder.TypeEnum.Striped, "Out of stock", 100, stampFont2) { Color = DeviceRGBColor.Get(SKColors.Gray) }.Build() ) { Rotation = 90, Author = "Stefano", Subject = "Custom stamp" }; // Define the standard stamps template path! /* * NOTE: The PDF specification defines several stamps (aka "standard stamps") whose rendering * depends on the support of viewer applications. As such support isn't guaranteed, PDF Clown * offers smooth, ready-to-use embedding of these stamps through the StampPath property of the * document configuration: you can decide to point to the stamps directory of your Acrobat * installation (e.g., in my GNU/Linux system it's located in * "/opt/Adobe/Reader9/Reader/intellinux/plug_ins/Annotations/Stamps/ENU") or to the * collection included in this distribution (std-stamps.pdf). */ document.Configuration.StampPath = GetResourcePath("../../pkg/templates/std-stamps.pdf"); // Add a standard stamp, rotating it 15 degrees counterclockwise! new Stamp( page, new SKPoint(485, 515), null, // Default size is natural size. "This is 'Confidential', a standard stamp", StandardStampEnum.Confidential) { Rotation = 15, Author = "Stefano", Subject = "Standard stamp" }; // Add a standard stamp, without rotation! new Stamp( page, new SKPoint(485, 580), null, // Default size is natural size. "This is 'SBApproved', a standard stamp", StandardStampEnum.BusinessApproved) { Author = "Stefano", Subject = "Standard stamp" }; // Add a standard stamp, rotating it 10 degrees clockwise! new Stamp( page, new SKPoint(485, 635), new SKSize(0, 40), // This scales the width proportionally to the 40-unit height (you can obviously do also the opposite, defining only the width). "This is 'SHSignHere', a standard stamp", StandardStampEnum.SignHere) { Rotation = -10, Author = "Stefano", Subject = "Standard stamp" }; } composer.ShowText("Text markup annotations:", new SKPoint(35, 650)); { composer.BeginLocalState(); composer.SetFont(font, 8); new TextMarkup( page, composer.ShowText("Highlight annotation", new SKPoint(35, 680)), "Text of the Highlight annotation", TextMarkup.MarkupTypeEnum.Highlight) { Author = "Stefano", Subject = "An highlight text markup!" }; new TextMarkup( page, composer.ShowText("Highlight annotation 2", new SKPoint(35, 695)).Inflate(0, 1), "Text of the Highlight annotation 2", TextMarkup.MarkupTypeEnum.Highlight) { Color = DeviceRGBColor.Get(SKColors.Magenta) }; new TextMarkup( page, composer.ShowText("Highlight annotation 3", new SKPoint(35, 710)).Inflate(0, 2), "Text of the Highlight annotation 3", TextMarkup.MarkupTypeEnum.Highlight) { Color = DeviceRGBColor.Get(SKColors.Red) }; new TextMarkup( page, composer.ShowText("Squiggly annotation", new SKPoint(180, 680)), "Text of the Squiggly annotation", TextMarkup.MarkupTypeEnum.Squiggly); new TextMarkup( page, composer.ShowText("Squiggly annotation 2", new SKPoint(180, 695)).Inflate(0, 2.5f), "Text of the Squiggly annotation 2", TextMarkup.MarkupTypeEnum.Squiggly) { Color = DeviceRGBColor.Get(SKColors.Orange) }; new TextMarkup( page, composer.ShowText("Squiggly annotation 3", new SKPoint(180, 710)).Inflate(0, 3), "Text of the Squiggly annotation 3", TextMarkup.MarkupTypeEnum.Squiggly) { Color = DeviceRGBColor.Get(SKColors.Pink) }; new TextMarkup( page, composer.ShowText("Underline annotation", new SKPoint(320, 680)), "Text of the Underline annotation", TextMarkup.MarkupTypeEnum.Underline ); new TextMarkup( page, composer.ShowText("Underline annotation 2", new SKPoint(320, 695)).Inflate(0, 2.5f), "Text of the Underline annotation 2", TextMarkup.MarkupTypeEnum.Underline ) { Color = DeviceRGBColor.Get(SKColors.Orange) }; new TextMarkup( page, composer.ShowText("Underline annotation 3", new SKPoint(320, 710)).Inflate(0, 3), "Text of the Underline annotation 3", TextMarkup.MarkupTypeEnum.Underline ) { Color = DeviceRGBColor.Get(SKColors.Green) }; new TextMarkup( page, composer.ShowText("StrikeOut annotation", new SKPoint(455, 680)), "Text of the StrikeOut annotation", TextMarkup.MarkupTypeEnum.StrikeOut ); new TextMarkup( page, composer.ShowText("StrikeOut annotation 2", new SKPoint(455, 695)).Inflate(0, 2.5f), "Text of the StrikeOut annotation 2", TextMarkup.MarkupTypeEnum.StrikeOut ) { Color = DeviceRGBColor.Get(SKColors.Orange) }; new TextMarkup( page, composer.ShowText("StrikeOut annotation 3", new SKPoint(455, 710)).Inflate(0, 3), "Text of the StrikeOut annotation 3", TextMarkup.MarkupTypeEnum.StrikeOut ) { Color = DeviceRGBColor.Get(SKColors.Green) }; composer.End(); } composer.Flush(); }
private void BuildLinks(Document document) { Pages pages = document.Pages; Page page = new Page(document); pages.Add(page); PrimitiveComposer composer = new PrimitiveComposer(page); BlockComposer blockComposer = new BlockComposer(composer); PdfType1Font font = PdfType1Font.Load(document, PdfType1Font.FamilyEnum.Courier, true, false); /* * 2.1. Goto-URI link. */ { blockComposer.Begin(SKRect.Create(30, 100, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle); composer.SetFont(font, 12); blockComposer.ShowText("Go-to-URI link"); composer.SetFont(font, 8); blockComposer.ShowText("\nIt allows you to navigate to a network resource."); composer.SetFont(font, 5); blockComposer.ShowText("\n\nClick on the box to go to the project's SourceForge.net repository."); blockComposer.End(); /* * NOTE: This statement instructs the PDF viewer to navigate to the given URI when the link is clicked. */ new Link(page, SKRect.Create(240, 100, 100, 50), "Link annotation", new GoToURI(document, new Uri("http://www.sourceforge.net/projects/clown"))) { Border = new Border(3, BorderStyleType.Beveled) }; } /* * 2.2. Embedded-goto link. */ { string filePath = PromptFileChoice("Please select a PDF file to attach"); /* * NOTE: These statements instruct PDF Clown to attach a PDF file to the current document. * This is necessary in order to test the embedded-goto functionality, * as you can see in the following link creation (see below). */ int fileAttachmentPageIndex = page.Index; string fileAttachmentName = "attachedSamplePDF"; string fileName = System.IO.Path.GetFileName(filePath); new FileAttachment( page, SKRect.Create(0, -20, 10, 10), "File attachment annotation", FileSpecification.Get(EmbeddedFile.Get(document, filePath), fileName ) ) { Name = fileAttachmentName, AttachmentName = FileAttachmentImageType.PaperClip }; blockComposer.Begin(SKRect.Create(30, 170, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle); composer.SetFont(font, 12); blockComposer.ShowText("Go-to-embedded link"); composer.SetFont(font, 8); blockComposer.ShowText("\nIt allows you to navigate to a destination within an embedded PDF file."); composer.SetFont(font, 5); blockComposer.ShowText("\n\nClick on the button to go to the 2nd page of the attached PDF file (" + fileName + ")."); blockComposer.End(); /* * NOTE: This statement instructs the PDF viewer to navigate to the page 2 of a PDF file * attached inside the current document as described by the FileAttachment annotation on page 1 of the current document. */ new Link( page, SKRect.Create(240, 170, 100, 50), "Link annotation", new GoToEmbedded( document, new GoToEmbedded.PathElement( document, fileAttachmentPageIndex, // Page of the current document containing the file attachment annotation of the target document. fileAttachmentName, // Name of the file attachment annotation corresponding to the target document. null // No sub-target. ), // Target represents the document to go to. new RemoteDestination( document, 1, // Show the page 2 of the target document. Destination.ModeEnum.Fit, // Show the target document page entirely on the screen. null, null ) // The destination must be within the target document. ) ) { Border = new Border(1, new LineDash(new float[] { 8, 5, 2, 5 })) }; } /* * 2.3. Textual link. */ { blockComposer.Begin(SKRect.Create(30, 240, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle); composer.SetFont(font, 12); blockComposer.ShowText("Textual link"); composer.SetFont(font, 8); blockComposer.ShowText("\nIt allows you to expose any kind of link (including the above-mentioned types) as text."); composer.SetFont(font, 5); blockComposer.ShowText("\n\nClick on the text links to go either to the project's SourceForge.net repository or to the project's home page."); blockComposer.End(); composer.BeginLocalState(); composer.SetFont(font, 10); composer.SetFillColor(DeviceRGBColor.Get(SKColors.Blue)); composer.ShowText( "PDF Clown Project's repository at SourceForge.net", new SKPoint(240, 265), XAlignmentEnum.Left, YAlignmentEnum.Middle, 0, new GoToURI( document, new Uri("http://www.sourceforge.net/projects/clown") ) ); composer.ShowText( "PDF Clown Project's home page", new SKPoint(240, 285), XAlignmentEnum.Left, YAlignmentEnum.Bottom, -90, new GoToURI( document, new Uri("http://www.pdfclown.org") ) ); composer.End(); } composer.Flush(); }