C # operations PDF Layer (layer)--Add, delete, set layer visibility

Source: Internet
Author: User
Tags pdfpen

Preface

By adding layers, we can precisely position elements such as text, pictures, tables, graphics, and so on in a page-specific position, stacking the elements together and forming the final effect of the page. In addition, we can set layer visibility, delete layers, and more for layers already on the page. So, in this article, we'll show you how to do the PDF layer in C #.

Summary of Points

1. Adding layers

    • Lines
    • Graphics
    • Image
    • String

2. Set the layer to hide, display

    • Specify individual layer Visibility
    • Set the visibility of all layers

3. Delete a layer

Tool Use
    • Spire.pdf for. NET 4.0
Code Manipulation First, Add a layer

We add layers by using spire.pdf, which supports adding multiple types of layers, such as lines, images, strings, pie charts, ellipses, rectangles, and polygons.

"C #"

usingspire.pdf;usingSpire.Pdf.Graphics;usingSystem.Drawing;namespaceaddlayers_pdf{classProgram {Static voidMain (string[] args) {            //Create a new PDF document and add a pagePdfdocument PDF =Newpdfdocument (); Pdfpagebase page=PDF.            Pages.Add (); //Add a line layer, set to invisiblePdfpagelayer layer = page. Pagelayers.add (" Line",true); //adds a line of the specified length to the specified position in the page and formats the line color, thickness , and so onLayer. Graphics.drawline (NewPDFpen (Pdfbrushes.blue,1),NewPointF (0,0),NewPointF (0, $));//you can add more than one line as needed here .//add a layer named Ellipse to the page to set the layer as invisibleLayer = page. Pagelayers.add ("Ellipse",true); //set graphic color, line thicknessPDFpen pen =NewPDFpen (Color.yellow,2.5f); //fills the inner color of the graphic and adds an oval shape of the specified size at the specified position in the pagePdfbrush brush =NewPdfsolidbrush (Color.White); Layer. Graphics.drawellipse (pen, brush, -, $, -, -); //Add a picture layer and name the layer "image layer"Layer = page. Pagelayers.add ("Image Layer"); //load a picture, add the picture as a layer and set the size of the image at the specified location in the pageLayer. Graphics.DrawImage (Pdfimage.fromfile ("Image.png"), $, the, Max,185); //add a layer of type string and name it "string layer"Layer = page. Pagelayers.add ("String Layer"); //assign a location to a page to paint as text, and format fonts, sizes, colors, and so onLayer. Graphics.DrawString ("oh~it ' S so good! Right? \ Let's GET started!\n COME on, guys!",NewPdffont (Pdffontfamily.timesroman, A),NewPDFpen (pdfbrushes.mediumvioletred,1), $, -); //Save and open a documentPdf. SaveToFile ("addlayers.pdf", fileformat.pdf); System.Diagnostics.Process.Start ("addlayers.pdf"); }    }}

Add Effect:

second, set the layer to hide, display (i) Set the hidden, display of a single layer

"C #"

usingspire.pdf;usingSpire.Pdf.Graphics.Layer;namespacehideorshowlayer_pdf{classProgram {Static voidMain (string[] args) {               //instantiating a pdfdocument class, loading a documentPdfdocument doc =NewPdfdocument ("addlayers.pdf"); //set layer Visible property to off, hide 3rd layer by index value//Doc. LAYERS[2].                Visibility = Pdfvisibility.off; //set layer Visible property to off, hide layer by layer nameDoc. layers["String Layer"]. Visibility =Pdfvisibility.off; //Save and open a documentDoc. SaveToFile ("hidelayer0.pdf"); System.Diagnostics.Process.Start ("hidelayer0.pdf"); }    }}

Test results:

1. Hide the effect of the layer with the index value

2. Hide the effect of the layer with the layer name

(ii) Set the hidden, display of all layers

"C #"

usingSystem;usingspire.pdf;usingSpire.Pdf.Graphics.Layer;namespaceshowlayer_pdf{classProgram {Static voidMain (string[] args) {            //Create a Pdfdocument class object and load a PDF test documentPdfdocument doc =NewPdfdocument ("addlayers.pdf"); //traverse all the layers in the document,             for(inti =0; I < Doc. Layers.count; i++)            {                //Show All Layers//Doc. Layers[i].                Visibility = Pdfvisibility.on; //Hide All LayersDoc. Layers[i]. Visibility =Pdfvisibility.off; }                //Save and open a documentDoc. SaveToFile ("hidealllayers.pdf"); System.Diagnostics.Process.Start ("hidealllayers.pdf"); }    }}

Test results:

Third, delete the layer

"C #"

usingspire.pdf;namespacedeletelayer_pdf{classProgram {Static voidMain (string[] args) {            //Create a Pdfdocument class object to load a PDF document containing multiple layersPdfdocument doc =Newpdfdocument (); Doc. LoadFromFile ("addlayers.pdf"); //Call Method Removelayer () to remove a layer named "line"Doc. Layers.removelayer ((" Line")); //Save and open a documentDoc. SaveToFile ("deletelayer.pdf", fileformat.pdf); System.Diagnostics.Process.Start ("deletelayer.pdf"); }    }}

Test results:

Read the end!

If you want to reprint, please specify the source!

C # operations PDF Layer (layer)--Add, delete, set layer visibility

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.