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

Source: Internet
Author: User

Objective

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
    • Visual Studio
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 #"

Using spire.pdf;using spire.pdf.graphics;using System.drawing;namespace addlayers_pdf{class Program {static            void Main (string[] args) {//New PDF document, add a page pdfdocument PDF = new Pdfdocument (); Pdfpagebase page = PDF.            Pages.Add (); Add a line layer, set to invisible pdfpagelayer layer = page.            Pagelayers.add ("line", true); Adds a line of the specified length to the specified position in the page, and sets the line color, thickness, and other formatting layers. Graphics.drawline (New PDFpen (Pdfbrushes.blue, 1), new PointF (0, 0), new PointF (0, 200)); Here you can add multiple lines//Add a layer named Ellipse to the page, and set the layer as invisible layers = page.            Pagelayers.add ("Ellipse", true);            Set the graphic color, line thickness pdfpen pen = new PDFpen (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 page Pdfbrush brush = new Pdfsolidbrush (color.white); Layer.            Graphics.drawellipse (pen, brush, 240, 200, 60,20); Add a picture layer and name the layer "Image layer" layer = page.            Pagelayers.add ("Image Layer"); Load picture, on pageAdd the picture as a layer and set the size layer of the image as the face specifies the location.            Graphics.DrawImage (Pdfimage.fromfile ("Image.png"), 200, 230, 150, 185); Add a layer of string type, named string layer layer = page.            Pagelayers.add ("String Layer"); Specifies the position of the page as text, and sets the font, size, color, and so on. Graphics.DrawString ("Oh~it ' S so good! Right? \ Let's GET started!\n COME on, guys! ", New Pdffont (Pdffontfamily.timesroman, N), new PDFpen (pdfbrushes.mediumvioletred            , 1), 45, 250); Save and open the document PDF.            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 #"

using Spire.Pdf;using Spire.Pdf.Graphics.Layer;namespace HideOrShowLayer_PDF{    class Program    {        static void Main(string[] args)        {               //实例化PdfDocument类,加载文档                PdfDocument doc = new PdfDocument("AddLayers.pdf");                //设置图层可见属性为Off,通过索引值来隐藏第3个图层                //doc.Layers[2].Visibility = PdfVisibility.Off;                //设置图层可见属性为Off,通过图层名称来隐藏图层                doc.Layers["String Layer"].Visibility = PdfVisibility.Off;                //保存并打开文档                doc.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 #"

using System;using Spire.Pdf;using Spire.Pdf.Graphics.Layer;namespace ShowLayer_PDF{    class Program    {        static void Main(string[] args)        {            //创建PdfDocument类对象,加载pdf测试文档            PdfDocument doc = new PdfDocument("AddLayers.pdf");            //遍历文档中的所有图层,            for (int i = 0; i < doc.Layers.Count; i++)            {                //显示所有图层                //doc.Layers[i].Visibility = PdfVisibility.On;                //隐藏所有图层                doc.Layers[i].Visibility = PdfVisibility.Off;            }                //保存并打开文档                doc.SaveToFile("HideAllLayers.pdf");                System.Diagnostics.Process.Start("HideAllLayers.pdf");        }    }}

Test results:

Third, delete the layer
"C #"

using Spire.Pdf;namespace DeleteLayer_PDF{    class Program    {        static void Main(string[] args)        {            //创建一个PdfDocument类对象,加载包含多个图层的PDF文档            PdfDocument doc = new PdfDocument();            doc.LoadFromFile("AddLayers.pdf");            //调用方法RemoveLayer()删除名为“Line”的图层            doc.Layers.RemoveLayer(("Line"));            //保存并打开文档            doc.SaveToFile("DeleteLayer.pdf", FileFormat.PDF);            System.Diagnostics.Process.Start("DeleteLayer.pdf");        }    }}

Test results:

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.