static void Main(string[] args) { Console.WriteLine("CreateLayer Sample:"); // ReSharper disable once UnusedVariable using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = Library.ResourceDirectory + "Sample_Input/ducky.pdf"; String sOutput = "CreateLayer-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Input file: " + sInput + ", writing to " + sOutput); Document doc = new Document(sInput); Console.WriteLine("Opened a document."); Page pg = doc.GetPage(0); Image img = (pg.Content.GetElement(0) as Image); // Containers, Forms and Annotations can be attached to an // OptionalContentGroup; other content (like Image) can // be made optional by placing it inside a Container Container container = new Container(); container.Content = new Content(); container.Content.AddElement(img); // We replace the Image with the Container // (which now holds the image) pg.Content.RemoveElement(0); pg.UpdateContent(); pg.Content.AddElement(container); pg.UpdateContent(); // We create a new OptionalContentGroup and place it in the // OptionalContentConfig.Order array OptionalContentGroup ocg = CreateNewOptionalContentGroup(doc, "Rubber Ducky"); // Now we associate the Container with the OptionalContentGroup // via an OptionalContentMembershipDict. Note that we MUST // update the Page's content afterwards. AssociateOCGWithContainer(doc, ocg, container); pg.UpdateContent(); doc.Save(SaveFlags.Full, sOutput); } }
// Associate a Container with an OptionalContentGroup via an OptionalContentMembershipDict. // This function associates a Containter with a single OptionalContentGroup and uses // a VisibilityPolicy of AnyOn. public static void AssociateOCGWithContainer(Document doc, OptionalContentGroup ocg, Container cont) { // Create an OptionalContentMembershipDict. The options here are appropriate for a // 'typical' usage; other options can be used to create an 'inverting' layer // (i.e. 'Display this content when the layer is turned OFF'), or to make the // Container's visibility depend on several OptionalContentGroups OptionalContentMembershipDict ocmd = new OptionalContentMembershipDict(doc, new OptionalContentGroup[] { ocg }, VisibilityPolicy.AnyOn); // Associate the Container with the OptionalContentMembershipDict cont.OptionalContentMembershipDict = ocmd; }
private void loadOrder() { PDFArray order = _dictionary["Order"] as PDFArray; if (order == null) { order = new PDFArray(); _dictionary.AddItem("Order", order); } _order = new OptionalContentGroup(order); }
// Create an OptionalContentGroup with a given name, and add it to the // default OptionalContentConfig's Order array. public static OptionalContentGroup CreateNewOptionalContentGroup(Document doc, string name) { // Create an OptionalContentGroup OptionalContentGroup ocg = new OptionalContentGroup(doc, name); // Add it to the Order array -- this is required so that the OptionalContentGroup // will appear in the 'Layers' control panel in Acrobat. It will appear in // the control panel with the name given in the OptionalContentGroup constructor. OptionalContentOrderArray order_list = doc.DefaultOptionalContentConfig.Order; order_list.Insert(order_list.Length, new OptionalContentOrderLeaf(ocg)); return(ocg); }
public Example_30() { PDF pdf = new PDF(new BufferedStream( new FileStream("Example_30.pdf", FileMode.Create))); Page page = new Page(pdf, Letter.PORTRAIT); Font font = new Font(pdf, CoreFont.HELVETICA); Image image = new Image( pdf, new BufferedStream(new FileStream( "images/map407.png", FileMode.Open, FileAccess.Read)), ImageType.PNG); image.SetLocation(10f, 100f); TextLine textLine = new TextLine(font); textLine.SetText("© OpenStreetMap contributors"); textLine.SetLocation(430f, 655f); textLine.DrawOn(page); textLine = new TextLine(font); textLine.SetText("http://www.openstreetmap.org/copyright"); textLine.SetURIAction("http://www.openstreetmap.org/copyright"); textLine.SetLocation(380f, 665f); textLine.DrawOn(page); OptionalContentGroup group = new OptionalContentGroup("Map"); group.Add(image); group.SetVisible(true); // group.SetPrintable(true); group.DrawOn(page); TextBox tb = new TextBox(font); tb.SetText("Hello Text"); tb.SetLocation(300f, 100f); tb = new TextBox(font); tb.SetText("Hello Blue Layer Text"); tb.SetLocation(300f, 200f); Line line = new Line(); line.SetPointA(300f, 250f); line.SetPointB(500f, 250f); line.SetWidth(2f); line.SetColor(Color.blue); group = new OptionalContentGroup("Blue"); group.Add(tb); group.Add(line); // group.SetVisible(true); group.DrawOn(page); line = new Line(); line.SetPointA(300f, 260f); line.SetPointB(500f, 260f); line.SetWidth(2f); line.SetColor(Color.red); image = new Image( pdf, new BufferedStream(new FileStream( "images/BARCODE.PNG", FileMode.Open, FileAccess.Read)), ImageType.PNG); image.SetLocation(10f, 100f); group = new OptionalContentGroup("Barcode"); group.Add(image); group.Add(line); group.SetVisible(true); group.SetPrintable(true); group.DrawOn(page); pdf.Close(); }
static void Main() { // Create new document Document pdfDocument = new Document(); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; // Add page Page page = new Page(PaperFormat.A4); pdfDocument.Pages.Add(page); Canvas canvas = page.Canvas; // Add main layer Layer allContents = new Layer("Main Layer"); canvas.BeginMarkedContent(allContents); // Add sub-layer Layer layer1 = new Layer("Square"); canvas.BeginMarkedContent(layer1); canvas.DrawRectangle(new SolidBrush(new ColorRGB(0, 255, 0)), 20, 20, 100, 100); canvas.EndMarkedContent(); // Add second sub-layer Layer layer2 = new Layer("Circle"); canvas.BeginMarkedContent(layer2); canvas.DrawCircle(new SolidBrush(new ColorRGB(255, 0, 0)), 70, 180, 50); canvas.EndMarkedContent(); // Add third sub-layer Layer layer3 = new Layer("Triangle"); canvas.BeginMarkedContent(layer3); canvas.DrawPolygon(new SolidBrush(new ColorRGB(0, 0, 255)), new PointF[] { new PointF(20, 340), new PointF(70, 240), new PointF(120, 340) }); canvas.EndMarkedContent(); canvas.EndMarkedContent(); pdfDocument.OptionalContents.Layers.Add(layer1); pdfDocument.OptionalContents.Layers.Add(layer2); pdfDocument.OptionalContents.Layers.Add(layer3); pdfDocument.OptionalContents.Layers.Add(allContents); // Configure layers // Group sub-layers OptionalContentGroup group = new OptionalContentGroup(); group.Add(new OptionalContentGroupLayer(layer1)); group.Add(new OptionalContentGroupLayer(layer2)); group.Add(new OptionalContentGroupLayer(layer3)); // Order sub-layers pdfDocument.OptionalContents.Configuration.Order.Add(new OptionalContentGroupLayer(allContents)); pdfDocument.OptionalContents.Configuration.Order.Add(group); // Make the third layer invisible by default (for example) pdfDocument.OptionalContents.Configuration.OFF.Add(layer3); // Force the PDF viewer to show the layers panel initially pdfDocument.PageMode = PageMode.OptionalContent; // Save document to file pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); }