using Aspose.Words;
using Gma.QrCodeNet.Encoding;
using System.IO;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing;
using System.Drawing.Imaging;
using System;
using Aspose.Words.Drawing;
using iTextSharp.text.pdf;
using iTextSharp.text;
namespace Tools
{
    public static class ToolsClass
    {
        /// <summary>
        /// word转PDF方法
        /// <param name="fileUrl">原文件地址</param>
        /// <param name="CreateUrl">生成的二维码存放路径(结尾需\)</param>
        /// </summary>
        public static string WordChangePdf(string fileUrl, string CreateUrl, string waterWord, string QRdata)
        {
            try
            {
                string physicalPath = ConfigurationManager.AppSettings["RootPhysicalPath"];
                string webPath = Ultimus.UWF.Common.Logic.WebUtil.GetRootPath();
                string QRurl = CreateQRcode(QRdata, physicalPath + CreateUrl);
                string fileName = fileUrl.Split('\\')[fileUrl.Split('\\').Length - 1];
                string pdfName = fileName.Split('.')[0];
                Aspose.Words.Document doc = new Aspose.Words.Document(@physicalPath + @fileUrl);
                DocumentBuilder builder = new DocumentBuilder(doc);
                builder.PageSetup.DifferentFirstPageHeaderFooter = true;
                System.Drawing.Image image = System.Drawing.Image.FromFile(@QRurl);
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
                builder.InsertImage(image, RelativeHorizontalPosition.RightMargin, -65.00, RelativeVerticalPosition.TopMargin, 25.00, 60.00, 60.00, WrapType.Square);
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
                builder.InsertImage(image, RelativeHorizontalPosition.RightMargin, -65.00, RelativeVerticalPosition.TopMargin, 25.00, 60.00, 60.00, WrapType.Square);
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
                builder.InsertImage(image, RelativeHorizontalPosition.RightMargin, -65.00, RelativeVerticalPosition.TopMargin, 25.00, 60.00, 60.00, WrapType.Square);
                string guid = Guid.NewGuid().ToString();
                string filename = @CreateUrl + pdfName + ".pdf";
                string newfilename = @CreateUrl + pdfName + "_1.pdf";
                //InsertWatermarkText(doc, waterWord, width, height);
                doc.Save(@physicalPath + filename, Aspose.Words.SaveFormat.Pdf);

                PdfReader pdfReader = null;
                PdfStamper pdfStamper = null;
                pdfReader = new PdfReader(@physicalPath + filename);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(@physicalPath + newfilename, FileMode.Create));
                int total = pdfReader.NumberOfPages + 1;
                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
                float width = psize.Width;
                float height = psize.Height;
                PdfContentByte content;
                BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                PdfGState gs = new PdfGState();
                for (int i = 1; i < total; i++)
                {
                    content = pdfStamper.GetOverContent(i);//在内容上方加水印
                    gs.FillOpacity = 0.4f;
                    content.SetGState(gs);
                    //content.SetGrayFill(0.3f);
                    //开始写入文本
                    content.BeginText();
                    BaseColor color = new BaseColor(200, 200, 200, 255);
                    content.SetColorFill(color);
                    content.SetFontAndSize(font, 65);
                    content.SetTextMatrix(0, 0);
                    content.ShowTextAligned(Element.ALIGN_CENTER, waterWord, width / 2 , height / 2, 45);
                    content.EndText();
                }
                pdfStamper.Close();

                return @webPath + newfilename;
            }
            catch (Exception e)
            {
                return "生成失败,请联系系统管理员";
            }
        }

        /// <summary>
        /// 生成二维码
        /// <param name="dataUrl">链接地址</param>
        /// <param name="CreateUrl">生成的二维码存放路径(结尾需\)</param>
        /// </summary>
        public static string CreateQRcode(string dataUrl, string CreateUrl)
        {
            try
            {
                string guid = Guid.NewGuid().ToString();
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
                QrCode qrCode = qrEncoder.Encode(@dataUrl);
                //保存成png文件
                string filename = @CreateUrl + guid + ".png";
                GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
                using (FileStream stream = new FileStream(filename, FileMode.Create))
                {
                    render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
                }
                return filename;
            }
            catch (Exception e)
            {
                return "生成失败,请联系系统管理员";
            }
        }
    }
}

 

posted on 2019-06-26 15:23  YYY_GOD  阅读(708)  评论(0)    收藏  举报