Exemplo n.º 1
0
        public override Arx_File On_Dm_Profile_GetDocument(BaseRegisteredClient client, Dm_Profile dmProfile, Arx_File file, ARXCancelEventArgs e)
        {
            // Faccio la trasformazione del documento in uscita per i PDF
            // Audit dell'evento di modifica di un documento
            string logFilePath = GetAuditFilePath();

            File.AppendAllText(logFilePath, string.Format("{0} GetDocument: {1} [{2} Bytes] Docnumber: {3}/{4} user: {5} SoftwareType: {6} ({7}){8}"
                                                          , DateTime.Now.ToString("O")
                                                          , file.FileName
                                                          , file.FileLength
                                                          , dmProfile.DOCNUMBER
                                                          , dmProfile.REVISIONE
                                                          , client.Utente.ToString()
                                                          , client.SoftwareType
                                                          , client.SoftwareName
                                                          , Environment.NewLine));

            // Trasformazione dei documenti PDF per le bolle DOCUMENTTYPE = 2
            if (dmProfile.DOCUMENTTYPE == 2)
            {
                if (file != null && file.FileName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))
                {
                    string originalFile            = file.CurrentFile;
                    string outputWatermarkFileName = Path.GetFileName(file.FileName);
                    string outputWatermarkFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Substring(9) + outputWatermarkFileName);

                    // Voglio creare un barcode con il valore del campo numero del profilo
                    string codeText = dmProfile.NUMERO ?? string.Empty;

                    PdfReader originalFileReader = new PdfReader(originalFile);
                    using (FileStream fs = new FileStream(outputWatermarkFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                        using (PdfStamper stamper = new PdfStamper(originalFileReader, fs))
                        {
                            // https://www.codeproject.com/Questions/865666/Adding-comment-on-pdf-layer-created-using-iTextsha
                            int pageCount = originalFileReader.NumberOfPages;

                            // Create New Layer for Watermark
                            PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
                            // Loop through each Page
                            for (int i = pageCount; i <= pageCount; i++)
                            {
                                // Getting the Page Size
                                Rectangle rect = originalFileReader.GetPageSize(i);

                                var posY = rect.Height - 50;
                                var posX = 50;

                                // Get the ContentByte object
                                PdfContentByte cb = stamper.GetUnderContent(i);

                                // Tell the cb that the next commands should be "bound" to this new layer
                                cb.BeginLayer(layer);
                                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 55);

                                PdfGState gState = new PdfGState();
                                cb.SetGState(gState);

                                string        codbartest = codeText;
                                BarcodePDF417 bcpdf417   = new BarcodePDF417();
                                //Asigna el código de barras en base64 a la propiedad text del objeto..
                                bcpdf417.Text = ASCIIEncoding.ASCII.GetBytes(codbartest);
                                Image imgpdf417 = bcpdf417.GetImage();
                                imgpdf417.SetAbsolutePosition(posX, posY);
                                imgpdf417.ScalePercent(200);
                                cb.AddImage(imgpdf417);
                                // Close the layer
                                cb.EndLayer();
                            }

                            var pdfArxFile = new Arx_File(outputWatermarkFilePath)
                            {
                                FileName = outputWatermarkFileName
                            };

                            // Restituisco il file elaborato
                            return(pdfArxFile);
                        }
                }
            }

            return(file);
        }
Exemplo n.º 2
0
        public override Arx_File On_Before_Dm_Profile_SetDocument(BaseRegisteredClient client, Dm_Profile dmProfile, Arx_File file, bool insertDocument, ARXCancelEventArgs e)
        {
            // RESTRIZIONI:
            // Voglio che per le bolle (DOCUMENTTYPE == 2) il documento debba essere un pdf
            if (dmProfile.DOCUMENTTYPE == 2)
            {
                if (!file.FileName.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase))
                {
                    // Mi salvo una copia del file vietato
                    var copyFilePath = Path.Combine(_workingDirectory, file.FileName);

                    if (File.Exists(copyFilePath))
                    {
                        File.Delete(copyFilePath);
                    }

                    File.Copy(file.CurrentFile, copyFilePath);

                    // Riporto l'incidente
                    string logFilePath = GetAuditFilePath();
                    File.AppendAllText(logFilePath, string.Format("{0} SetDocument !!VIETATO!!: {1} [{2} Bytes] Docnumber: {3}/{4} user: {5} SoftwareType: {6} ({7}){8}"
                                                                  , DateTime.Now.ToString("O")
                                                                  , file.FileName
                                                                  , file.FileLength
                                                                  , dmProfile.DOCNUMBER
                                                                  , dmProfile.REVISIONE
                                                                  , client.Utente.ToString()
                                                                  , client.SoftwareType
                                                                  , client.SoftwareName
                                                                  , Environment.NewLine));

                    e.Message = "Il documento deve essere un pdf";
                    e.Cancel  = true;
                    return(null);
                }
            }

            return(base.On_Before_Dm_Profile_SetDocument(client, dmProfile, file, insertDocument, e));
        }