using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Vml;
using DocumentFormat.OpenXml.Vml.Office;
using DocumentFormat.OpenXml.Wordprocessing;
using MCS.Library.Principal;
using Seagull2.Components.Material.Services;
using Seagull2.ContractV2.TransactionData;
using Seagull2.Core;
using Seagull2.Owin.Material.Models;
using Seagull2.Owin.Material.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
namespace Seagull2.ContractV2.WebApi.Common
{
/// <summary>
/// 合同范本文档操作类
/// </summary>
public class ContractDocHelper
{
public static void AddWatermarkInDoc(string fileName, string watermarkFileName)
{
byte[] sourceBytes = File.ReadAllBytes(fileName);
MemoryStream inMemoryStream = new MemoryStream();
inMemoryStream.Write(sourceBytes, 0, (int)sourceBytes.Length);
var doc = WordprocessingDocument.Open(inMemoryStream, true);
AddWatermark(doc, watermarkFileName);
doc.MainDocumentPart.Document.Save();
doc.Close();
doc.Dispose();
doc = null;
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
inMemoryStream.WriteTo(fileStream);
}
inMemoryStream.Close();
inMemoryStream.Dispose();
inMemoryStream = null;
}
static void AddWatermark(WordprocessingDocument doc, string watermarkFileName)
{
if (doc.MainDocumentPart.HeaderParts.Count() == 0)
{
doc.MainDocumentPart.DeleteParts(doc.MainDocumentPart.HeaderParts);
var newHeaderPart = doc.MainDocumentPart.AddNewPart<HeaderPart>();
var rId = doc.MainDocumentPart.GetIdOfPart(newHeaderPart);
var headerRef = new HeaderReference();
headerRef.Id = rId;
var sectionProps = doc.MainDocumentPart.Document.Body.Elements<SectionProperties>().LastOrDefault();
if (sectionProps == null)
{
sectionProps = new SectionProperties();
doc.MainDocumentPart.Document.Body.Append(sectionProps);
}
sectionProps.RemoveAllChildren<HeaderReference>();
sectionProps.Append(headerRef);
newHeaderPart.Header = MakeHeader();
newHeaderPart.Header.Save();
}
foreach (HeaderPart headerPart in doc.MainDocumentPart.HeaderParts)
{
var sdtBlock1 = new SdtBlock();
var sdtProperties1 = new SdtProperties();
var sdtId1 = new SdtId() { Val = 87908844 };
var sdtContentDocPartObject1 = new SdtContentDocPartObject();
var docPartGallery1 = new DocPartGallery() { Val = "Watermarks" };
var docPartUnique1 = new DocPartUnique();
sdtContentDocPartObject1.Append(docPartGallery1);
sdtContentDocPartObject1.Append(docPartUnique1);
sdtProperties1.Append(sdtId1);
sdtProperties1.Append(sdtContentDocPartObject1);
ImagePart imgPart = headerPart.AddImagePart(ImagePartType.Jpeg);
using (var fileStream = new FileStream(watermarkFileName, FileMode.Open, FileAccess.Read))
{
imgPart.FeedData(fileStream);
fileStream.Close();
fileStream.Dispose();
}
string imgId = headerPart.GetIdOfPart(imgPart);
var sdtContentBlock1 = new SdtContentBlock();
var paragraph2 = new Paragraph()
{
RsidParagraphAddition = "00656E18",
RsidRunAdditionDefault = "00656E18"
};
var paragraphProperties2 = new ParagraphProperties();
var paragraphStyleId2 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties2.Append(paragraphStyleId2);
var run1 = new Run();
var runProperties1 = new RunProperties();
var noProof1 = new NoProof();
var languages1 = new Languages() { EastAsia = "zh-TW" };
runProperties1.Append(noProof1);
runProperties1.Append(languages1);
var picture1 = new Picture();
var shapetype1 = new Shapetype()
{
Id = "_x0000_t136",
CoordinateSize = "21600,21600",
OptionalNumber = 136,
Adjustment = "10800",
EdgePath = "m@7,l@8,m@5,21600l@6,21600e"
};
var formulas1 = new Formulas();
var formula1 = new Formula() { Equation = "sum #0 0 10800" };
var formula2 = new Formula() { Equation = "prod #0 2 1" };
var formula3 = new Formula() { Equation = "sum 21600 0 @1" };
var formula4 = new Formula() { Equation = "sum 0 0 @2" };
var formula5 = new Formula() { Equation = "sum 21600 0 @3" };
var formula6 = new Formula() { Equation = "if @0 @3 0" };
var formula7 = new Formula() { Equation = "if @0 21600 @1" };
var formula8 = new Formula() { Equation = "if @0 0 @2" };
var formula9 = new Formula() { Equation = "if @0 @4 21600" };
var formula10 = new Formula() { Equation = "mid @5 @6" };
var formula11 = new Formula() { Equation = "mid @8 @5" };
var formula12 = new Formula() { Equation = "mid @7 @8" };
var formula13 = new Formula() { Equation = "mid @6 @7" };
var formula14 = new Formula() { Equation = "sum @6 0 @5" };
formulas1.Append(formula1);
formulas1.Append(formula2);
formulas1.Append(formula3);
formulas1.Append(formula4);
formulas1.Append(formula5);
formulas1.Append(formula6);
formulas1.Append(formula7);
formulas1.Append(formula8);
formulas1.Append(formula9);
formulas1.Append(formula10);
formulas1.Append(formula11);
formulas1.Append(formula12);
formulas1.Append(formula13);
formulas1.Append(formula14);
var path1 = new DocumentFormat.OpenXml.Vml.Path()
{
AllowTextPath = TrueFalseValue.FromBoolean(true),
ConnectionPointType = ConnectValues.Custom,
ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800",
ConnectAngles = "270,180,90,0"
};
//var textPath1 = new TextPath()
//{
// On = TrueFalseValue.FromBoolean(true),
// FitShape = TrueFalseValue.FromBoolean(true)
//};
var shapeHandles1 = new ShapeHandles();
var shapeHandle1 = new ShapeHandle()
{
Position = "#0,bottomRight",
XRange = "6629,14971"
};
shapeHandles1.Append(shapeHandle1);
//var lock1 = new DocumentFormat.OpenXml.Vml.Office.Lock
//{
// Extension = ExtensionHandlingBehaviorValues.Edit,
// TextLock = TrueFalseValue.FromBoolean(true),
// ShapeType = TrueFalseValue.FromBoolean(true)
//};
shapetype1.Append(formulas1);
shapetype1.Append(path1);
//shapetype1.Append(textPath1);
shapetype1.Append(shapeHandles1);
//shapetype1.Append(lock1);
var shape1 = new Shape()
{
Id = "WordPictureWatermark144925235",
Style = "position:absolute;left:-60;text-align:left;margin-left:0;margin-top:0;width:140pt;height:50pt;rotation:270;z-index:-251657216;mso-position-horizontal:left;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin",
OptionalString = "_x0000_s2050",
AllowInCell = TrueFalseValue.FromBoolean(false),
Type = "#_x0000_t75"
};
//var fill1 = new Fill() { Opacity = ".3" };
//TextPath textPath2 = new TextPath()
//{
// Style = "font-family:\"Calibri\";font-size:1pt",
// String = textWatermark
//};
var imgdata = new ImageData()
{
Title = "logo",
Gain = "19661f",
BlackLevel = "22938f",
RelId = imgId,
RelationshipId = imgId
};
//var textWrap1 = new TextWrap()
//{
// AnchorX = DocumentFormat.OpenXml.Vml.Wordprocessing.HorizontalAnchorValues.Margin,
// AnchorY = DocumentFormat.OpenXml.Vml.Wordprocessing.VerticalAnchorValues.Margin
//};
//shape1.Append(fill1);
shape1.Append(imgdata);
//shape1.Append(textWrap1);
picture1.Append(shapetype1);
picture1.Append(shape1);
run1.Append(runProperties1);
run1.Append(picture1);
paragraph2.Append(paragraphProperties2);
paragraph2.Append(run1);
sdtContentBlock1.Append(paragraph2);
sdtBlock1.Append(sdtProperties1);
sdtBlock1.Append(sdtContentBlock1);
headerPart.Header.Append(sdtBlock1);
headerPart.Header.Save();
//break;
}
}
static Header MakeHeader()
{
var header = new Header();
var paragraph = new Paragraph();
var run = new Run();
var text = new Text();
text.Text = "";
run.Append(text);
paragraph.Append(run);
header.Append(paragraph);
return header;
}
/// <summary>
/// 生成水印图片
/// </summary>
private string generateWaterMarkImage(string headerText)
{
string fileName = System.IO.Path.Combine(ConfigHelper.FileTempAddress, string.Format("{0}_{1}.png", Guid.NewGuid().ToString(), headerText));
string logoFileName = System.IO.Path.Combine(ConfigHelper.FileTempAddress, "logo.png");
using (FileStream fs = new FileStream(logoFileName, FileMode.Open, FileAccess.Read,
FileShare.Read))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
MemoryStream ms = new MemoryStream(bytes);
Bitmap img = new Bitmap(ms);
Graphics g = Graphics.FromImage(img);
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 16);
SolidBrush sbrush = new SolidBrush(System.Drawing.Color.Black);
g.DrawString(headerText, font, sbrush, new PointF(15, 3));
img.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
return fileName;
}
}
}