Example #1
0
        public stDoc ToRasterFormatRgWatermarked(stDoc Doc, string ext, string label, string watermark, string user, string password,
                                                 int AllowPrinting, int AllowModifyContents,
                                                 int AllowCopy, int AllowModifyAnnotations,
                                                 int AllowFillIn, int AllowScreenReaders,
                                                 int AllowAssembly, int AllowDegradedPrinting)
        {
            try
            {
                logger.DebugFormat("ToRasterForToRasterFormatRgWatermarkedmatRg - INIT - {0}", HttpContext.Current.Request.UserHostAddress);

                stDoc res = new stDoc()
                {
                    FileExtension = "PDF"
                };
                byte[] documentContent                    = Convert.FromBase64String(Doc.Blob);
                byte[] pdfDocumentContent                 = PrintRedirectedService.ConvertToFormatLabeled(documentContent, Doc.FileExtension, "PDF", label);
                byte[] watermarkedDocumentContent         = PrintRedirectedService.EtichettaWatermark(pdfDocumentContent, watermark);
                ConversionSecureParameter secureParameter = new ConversionSecureParameter
                {
                    User                   = user,
                    Password               = password,
                    AllowPrinting          = Convert.ToBoolean(AllowPrinting),
                    AllowModifyContents    = Convert.ToBoolean(AllowModifyContents),
                    AllowCopy              = Convert.ToBoolean(AllowCopy),
                    AllowModifyAnnotations = Convert.ToBoolean(AllowModifyAnnotations),
                    AllowFillIn            = Convert.ToBoolean(AllowFillIn),
                    AllowScreenReaders     = Convert.ToBoolean(AllowScreenReaders),
                    AllowAssembly          = Convert.ToBoolean(AllowAssembly),
                    AllowDegradedPrinting  = Convert.ToBoolean(AllowDegradedPrinting)
                };
                byte[] lockedDocumentContent = PdfLabelerService.RightPdf(watermarkedDocumentContent, secureParameter);
                res.Blob = Convert.ToBase64String(lockedDocumentContent);
                return(res);
            }
            catch (Exception exception)
            {
                logger.Error(exception);
                throw;
            }
            finally
            {
                logger.DebugFormat("ToRasterForToRasterFormatRgWatermarkedmatRg - END -{0}", HttpContext.Current.Request.UserHostAddress);
            }
        }
Example #2
0
        public byte[] RightPdf(byte[] fileBlob, ConversionSecureParameter secureParameter)
        {
            if (string.IsNullOrEmpty(secureParameter.Password))
            {
                secureParameter.Password = ConfigurationManager.AppSettings["PdfOwnerPassword"] == null ? "Passw0rd" : ConfigurationManager.AppSettings["PdfOwnerPassword"].ToString();
            }
            PdfReader reader = new PdfReader(fileBlob);

            using (MemoryStream fileMemoryStream = new MemoryStream())
            {
                Document       document = null;
                PdfWriter      writer   = null;
                PdfContentByte cb       = null;
                try
                {
                    PdfImportedPage           page;
                    iTextSharp.text.Rectangle rect = null;

                    for (int j = 0; j < reader.NumberOfPages; j++)
                    {
                        int rt = reader.GetPageRotation(j + 1);
                        rect = reader.GetPageSizeWithRotation(j + 1); //
                        if (j == 0)
                        {
                            document = new Document(rect);
                            writer   = PdfWriter.GetInstance(document, fileMemoryStream);
                            writer.SetEncryption(PdfWriter.STRENGTH40BITS, secureParameter.User, secureParameter.Password,
                                                 (secureParameter.AllowPrinting ? PdfWriter.AllowPrinting : 0)
                                                 | (secureParameter.AllowModifyContents ? PdfWriter.AllowModifyContents : 0)
                                                 | (secureParameter.AllowCopy ? PdfWriter.AllowCopy : 0)
                                                 | (secureParameter.AllowModifyAnnotations ? PdfWriter.AllowModifyAnnotations : 0)
                                                 | (secureParameter.AllowFillIn ? PdfWriter.AllowFillIn : 0)
                                                 | (secureParameter.AllowScreenReaders ? PdfWriter.AllowScreenReaders : 0)
                                                 | (secureParameter.AllowAssembly ? PdfWriter.AllowAssembly : 0)
                                                 | (secureParameter.AllowDegradedPrinting ? PdfWriter.AllowDegradedPrinting : 0));
                            document.Open();
                            cb = writer.DirectContent;
                        }
                        else
                        {
                            document.SetPageSize(rect);
                            document.NewPage();
                        }

                        page = writer.GetImportedPage(reader, j + 1);

                        if (rt == 90)
                        {
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, rect.Height);
                        }
                        else if (rt == 270)
                        {
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, rect.Width, 0);
                        }
                        else if (rt == 180)
                        {
                            double angle = Math.PI; //Math.sin/cos want in radians
                            switch (rt)
                            {
                            case 90: angle = Math.PI * 90 / 180; break;

                            case 180: angle = Math.PI * 180 / 180; break;

                            case 270: angle = Math.PI * 270 / 180; break;
                            }
                            float a = (float)Math.Cos(angle);
                            float b = (float)Math.Sin(angle);
                            float c = (float)-Math.Sin(angle);
                            float d = (float)Math.Cos(angle);
                            cb.AddTemplate(page, a, b, c, d, rect.Width, rect.Height);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                        }
                        //if (rt == 90 || rt == 270)
                        //    cb.AddTemplate(page, 0, 1f, -1f, 0, 0, rect.Height);
                        //else
                        //    cb.AddTemplate(page, 0, 0);
                    }
                }
                finally
                {
                    if (document != null)
                    {
                        document.Close();
                    }
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
                return(fileMemoryStream.ToArray());
            }
        }