How to add a picture stamp in a PDF file using C # detailed introduction

Source: Internet
Author: User
Adding stamps to a document can play a role, such as preventing files from being used at will, or ensuring the security and authority of the contents of the document. C # Add a picture stamp there are actually many ways to do it, and here I'm using free third-party software, Spire.pdf, to show you how to programmatically add a picture stamp to a PDF file





Adding stamps to a document can play a role, such as preventing files from being used at will, or ensuring the security and authority of the contents of the document. C # Add a picture stamp there are actually many ways to do it, and here I'm using free third-party software, Spire.pdf, to show you how to programmatically add a picture stamp to a PDF file.



The steps are as follows:



Until then, we need to add the DLL file as a reference. Add reference → Browse →spire.xls folder→bin→.net 2.0/3.5/4.0/4.5/4.0 clientprofile→spire.xls.dll.



First step: Create a new PDF Document object and load the document you want to add the stamp to.

PdfDocument doc = new PdfDocument ();
doc.LoadFromFile (@ "E: \ Visual Studio \ Sample \ template7 \ sample.pdf");
Step 2: Get the first page of the document.

PdfPageBase page = doc.Pages [0];
Step 3: Create a new PdfRubberStampAnnotation object, and specify the scope and size of its annotation.

PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation (new RectangleF (new PointF (-5, -5), new SizeF (60, 60)));
Step 4: Instantiate a PdfAppearance object.

PdfAppearance loApprearance = new PdfAppearance (loStamp);
Step 5: Load the picture used as a seal.

PdfImage image = PdfImage.FromFile (@ "C: \ Users \ Administrator \ Pictures \ sample.jpg");
Step 6: Create a new PDF template and draw pictures in the template.

PdfTemplate template = new PdfTemplate (160, 160);
template.Graphics.DrawImage (image, 0, 0);
loApprearance.Normal = template;
loStamp.Appearance = loApprearance;
Step 7: Add a seal to the PDF document.

page.AnnotationsWidget.Add (loStamp);
Step 8: Save the document.

string output = "ImageStamp.pdf";
doc.SaveToFile (output);
PDF document before running:

The pdf document after running:

All codes:

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
using Spire.Pdf.Graphics;

namespace addanimagestamptoaPDF_file
{
  public partial class Form1: Form
  {
    public Form1 ()
    {
      InitializeComponent ();
    }

    private void button1_Click (object sender, EventArgs e)
    {
      PdfDocument doc = new PdfDocument ();
      doc.LoadFromFile (@ "E: \ Visual Studio \ Sample \ template7 \ sample.pdf");
 
      PdfPageBase page = doc.Pages [0];
      
      PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation (new RectangleF (new PointF (-5, -5), new SizeF (60, 60)));
      PdfAppearance loApprearance = new PdfAppearance (loStamp);
      PdfImage image = PdfImage.FromFile (@ "C: \ Users \ Administrator \ Pictures \ sample.jpg");
 
      PdfTemplate template = new PdfTemplate (160, 160);
      template.Graphics.DrawImage (image, 0,0);
      loApprearance.Normal = template;
      loStamp.Appearance = loApprearance;
        
      page.AnnotationsWidget.Add (loStamp);

      string output = "ImageStamp.pdf";
      doc.SaveToFile (output);
  
    }
  }
}
Through this component, in addition to quickly adding a picture seal to a PDF file, we can also add a picture and text watermark and add a picture background to the PDF file. You can refer to it, maybe it will help you. Thanks for browsing.

Related Article

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.