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