Insert dense watermark and QR code in Word and Excel, and wordexcel watermark

Source: Internet
Author: User

Insert dense watermark and QR code in Word and Excel, and wordexcel watermark

The customer recently asked to develop a feature:

After a file is uploaded to the system, an intensive watermark and two-dimensional code are automatically added to each page of the document. After printing the paper document, the system scans the two-dimensional code to automatically open the system's electronic file to prevent unauthorized tampering.

 

A simple DEMO program is developed:

Program

 

Finally:

 

 

 

Used to generate a QR code: ThoughtWorks. QRCode

The operation document uses Aspose.

 

General logic: first, generate a QR code image. Open the document and append a QR code image to the document header. At the same time, append a watermark to each page of the document. (In this region, the customer needs to insert multiple intensive watermarks, I have read the standard Microsoft Word operation and can only insert one at a time. I have used a scoop method for this implementation. I have called the watermark Insertion Method multiple times to see the code below)

 

/*********************************** Start ** *************************Insert watermark, QR code to Word logic******* ****************************/

Generate a QR code snippet:

// Generate a QR code
Protected string CreateQRCODE (string Codestring, string m_savefilepath, int QRCodeScale, int QRCodeVersion)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder ();
QrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE;
QrCodeEncoder. QRCodeScale = QRCodeScale;
QrCodeEncoder. QRCodeVersion = QRCodeVersion;
QrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M;
System. Drawing. Image image = qrCodeEncoder. Encode (Codestring, Encoding. UTF8 );
String filename = DateTime. Now. ToString ("yyyymmddhhmmssfff"). ToString () + ". jpg ";
String filepath = m_savefilepath + filename;
System. IO. FileStream fs = new System. IO. FileStream (filepath, System. IO. FileMode. OpenOrCreate, System. IO. FileAccess. Write );
Image. Save (fs, System. Drawing. Imaging. ImageFormat. Jpeg );

Fs. Close ();
Image. Dispose ();
If (QRCodeScale> = 3)
{
// QR code decoding
Var codeDecoder = CodeDecoder (filepath );
}
Return filepath;
}

Append the QR code to the document code segment:

/// <Summary>
/// Add a header and footer to word and insert the image to the header and footer
/// </Summary>
/// <Param name = "doc"> Target word path </param>
/// <Param name = "FilePath"> path of the saved document </param>
/// <Param name = "imgPath"> image path </param>
Protected string SetImageHeadOrFootDoc (Aspose. Words. Document doc, string FilePath, string imgPath, string Codestring)
{
DocumentBuilder builder = new DocumentBuilder (doc );

Section currentSection = builder. CurrentSection;

Aspose. Words. PageSetup pageSetup = currentSection. PageSetup;


PageSetup. HeaderDistance = 20;

Builder. MoveToHeaderFooter (HeaderFooterType. HeaderFirst );

// Create the title of another page
PageSetup. HeaderDistance = 20;

Builder. MoveToHeaderFooter (HeaderFooterType. HeaderPrimary );

// Insert the absolute position image into the header/upper left corner.
// The distance from the top or left edge page is set to 10 points.
Builder. InsertImage (imgPath, RelativeHorizontalPosition. Page, 280, RelativeVerticalPosition. Page, 10, 50, 50, WrapType. Through );


// Create a page other than the page.
Builder. MoveToHeaderFooter (HeaderFooterType. FooterPrimary );

Doc. Save (FilePath );
Return FilePath;
}

 

Append the watermark to the document:

/// <Summary>
/// Insert the watermark to the Word Document
/// </Summary>
/// <Param name = "doc"> Word document path </param>
/// <Param name = "watermarkText"> watermark text </param>
/// <Param name = "RH"> relative horizontal position of the watermark </param>
/// <Param name = "RV"> relative vertical position of the watermark </param>
/// <Param name = "VA"> watermark horizontal position </param>
/// <Param name = "HA"> vertical watermark position </param>
Private static void Merge (Aspose. Words. Document doc, string watermarkText, RelativeHorizontalPosition RH, RelativeVerticalPosition RV, Aspose. Words. Drawing. verticalignment VA, Aspose. Words. Drawing. Exclude HA)
{
// Instantiate a watermark image
Aspose. Words. Drawing. Shape watermark = new Aspose. Words. Drawing. Shape (doc, ShapeType. TextPlainText );

// Set the watermark text style
Watermark. TextPath. Text = watermarkText;
Watermark. TextPath. FontFamily = "Arial ";
Watermark. Width = 200;
Watermark. Height = 100;
// Watermark direction settings
Watermark. Rotation =-20;
// Watermark line color Filling
Watermark. Fill. Color = Color. Gray;
Watermark. StrokeColor = Color. Gray;

// Set the watermark image to be placed in the center of each page
Watermark. RelativeHorizontalPosition = RH;
Watermark. RelativeVerticalPosition = RV;
Watermark. WrapType = WrapType. None;
Watermark. VerticalAlignment = VA;
Watermark. HorizontalAlignment = HA;

// Initialize a paragraph and append the watermark to the paragraph
Paragraph watermarkPara = new Paragraph (doc );
WatermarkPara. AppendChild (watermark );

// Loop every page and insert the watermark into every page of the document
Foreach (Section sect in doc. Sections)
{
// Insert the watermark to each page of the document
InsertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType. HeaderPrimary );
InsertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType. HeaderFirst );
InsertWatermarkIntoHeader (watermarkPara, sect, HeaderFooterType. HeaderEven );
}

}

/// <Summary>
/// Insert the watermark to the Document Header
/// </Summary>
/// <Param name = "watermarkPara"> watermark </param>
/// <Param name = "sect"> document area </param>
/// <Param name = "headerType"> Document Header type </param>
Private static void InsertWatermarkIntoHeader (Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
Aspose. Words. HeaderFooter header = sect. HeadersFooters [headerType];

If (header = null)
{
// If there is no document header or the header cannot be found, a new document header is automatically created.
Header = new Aspose. Words. HeaderFooter (sect. Document, headerType );
Sect. HeadersFooters. Add (header );
}

// Copy the watermark and append it to the Document Header
Header. AppendChild (watermarkPara. Clone (true ));
}

Intensive watermark, insert multiple watermarks

// Insert a QR code watermark to the word(The method is called repeatedly, and the insertion location is different each time)
Aspose. Words. Document doc = new Aspose. Words. Document (DocPath );
Dependencies (doc, textFontContent. Text, RelativeHorizontalPosition. Page, RelativeVerticalPosition. Page, Aspose. Words. Drawing. verticalignment. Center, Aspose. Words. Drawing. Rows. Left );
Dependencies (doc, textFontContent. Text, RelativeHorizontalPosition. Page, RelativeVerticalPosition. Page, Aspose. Words. Drawing. verticalignment. Bottom, Aspose. Words. Drawing. Orientation. Right );
Dependencies (doc, textFontContent. Text, RelativeHorizontalPosition. Page, RelativeVerticalPosition. Page, Aspose. Words. Drawing. verticalignment. Bottom, Aspose. Words. Drawing. Lost. Left );
Vertex (doc, textFontContent. Text, RelativeHorizontalPosition. Page, RelativeVerticalPosition. Page, Aspose. Words. Drawing. verticalignment. Center, Aspose. Words. Drawing. vertex. Right );

/*************************************** * END ***********************Insert watermark, QR code to Word logic* END ************ ****************************/

 

 

/*************************************** * Start ***********************Insert watermark, QR code to Excel Logic************ ****************************/
/// <Summary>
/// Insert the watermark and add the QR code to the excel document.
/// </Summary>
/// <Param name = "FileName"> path of the target document </param>
/// <Param name = "watermarkText"> watermark text </param>
/// <Param name = "FilePath"> path of the saved document </param>
Protected string InsertWatermarkIntoAndQRCodeToWorkBook (string FileName, string watermarkText, string FilePath, string imgPath)
{
// Initialize the workbook
Workbook workbook = new Workbook (FileName );
// Obtain the workbook table set
WorksheetCollection Wsheetc = workbook. Worksheets;

Foreach (Worksheet sheet in Wsheetc ){
// Insert a watermark
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 1, 1, 1, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 1, 1, 10, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 1, 1, 18, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 14, 1, 1, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 14, 1, 10, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 14, 1, 18, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 28, 1, 1, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 28, 1, 10, 1,100,100 );
InsertWaterMarkToXLS (workbook, sheet, watermarkText, 28, 1, 18, 1,100,100 );
// Insert a QR code
InsertQrCodeToXls (workbook, imgPath, sheet );
}

// Save the excel document
Workbook. Save (FilePath );
Return FilePath;
}
/// <Summary>
/// Insert the QR code to the XLS header and footer
/// </Summary>
/// <Param name = "workbook"> Target XLS document </param>
/// <Param name = "imgPath"> QR code image path </param>
/// <Param name = "sheet"> worksheet </param>
Protected void InsertQrCodeToXls (Workbook workbook, string imgPath, Worksheet sheet)
{

// Add a header and add an image to the header
FileStream inFile;
Byte [] binaryData;
InFile = new System. IO. FileStream (imgPath, System. IO. FileMode. Open, System. IO. FileAccess. Read );

BinaryData = new Byte [inFile. Length];

Long bytesRead = inFile. Read (binaryData, 0, (int) inFile. Length );

Aspose. Cells. PageSetup pageSetup = sheet. PageSetup;

PageSetup. SetHeaderPicture (1, binaryData );

PageSetup. SetHeader (1, "& G ");

PageSetup. SetHeader (2, "& ");

}
/// <Summary>
/// Insert the watermark to XlS
/// </Summary>
/// <Param name = "workbook"> Target Excel file </param>
/// <Param name = "sheet"> Target workbook </param>
/// <Param name = "watermarkText"> watermark text </param>
/// <Param name = "upperLeftRow"> </param>
/// <Param name = "top"> distance from top </param>
/// <Param name = "upperLeftColumn"> </param>
/// <Param name = "left"> distance from left </param>
/// <Param name = "height"> watermark height </param>
/// <Param name = "width"> watermark width </param>
Protected void InsertWaterMarkToXLS (Workbook workbook, Worksheet sheet, string watermarkText, int upperLeftRow, int top, int upperLeftColumn, int left, height, int width)
{
// Add a watermark
Aspose. cells. drawing. shape wordart = sheet. shapes. addTextEffect (MsoPresetTextEffect. textEffect2, watermarkText, "Arial Black", 50, false, true, upperLeftRow, top, upperLeftColumn, left, height, width );

// Fill it with an artistic font
MsoFillFormat wordArtFormat = wordart. FillFormat;
// Set the color
WordArtFormat. ForeColor = System. Drawing. Color. Gray;
// Set transparency
WordArtFormat. Transparency = 0.5;
// Set invisible lines
MsoLineFormat lineFormat = wordart. LineFormat;
LineFormat. IsVisible = false;
}
/*************************************** * END ***********************Insert watermark, QR code to Excel Logic* END ************ ****************************/

 

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.