using System;
using Aspose.Words;
using Neu.Uasd.HrPrinter.Utility.Log4;
using Aspose.Words.Drawing;
using System.IO;
using Aspose.Words.Rendering;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using Neu.Uasd.HrPrinter.Utility.entity.certModel;
using System.Drawing.Printing;
namespace Neu.Uasd.HrPrinter.Utility.Tools
{
public class AsposeDocHelper
{
public static List<String> GetDocBookMarks(string fileDoc)
{
List<String> bookMarks = new List<string>();
try
{
if (!System.IO.File.Exists(fileDoc))
{
Info("文件未找到:" + fileDoc);
}
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
BookmarkCollection bookmarks = doc.Range.Bookmarks;
if (bookmarks != null)
{
foreach (Bookmark item in bookmarks)
{
Info(item.ToString());
bookMarks.Add(item.Name);
}
}
}
catch (Exception ex)
{
Utility.Log4. Error(ex);
}
return bookMarks;
}
/// <summary>
/// 将Word转换为Png
/// </summary>
/// <param name="filepath">文件地址</param>
/// <param name="pageIndex">要转换的页</param>
/// <returns></returns>
public static void Word2Png(string filepath, int pageIndex)
{
// Stream memStream = new Stream();
Aspose.Words.Document doc = new Aspose.Words.Document(filepath);
string tempPng = System.IO.Path.Combine(Environment.CurrentDirectory, "img/certModel/收入证明/temp.png");
PageInfo pageInfo = doc.GetPageInfo(pageIndex);
float scale = 100 / 100.0f;
const int Resolution = 96;
Size imgSize = pageInfo.GetSizeInPixels(scale, Resolution);
using (Bitmap img = new Bitmap(imgSize.Width, imgSize.Height))
{
img.SetResolution(Resolution, Resolution);
using (Graphics gfx = Graphics.FromImage(img))
{
gfx.Clear(Color.White);
doc.RenderToScale(pageIndex, gfx, 0, 0, scale);
img.Save(tempPng, ImageFormat.Png);
}
}
// Send the bitmap data to the output stream.
// Response.ContentType = "image/png";
//byte[] imageData = memStream.ToArray();
// Response.OutputStream.Write(imageData, 0, imageData.Length);
}
/// <summary>
/// 更新文档中全部的 书签
/// </summary>
/// <param name="fileDoc"></param>
/// <param name="modifyBookItems"></param>
/// <param name="outPutDoc"></param>
public static void UpdateBookMark(string fileDoc,List<BookMarkItem> modifyBookItems,string outPutDoc)
{
try
{
if (!System.IO.File.Exists(fileDoc))
{
Info("收入证明文件未找到:" + fileDoc);
}
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
foreach (var item in modifyBookItems)
{
if (string.IsNullOrEmpty (item.Key ) || string.IsNullOrEmpty (item.Value))
{
Error("标签键值为空 :" + item.Key +item.Value);
continue;
}
Bookmark bookmark = doc.Range.Bookmarks[item.Key];
if (bookmark != null)
{
// bookmark.Text = item.Value;
builder.MoveToBookmark(item.Key);
string showValue = item.Value;
if (item.FontStyle == UI.Common.Entity.enums.BookMarkFontStyle.StrikeThrough)
{
builder.Font.StrikeThrough = true;
}
else if (item.FontStyle == UI.Common.Entity.enums.BookMarkFontStyle.CheckBox)
{
showValue = item.ShowValue;
}
//builder.Font.Italic = true;
//builder.Underline = Underline.Double;
//builder.Font.Outline = true;
//builder.StartBookmark(item.Key);
//BookmarkEnd end= builder.EndBookmark(item.Key);
builder.Write(showValue);
}
else
{
Info("标签未找到:" + item.Key);
}
}
Neu.Uasd.HrPrinter.Utility.Log4. Info("签名文档: " + fileDoc);
string _md5String = SecurityHelper.MakeSignMd5(modifyBookItems);
Neu.Uasd.HrPrinter.Utility.Log4. Info("签名结果: " + _md5String);
//AddFooter(builder, _md5String);
doc.Save(outPutDoc);
}
catch (Exception ex)
{
Utility.Log4. Error(ex);
}
}
/// <summary>
/// 插入印章 书签
/// </summary>
/// <param name="fileDoc"></param>
/// <param name="sealMark"></param>
/// <param name="outPutDoc"></param>
public static void UpdateSealImage(string fileDoc, BookMarkItem sealMark , string outPutDoc)
{
Document doc = new Document(fileDoc);//TempValue doc模板的路径
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = new Shape(doc, ShapeType.Image);
//string sealFileName = System.IO.Path.Combine(Environment.CurrentDirectory, "img/seal/人事章-集团.png"); //目录地址
if (!System.IO.File.Exists(sealMark.Value))
{
Info("印章文件未找到:" + sealMark.Value);
}
shape.ImageData.SetImage(sealMark.Value);
shape.HorizontalAlignment = HorizontalAlignment.Right; //靠右对齐
//shape.Left = 20;
//shape.Top = 160;
shape.Width = 70;//设置宽和高
shape.Height = 70;
shape.WrapType = WrapType.None;
shape.BehindText = true;
if (doc.Range.Bookmarks[sealMark.Key] != null)
{
builder.MoveToBookmark(sealMark.Key);
builder.InsertNode(shape);
}
//builder.MoveToBookmark(sealMark.Key);
// builder.InsertNode(shape);
//builder.InsertImage(
// sealMark.Value,
// RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 70, 70, WrapType.Square);
doc.Save(outPutDoc);
}
/// <summary>
/// 插入印章 书签
/// </summary>
/// <param name="fileDoc"></param>
/// <param name="sealMark"></param>
/// <param name="outPutDoc"></param>
public static void UpdateSealImage(string fileDoc, SealItem sealItem, string outPutDoc)
{
Document doc = new Document(fileDoc);
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = new Shape(doc, ShapeType.Image);
if (!System.IO.File.Exists(sealItem.SealFileName))
{
Info("印章文件未找到:" + sealItem.SealFileName);
}
// floating image
builder.InsertImage(sealItem.SealFileName,
RelativeHorizontalPosition.Margin,
sealItem.Left,
RelativeVerticalPosition.Margin,
sealItem.Top,
sealItem.Width,
sealItem.Height,
WrapType.None);
doc.Save(outPutDoc);
}
public static void Word2Img(string fileDoc, string outPutFile)
{
Word2Img( fileDoc, outPutFile,0);
}
public static void Word2Img(string fileDoc, string outPutFile,int pageIndex)
{
Stream memStream = new Stream();
//Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
float MyScale = 0.50f;
float MyResolution = 150.0f;
//PageInfo pageInfo = doc.GetPageInfo(pageIndex);
//float scale = 1.0f;
//const int Resolution = 100;
//Size imgSize = pageInfo.GetSizeInPixels(scale, Resolution);
//using (Bitmap img = new Bitmap(imgSize.Width, imgSize.Height))
//{
// img.SetResolution(Resolution, Resolution);
// using (Graphics gfx = Graphics.FromImage(img))
// {
// gfx.Clear(Color.White);
// doc.RenderToScale(pageIndex, gfx, 0, 0, scale);
// img.Save(outPutFile, ImageFormat.Png);
// }
//}
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
if (File.Exists(outPutFile))
{
try{
File.Delete(outPutFile);
}
catch(Exception ex )
{
Error(ex);
}
}
doc.Save(outPutFile,SaveFormat.Tiff);
}
public static void Word2Pdf(string fileDoc, string outPutFile )
{
// Stream memStream = new Stream();
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
doc.Save(outPutFile, SaveFormat.Pdf);
}
public static void Word2Xps(string fileDoc, string outPutFile)
{
// Stream memStream = new Stream();
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
doc.Save(outPutFile, SaveFormat.Xps);
}
public static void PrintDoc(string fileDoc)
{
if (!System.IO.File.Exists(fileDoc))
{
Info("打印文件未找到:" + fileDoc);
}
else
{
try
{
Document doc = new Document(fileDoc);
var pd = new AsposeWordsPrintDocument(doc);
pd.PrintController = new StandardPrintController();
pd.Print();
}catch(Exception ex)
{
Error("打印文件异常:" + fileDoc);
Error(ex);
}
}
}
public static void AddFooter(string fileDoc,string value)
{
Aspose.Words.Document doc = new Aspose.Words.Document(fileDoc);
DocumentBuilder builder = new DocumentBuilder(doc);
AddFooter(builder, value);
doc.Save(fileDoc);
}
public static void AddFooter(DocumentBuilder builder, string footer)
{
//设置移动到页面最底下
// builder.MoveToDocumentEnd();
//设置奇数页和偶数页页眉页脚
// builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
//设置除第一页外的页眉页脚
// builder.PageSetup.DifferentFirstPageHeaderFooter=true;
//设置移动到页眉和页脚
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
//设置字体
// builder.Font.Name ="Arial";
// builder.Font.Bold = true;
builder.Font.Size = 12;
builder.Write(footer);
}
}
}