/** <summary>Populates a PDF file with contents.</summary> */ private void Populate( Document document ) { // Initialize a new page! Page page = new Page(document); document.Pages.Add(page); // Initialize the primitive composer (within the new page context)! PrimitiveComposer composer = new PrimitiveComposer(page); composer.SetFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Helvetica, true, false), 12); // Initialize the block composer (wrapping the primitive one)! BlockComposer blockComposer = new BlockComposer(composer); // Initialize the document layer configuration! LayerDefinition layerDefinition = document.Layer; document.PageMode = Document.PageModeEnum.Layers; // Shows the layers tab on document opening. // Get the root layers collection! Layers rootLayers = layerDefinition.Layers; // 1. Nested layers. { Layer nestedLayer = new Layer(document, "Nested layers"); rootLayers.Add(nestedLayer); Layers nestedSubLayers = nestedLayer.Layers; Layer nestedLayer1 = new Layer(document, "Nested layer 1"); nestedSubLayers.Add(nestedLayer1); Layer nestedLayer2 = new Layer(document, "Nested layer 2"); nestedSubLayers.Add(nestedLayer2); nestedLayer2.Locked = true; /* NOTE: Text in this section is shown using PrimitiveComposer. */ composer.BeginLayer(nestedLayer); composer.ShowText(nestedLayer.Title, new PointF(50, 50)); composer.End(); composer.BeginLayer(nestedLayer1); composer.ShowText(nestedLayer1.Title, new PointF(50, 75)); composer.End(); composer.BeginLayer(nestedLayer2); composer.ShowText(nestedLayer2.Title, new PointF(50, 100)); composer.End(); } // 2. Simple group (labeled group of non-nested, inclusive-state layers). { Layers simpleGroup = new Layers(document, "Simple group"); rootLayers.Add(simpleGroup); Layer layer1 = new Layer(document, "Grouped layer 1"); simpleGroup.Add(layer1); Layer layer2 = new Layer(document, "Grouped layer 2"); simpleGroup.Add(layer2); /* NOTE: Text in this section is shown using BlockComposer along with PrimitiveComposer to demonstrate their flexible cooperation. */ blockComposer.Begin(new RectangleF(50, 125, 200, 50), XAlignmentEnum.Left, YAlignmentEnum.Middle); composer.BeginLayer(layer1); blockComposer.ShowText(layer1.Title); composer.End(); blockComposer.ShowBreak(new SizeF(0, 15)); composer.BeginLayer(layer2); blockComposer.ShowText(layer2.Title); composer.End(); blockComposer.End(); } // 3. Radio group (labeled group of non-nested, exclusive-state layers). { Layers radioGroup = new Layers(document, "Radio group"); rootLayers.Add(radioGroup); Layer radio1 = new Layer(document, "Radiogrouped layer 1"); radioGroup.Add(radio1); radio1.Visible = true; Layer radio2 = new Layer(document, "Radiogrouped layer 2"); radioGroup.Add(radio2); radio2.Visible = false; Layer radio3 = new Layer(document, "Radiogrouped layer 3"); radioGroup.Add(radio3); radio3.Visible = false; // Register this option group in the layer configuration! LayerGroup options = new LayerGroup(document); options.Add(radio1); options.Add(radio2); options.Add(radio3); layerDefinition.OptionGroups.Add(options); /* NOTE: Text in this section is shown using BlockComposer along with PrimitiveComposer to demonstrate their flexible cooperation. */ blockComposer.Begin(new RectangleF(50, 185, 200, 75), XAlignmentEnum.Left, YAlignmentEnum.Middle); composer.BeginLayer(radio1); blockComposer.ShowText(radio1.Title); composer.End(); blockComposer.ShowBreak(new SizeF(0, 15)); composer.BeginLayer(radio2); blockComposer.ShowText(radio2.Title); composer.End(); blockComposer.ShowBreak(new SizeF(0, 15)); composer.BeginLayer(radio3); blockComposer.ShowText(radio3.Title); composer.End(); blockComposer.End(); } // 4. Print-only layer. { Layer printOnlyLayer = new Layer(document, "Print-only layer"); printOnlyLayer.Visible = false; printOnlyLayer.Printable = true; printOnlyLayer.Locked = true; rootLayers.Add(printOnlyLayer); composer.BeginLayer(printOnlyLayer); composer.ShowText(printOnlyLayer.Title, new PointF(50, 270)); composer.End(); } composer.Flush(); }
public static void Main(string[] args) { if (args.Length != 3) { Console.WriteLine ("Usage: NbTest <background.pdf> <text> <output.pdf>"); return; } if (!File.Exists (args [0])) { Console.Error.WriteLine ("Invalid text filename, it doesn't exist."); return; } string text = args [1]; PDFFiles.File file = new PDFFiles.File (args [0]); Document document = file.Document; PageStamper stamper = new PageStamper (document.Pages [0]); PrimitiveComposer composer = stamper.Foreground; PDFFonts.Font font = PDFFonts.Font.Get (document, FONT_NAME); if (document.Layer == null) { document.Layer = new LayerDefinition (document); } document.PageMode = Document.PageModeEnum.Layers; Layer layer = new Layer(document, "Personalization"); document.Layer.Layers.Add(layer); var margin = new RectangleF(MilimitersToPoints(2), MilimitersToPoints(1), MilimitersToPoints(6), MilimitersToPoints(1)); //var frame = new RectangleF(MilimitersToPoints(0), MilimitersToPoints(179), // MilimitersToPoints(255), MilimitersToPoints(26)); var frame = new RectangleF(MilimitersToPoints(105), MilimitersToPoints(155), MilimitersToPoints(100), MilimitersToPoints(26)); var text_frame = new RectangleF(frame.X + margin.X, frame.Y + margin.Y, frame.Width - margin.Right, frame.Height - margin.Bottom); int font_size = GetFontSizeToFit(text, text_frame); composer.BeginLayer(layer); //composer.Rotate(90, new PointF(0, MilimitersToPoints(255))); composer.SetFillColor(new DeviceRGBColor(86f/255,175f/255,49f/255)); composer.DrawRectangle(frame); composer.Fill(); composer.SetFillColor(DeviceRGBColor.White); composer.SetFont(font, font_size); BlockComposer blockComposer = new BlockComposer(composer); blockComposer.Begin(text_frame, AlignmentXEnum.Center, AlignmentYEnum.Middle); blockComposer.ShowText(text); blockComposer.End(); composer.End(); stamper.Flush(); file.Save(args [2], PDFFiles.SerializationModeEnum.Standard); file.Dispose(); }