示例#1
0
        /// <summary>
        /// 浮水印跟頁面一樣大
        /// </summary>
        /// <param name="pdfPage"></param>
        /// <param name="arg"></param>
        private static void AddWatermarkFitPage(Aspose.Pdf.Page pdfPage, WatermarkArg arg)
        {
            //var text = new FormattedText(arg.Watermark);
            var defaultTextState = new TextState("MingLiU");
            var stamp            = new TextStamp(arg.Watermark, defaultTextState);

            stamp.RotateAngle = arg.RotateAngle;
            stamp.XIndent     = arg.WatermarkHorizontalSpace;
            stamp.YIndent     = arg.WatermarkVerticalSpace;
            stamp.Opacity     = arg.Opacity;
            stamp.Width       = pdfPage.CropBox.Width;
            stamp.Height      = pdfPage.CropBox.Height;
            pdfPage.AddStamp(stamp);
        }
示例#2
0
        private void AddWatermarkToPage(double pWidth, double pHeight, Aspose.Pdf.Page pg, string sUserName, string sTextDate)
        {
            //try{
            //    DeleteExistingWatermarks(pg);
            //}catch {


            //}

            double alfaR = 30;

            double c    = pWidth / pHeight;
            double alfa = Math.Atan(c);

            alfaR = (alfa * 180) / Math.PI;

            FormattedText wtext = new FormattedText("Confidential", new FontColor(220, 20, 60), new FontColor(255, 255, 255),
                                                    Aspose.Pdf.Facades.FontStyle.HelveticaBold,
                                                    EncodingType.Winansi, true, 35);

            wtext.AddNewLineText("Downloaded by");
            wtext.AddNewLineText(sUserName.Length > 55 ? sUserName.Substring(0, 55) : sUserName);
            wtext.AddNewLineText(sTextDate.Length > 19 ? sTextDate.Substring(0, 19) : sTextDate);

            TextStamp wStamp = new TextStamp(wtext);

            wStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
            wStamp.TextState.FontStyle       = FontStyles.Bold;
            wStamp.TextState.FontSize        = 35;

            wStamp.HorizontalAlignment = HorizontalAlignment.Center;
            wStamp.VerticalAlignment   = VerticalAlignment.Center;
            wStamp.TextAlignment       = HorizontalAlignment.Center;
            wStamp.Opacity             = .15F;
            wStamp.RotateAngle         = (float)(alfaR - 90);

            pg.AddStamp(wStamp);
        }
示例#3
0
        /// <summary>
        /// 依 浮水印 水平地蓋滿整個頁面
        /// </summary>
        /// <param name="pdfPage"></param>
        /// <param name="arg"></param>
        private static void AddWatermarkRepeatHorizontal(Aspose.Pdf.Page pdfPage, WatermarkArg arg)
        {
            if (arg.WatermarkHeight < minValue)
            {
                throw new ArgumentException($"{nameof(arg.WatermarkHeight)} must greater than {minValue}");
            }
            if (arg.WatermarkWidth < minValue)
            {
                throw new ArgumentException($"{nameof(arg.WatermarkWidth)} must greater than {minValue}");
            }

            //var text = new FormattedText(arg.Watermark);
            var yIndent   = pdfPage.CropBox.Height - arg.WatermarkHeight;
            var yLimit    = 0 - (arg.WatermarkHeight + arg.WatermarkVerticalSpace);
            var pageWidth = pdfPage.CropBox.Width;
            var xIndent   = 0d;

            while (yIndent > yLimit)
            {
                while (xIndent < pageWidth)
                {
                    var defaultTextState = new TextState("MingLiU");
                    var stamp            = new TextStamp(arg.Watermark, defaultTextState);
                    stamp.RotateAngle = arg.RotateAngle;
                    stamp.XIndent     = xIndent;
                    stamp.YIndent     = yIndent;
                    stamp.Opacity     = arg.Opacity;
                    stamp.Width       = arg.WatermarkWidth;
                    stamp.Height      = arg.WatermarkHeight;
                    pdfPage.AddStamp(stamp);
                    xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace);
                }
                xIndent = 0;
                var yIdentReduce = (arg.WatermarkHeight + arg.WatermarkVerticalSpace);

                yIndent -= yIdentReduce;
            }
        }
示例#4
0
        /// <summary>
        /// 以角度線性期function來呈現
        /// </summary>
        /// <param name="pdfPage"></param>
        /// <param name="arg"></param>
        private static void AddWatermarkRepeatRotateAngle(Aspose.Pdf.Page pdfPage, WatermarkArg arg)
        {
            if (arg.WatermarkHeight < minValue)
            {
                throw new ArgumentException($"{nameof(arg.WatermarkHeight)} must greater than {minValue}");
            }
            if (arg.WatermarkWidth < minValue)
            {
                throw new ArgumentException($"{nameof(arg.WatermarkWidth)} must greater than {minValue}");
            }

            var text       = new FormattedText(arg.Watermark);
            var yIndent    = pdfPage.CropBox.Height - arg.WatermarkHeight;
            var yLimit     = 0 - (arg.WatermarkHeight + arg.WatermarkVerticalSpace);
            var pageWidth  = pdfPage.CropBox.Width;
            var pageHeight = pdfPage.CropBox.Height;
            var xIndent    = 0d;

            while (yIndent > yLimit)
            {
                var y = yIndent;
                while (xIndent < pageWidth && y < pageHeight)
                {
                    var stamp = new TextStamp(text);
                    stamp.RotateAngle = arg.RotateAngle;
                    stamp.XIndent     = xIndent;
                    stamp.YIndent     = y;
                    stamp.Opacity     = arg.Opacity;
                    stamp.Width       = arg.WatermarkWidth;
                    stamp.Height      = arg.WatermarkHeight;
                    pdfPage.AddStamp(stamp);
                    xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace);
                    xIndent  = xIndent + Math.Cos(30) * arg.WatermarkWidth;
                    y        = y + Math.Sign(30) * (arg.WatermarkHeight + arg.WatermarkVerticalSpace);
                }
                xIndent = 0;
                var yIdentReduce = (arg.WatermarkHeight + arg.WatermarkVerticalSpace);
                yIndent -= yIdentReduce;
            }

            //到底了,要再連走 X
            var baseX = 0d;

            while (baseX < pageWidth)
            {
                var y = yIndent;
                xIndent = baseX;
                while (xIndent < pageWidth)
                {
                    var stamp = new TextStamp(text);
                    stamp.RotateAngle = arg.RotateAngle;
                    stamp.XIndent     = xIndent;
                    stamp.YIndent     = y;
                    stamp.Opacity     = arg.Opacity;
                    stamp.Width       = arg.WatermarkWidth;
                    stamp.Height      = arg.WatermarkHeight;
                    pdfPage.AddStamp(stamp);
                    xIndent += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace);
                    xIndent  = xIndent + Math.Cos(30) * arg.WatermarkWidth;
                    y        = y + Math.Sign(30) * (arg.WatermarkHeight + arg.WatermarkVerticalSpace);
                }
                baseX += (arg.WatermarkWidth + arg.WatermarkHorizontalSpace);;
            }
        }