static void Main(string[] args) { PDFNet.Initialize(); try { using (PDFDoc doc = new PDFDoc()) { Page page = doc.PageCreate(); doc.PagePushBack(page); Obj annots = doc.CreateIndirectArray(); page.GetSDFObj().Put("Annots", annots); Create3DAnnotation(doc, annots); doc.Save(output_path + "dice_u3d.pdf", SDFDoc.SaveOptions.e_linearized); } Console.WriteLine("Done"); } catch (PDFNetException e) { Console.WriteLine(e.Message); } }
// A utility function used to add new Content Groups (Layers) to the document. static Group CreateLayer(PDFDoc doc, String layer_name) { Group grp = Group.Create(doc, layer_name); Config cfg = doc.GetOCGConfig(); if (cfg == null) { cfg = Config.Create(doc, true); cfg.SetName("Default"); } // Add the new OCG to the list of layers that should appear in PDF viewer GUI. Obj layer_order_array = cfg.GetOrder(); if (layer_order_array == null) { layer_order_array = doc.CreateIndirectArray(); cfg.SetOrder(layer_order_array); } layer_order_array.PushBack(grp.GetSDFObj()); return(grp); }
static void AnnotationLowLevelAPI(PDFDoc doc) { Page page = doc.GetPage(1); Obj annots = page.GetAnnots(); if (annots == null) { // If there are no annotations, create a new annotation // array for the page. annots = doc.CreateIndirectArray(); page.GetSDFObj().Put("Annots", annots); } // Create the Text annotation Obj text_annot = doc.CreateIndirectDict(); text_annot.PutName("Subtype", "Text"); text_annot.PutBool("Open", true); text_annot.PutString("Contents", "The quick brown fox ate the lazy mouse."); text_annot.PutRect("Rect", 266, 116, 430, 204); // Insert the annotation in the page annotation array annots.PushBack(text_annot); // Create a Link annotation Obj link1 = doc.CreateIndirectDict(); link1.PutName("Subtype", "Link"); Destination dest = Destination.CreateFit(doc.GetPage(2)); link1.Put("Dest", dest.GetSDFObj()); link1.PutRect("Rect", 85, 705, 503, 661); annots.PushBack(link1); // Create another Link annotation Obj link2 = doc.CreateIndirectDict(); link2.PutName("Subtype", "Link"); Destination dest2 = Destination.CreateFit(doc.GetPage(3)); link2.Put("Dest", dest2.GetSDFObj()); link2.PutRect("Rect", 85, 638, 503, 594); annots.PushBack(link2); // Note that PDFNet APi can be used to modify existing annotations. // In the following example we will modify the second link annotation // (link2) so that it points to the 10th page. We also use a different // destination page fit type. link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj()); // Create a third link annotation with a hyperlink action (all other // annotation types can be created in a similar way) Obj link3 = doc.CreateIndirectDict(); link3.PutName("Subtype", "Link"); link3.PutRect("Rect", 85, 570, 503, 524); // Create a URI action Obj action = link3.PutDict("A"); action.PutName("S", "URI"); action.PutString("URI", "http://www.pdftron.com"); annots.PushBack(link3); }
static void Main(string[] args) { PDFNet.Initialize(); try { using (PDFDoc doc = new PDFDoc()) using (ElementBuilder builder = new ElementBuilder()) // ElementBuilder is used to build new Element objects using (ElementWriter writer = new ElementWriter()) // ElementWriter is used to write Elements to the page { // Create three layers... Group image_layer = CreateLayer(doc, "Image Layer"); Group text_layer = CreateLayer(doc, "Text Layer"); Group vector_layer = CreateLayer(doc, "Vector Layer"); // Start a new page ------------------------------------ Page page = doc.PageCreate(); writer.Begin(page); // begin writing to this page // Add new content to the page and associate it with one of the layers. Element element = builder.CreateForm(CreateGroup1(doc, image_layer.GetSDFObj())); writer.WriteElement(element); element = builder.CreateForm(CreateGroup2(doc, vector_layer.GetSDFObj())); writer.WriteElement(element); // Add the text layer to the page... bool enableOCMD = false; // set to 'true' to enable 'ocmd' example. if (enableOCMD) { // A bit more advanced example of how to create an OCMD text layer that // is visible only if text, image and path layers are all 'ON'. // An example of how to set 'Visibility Policy' in OCMD. Obj ocgs = doc.CreateIndirectArray(); ocgs.PushBack(image_layer.GetSDFObj()); ocgs.PushBack(vector_layer.GetSDFObj()); ocgs.PushBack(text_layer.GetSDFObj()); OCMD text_ocmd = OCMD.Create(doc, ocgs, OCMD.VisibilityPolicyType.e_AllOn); element = builder.CreateForm(CreateGroup3(doc, text_ocmd.GetSDFObj())); } else { element = builder.CreateForm(CreateGroup3(doc, text_layer.GetSDFObj())); } writer.WriteElement(element); // Add some content to the page that does not belong to any layer... // In this case this is a rectangle representing the page border. element = builder.CreateRect(0, 0, page.GetPageWidth(), page.GetPageHeight()); element.SetPathFill(false); element.SetPathStroke(true); element.GetGState().SetLineWidth(40); writer.WriteElement(element); writer.End(); // save changes to the current page doc.PagePushBack(page); // Set the default viewing preference to display 'Layer' tab. PDFDocViewPrefs prefs = doc.GetViewPrefs(); prefs.SetPageMode(PDFDocViewPrefs.PageMode.e_UseOC); doc.Save(output_path + "pdf_layers.pdf", SDFDoc.SaveOptions.e_linearized); Console.WriteLine("Done."); } } catch (PDFNetException e) { Console.WriteLine(e.Message); } // The following is a code snippet shows how to selectively render // and export PDF layers. try { using (PDFDoc doc = new PDFDoc(output_path + "pdf_layers.pdf")) { doc.InitSecurityHandler(); if (!doc.HasOC()) { Console.WriteLine("The document does not contain 'Optional Content'"); } else { Config init_cfg = doc.GetOCGConfig(); Context ctx = new Context(init_cfg); using (PDFDraw pdfdraw = new PDFDraw()) { pdfdraw.SetImageSize(1000, 1000); pdfdraw.SetOCGContext(ctx); // Render the page using the given OCG context. Page page = doc.GetPage(1); // Get the first page in the document. pdfdraw.Export(page, output_path + "pdf_layers_default.png"); // Disable drawing of content that is not optional (i.e. is not part of any layer). ctx.SetNonOCDrawing(false); // Now render each layer in the input document to a separate image. Obj ocgs = doc.GetOCGs(); // Get the array of all OCGs in the document. if (ocgs != null) { int i, sz = ocgs.Size(); for (i = 0; i < sz; ++i) { Group ocg = new Group(ocgs.GetAt(i)); ctx.ResetStates(false); ctx.SetState(ocg, true); string fname = "pdf_layers_" + ocg.GetName() + ".png"; Console.WriteLine(fname); pdfdraw.Export(page, fname); } } // Now draw content that is not part of any layer... ctx.SetNonOCDrawing(true); ctx.SetOCDrawMode(Context.OCDrawMode.e_NoOC); pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png"); Console.WriteLine("Done."); } } } } catch (PDFNetException e) { Console.WriteLine(e.Message); } }