C # Add watermarks to Word documents and PDFs

Source: Internet
Author: User

Requirements: In Word, PDF document to add a watermark at the specified location, because the location to be flexible so you can not directly use the operation of Word and PDF to complete, you need to convert it to a picture, and then the watermark image and the converted image composition so that you can arbitrarily specify the location of the watermark. Finally, the synthesized picture is inserted into the new PDF, because Word has a header and footer so it cannot be inserted into word at last.

The test interface is as follows:

Select the location of the document where you want to add the watermark, select the output path, select the type of document you want to convert, click "Generate", and then prompt "Build success!" ”。

Key code:

public static void Addbarcodetopdf (String inputpath, String OutputPath, String Barcodepath, DocType DocType,
int Locationx, int locationy, int width, int height)
{
String docimgpath= application.startuppath+ "\\DocImg\\";//conversion to the path of the document picture storage
if (! Directory.Exists (Docimgpath))
{
Directory.CreateDirectory (Docimgpath);
}
Idoctoimage doctoimage;//to instantiate the corresponding object by selecting the document type
Switch (docType)
{
Case Doctype.word:
Doctoimage = wordtoimage.instance;
Break
Case Doctype.pdf:
Doctoimage = pdftoimage.instance;
Break
Default
Doctoimage = wordtoimage.instance;
Break

}
Doctoimage.convertdoctoimage (InputPath, Docimgpath);//The method of converting a document to a picture, which is not detailed in the source code
String Docbarcodeimgpath = Application.startuppath + "\\DocBarcodeImg\\";//image storage path after image and watermark image synthesis
if (! Directory.Exists (Docbarcodeimgpath))
{
Directory.CreateDirectory (Docbarcodeimgpath);
}
string[] imagepaths = Directory.GetFiles (Docimgpath);
Combineimage (Imagepaths, Barcodepath, Docbarcodeimgpath,locationx, locationy, width, height);//image and Watermark image synthesis method
Imagepaths=directory.getfiles (Docbarcodeimgpath);
Insertpictruetopdf (Imagepaths, OutputPath);//insert a picture into a new PDF

}

Specific code address: Http://pan.baidu.com/s/1qW7H9LU

C # Add watermarks to Word documents and PDFs

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.