Beispiel #1
0
        /// <summary>
        ///  Add configured Page Sigmatutre
        /// </summary>
        /// <param name="fileBlob"></param>
        /// <returns></returns>
        public byte[] AddLabelToPages(byte[] fileBlob)
        {
            PdfReader reader   = new PdfReader(fileBlob);
            string    tempFile = Path.Combine(Path.GetTempPath(), "SC_" + Guid.NewGuid() + ".tmp");

            logger.DebugFormat("EtichettaPdfWithOverprint..Pages:{0}", reader.NumberOfPages);
            try
            {
                Document       document = null;
                PdfWriter      writer   = null;
                PdfContentByte cb       = null;
                try
                {
                    for (int j = 1; j <= reader.NumberOfPages; j++)
                    {
                        var rt   = reader.GetPageRotation(j);
                        var rect = reader.GetPageSizeWithRotation(j); //
                        if (j == 1)
                        {
                            document = new Document(rect);
                            writer   = PdfWriter.GetInstance(document, new FileStream(tempFile, FileMode.Create));

                            /*
                             * PDF-A: lo standard pdf-a NECESSITA che i font siano embedded nel documento.
                             * I font usati per stampare l'etichetta ed il numero di pagina NON fanno eccezione.
                             */
                            //writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);//i tried with 1_7instead, same result
                            //writer.PDFXConformance = PdfWriter.PDFA1B;
                            //writer.CreateXmpMetadata();
                            document.Open();
                            cb = writer.DirectContent;
                        }
                        else
                        {
                            document.SetPageSize(rect);
                            document.NewPage();
                        }

                        var page = writer.GetImportedPage(reader, j);
                        logger.DebugFormat("AddLabelToPages -> GetPageRotation: {0}", rt);
                        logger.DebugFormat("AddLabelToPages -> scalePercent: {0}", scalePercent);
                        if (rt == 90)
                        {
                            cb.AddTemplate(page, 0, -1.0F, scalePercent, 0, 0, rect.Height);
                        }
                        else if (rt == 270)
                        {
                            cb.AddTemplate(page, 0, scalePercent, -1f, 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, 1f, 0, 0, scalePercent, 0, 0);
                        }
                        //#warning GESTIRE MEGLIO LA ROTAZIONE! Se il documento viene ruotato, la grafica eventualmente associata ai commenti PDF (linee rosse, fumetti, eccetera) viene disegnata sbagliata.
                        //                            cb.AddTemplate(page, 0, 0);
                        // put header
                        string testo = testoEtichetta.Replace("(pagina)", j.ToString());
                        testo = testo.Replace("(pagine)", reader.NumberOfPages.ToString());

                        string FontName = FontFactory.COURIER;
                        if (string.Compare(FontFace, "HELVETICA", true) == 0)
                        {
                            FontName = FontFactory.HELVETICA;
                        }
                        if (string.Compare(FontFace, "TIMES", true) == 0)
                        {
                            FontName = FontFactory.TIMES;
                        }
                        int style = iTextSharp.text.Font.NORMAL;
                        if (FontStyle.IndexOf("Bold") >= 0)
                        {
                            style |= iTextSharp.text.Font.BOLD;
                        }
                        if (FontStyle.IndexOf("Italic") >= 0)
                        {
                            style |= iTextSharp.text.Font.ITALIC;
                        }

                        var phrase = new Phrase();
                        var chunk  = new Chunk(testo, FontFactory.GetFont(FontName, font.SizeInPoints, style, new iTextSharp.text.BaseColor(0, 0, 0)));
                        phrase.Add(chunk);
                        var ct = new ColumnText(cb);
                        ct.SetSimpleColumn(phrase, posizione.X, (rect.Height - font.SizeInPoints) - posizione.Y, rect.Right, 0, 0, Element.ALIGN_LEFT);
                        ct.Go();
                        if (testoFooter.Length > 0)
                        {
                            testo = testoFooter.Replace("(pagina)", j.ToString());
                            testo = testo.Replace("(pagine)", reader.NumberOfPages.ToString());

                            phrase = new Phrase();
                            chunk  = new Chunk(testo, FontFactory.GetFont(FontName, font.SizeInPoints, style, new iTextSharp.text.BaseColor(0, 0, 0)));
                            phrase.Add(chunk);
                            ct = new ColumnText(cb);
                            ct.SetSimpleColumn(phrase, posizione.X, posizione.Y, rect.Right, 0, 0, Element.ALIGN_LEFT);
                            ct.Go();
                        }
                        cb.Stroke();

                        PrintRedirected.ExtractPdfComments(ref reader, ref writer, j);
                    }
                }
                finally
                {
                    ////Try to dispose ITextSharp
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (document != null)
                    {
                        document.Close();
                    }
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
                return(File.ReadAllBytes(tempFile));
            }
            finally
            {
                try
                {
                    bool DelFile = string.Compare(ConfigurationManager.AppSettings["DeleteFile"], "true", true) == 0;
                    if (DelFile)
                    {
                        File.Delete(tempFile);
                    }
                    else
                    {
                        logger.DebugFormat("EtichettaPdfWithOverprint..File:{0}", tempFile);
                    }
                }
                catch (Exception ex) { logger.Error(ex); }
            }
        }
Beispiel #2
0
        public byte[] AddLabelToPagesNew(byte[] fileBlob)
        {
            byte[] result = null;
            var    pdf    = new PdfContentInfo(fileBlob);

            PdfReader reader = null;
            PdfWriter writer = null;

            using (reader = pdf.GetReader())
                using (var ms = new MemoryStream())
                    using (var document = new Document())
                        using (writer = PdfWriter.GetInstance(document, ms))
                        {
                            logger.DebugFormat("EtichettaPdfWithOverprint..Pages:{0}", reader.NumberOfPages);
                            try
                            {
                                PdfContentByte cb = null;

                                document.Open();
                                cb = writer.DirectContent;

                                for (int j = 1; j <= reader.NumberOfPages; j++)
                                {
                                    var rt   = reader.GetPageRotation(j);
                                    var rect = reader.GetPageSizeWithRotation(j);

                                    document.SetPageSize(rect);
                                    document.NewPage();

                                    var page = writer.GetImportedPage(reader, j);

                                    var tx = PdfTransformerFactory.Default().SetOrigin(page, rect);
                                    cb.TransformTo(tx);

                                    string testo = testoEtichetta.Replace("(pagina)", j.ToString());
                                    testo = testo.Replace("(pagine)", reader.NumberOfPages.ToString());

                                    string FontName = FontFactory.COURIER;
                                    if (string.Compare(FontFace, "HELVETICA", true) == 0)
                                    {
                                        FontName = FontFactory.HELVETICA;
                                    }
                                    if (string.Compare(FontFace, "TIMES", true) == 0)
                                    {
                                        FontName = FontFactory.TIMES;
                                    }
                                    int style = iTextSharp.text.Font.NORMAL;
                                    if (FontStyle.IndexOf("Bold") >= 0)
                                    {
                                        style |= iTextSharp.text.Font.BOLD;
                                    }
                                    if (FontStyle.IndexOf("Italic") >= 0)
                                    {
                                        style |= iTextSharp.text.Font.ITALIC;
                                    }

                                    var phrase = new Phrase();
                                    var chunk  = new Chunk(testo, FontFactory.GetFont(FontName, font.SizeInPoints, style, new iTextSharp.text.BaseColor(0, 0, 0)));
                                    phrase.Add(chunk);
                                    var ct = new ColumnText(cb);
                                    ct.SetSimpleColumn(phrase, posizione.X, (rect.Height - font.SizeInPoints) - posizione.Y, rect.Right, 0, 0, Element.ALIGN_LEFT);
                                    ct.Go();
                                    if (testoFooter.Length > 0)
                                    {
                                        testo = testoFooter.Replace("(pagina)", j.ToString());
                                        testo = testo.Replace("(pagine)", reader.NumberOfPages.ToString());

                                        phrase = new Phrase();
                                        chunk  = new Chunk(testo, FontFactory.GetFont(FontName, font.SizeInPoints, style, new iTextSharp.text.BaseColor(0, 0, 0)));
                                        phrase.Add(chunk);
                                        ct = new ColumnText(cb);
                                        ct.SetSimpleColumn(phrase, posizione.X, posizione.Y, rect.Right, 0, 0, Element.ALIGN_LEFT);
                                        ct.Go();
                                    }
                                    cb.Stroke();

                                    PrintRedirected.ExtractPdfComments(ref reader, ref writer, j);
                                }

                                document.Close();
                                result = ms.ToDeepCopyArray();
                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                                throw;
                            }
                        }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Aggiunge al PDF la signatura della firma
        /// </summary>
        /// <param name="fileBlob"></param>
        /// <returns>
        /// PDF con signature inclusa
        /// </returns>
        public byte[] AddSignatureToPageAndResize(byte[] fileBlob, byte[] originalFileBlob)
        {
            logger.DebugFormat("AddSignatureToPageAndResize..New");
            string inFile = Path.Combine(Path.GetTempPath(), "SC_" + Guid.NewGuid() + ".tmp");

            try
            {
                PdfReader reader   = null;
                Document  document = null;
                PdfWriter writer   = null;
                try
                {
                    reader   = new PdfReader(fileBlob);
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    writer   = PdfWriter.GetInstance(document, new FileStream(inFile, FileMode.OpenOrCreate));
                    writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);//i tried with 1_7instead, same result
                    writer.PDFXConformance = PdfWriter.PDFX1A2001;
                    writer.CreateXmpMetadata();
                    document.Open();
                    var cb = writer.DirectContent;
                    iTextSharp.text.Rectangle rect = null;
                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        rect = reader.GetPageSizeWithRotation(i);
                        document.SetPageSize(rect);
                        document.NewPage();
                        var page = writer.GetImportedPage(reader, i);
                        if (rect.Rotation == 90)
                        {
                            cb.AddTemplate(page, 0, -1.0F, scalePercent, 0, 0, rect.Height);
                        }
                        else if (rect.Rotation == 270)
                        {
                            cb.AddTemplate(page, 0, scalePercent, -1f, 0, rect.Width, 0);
                        }
                        else if (rect.Rotation == 180)
                        {
                            double angle = Math.PI; //Math.sin/cos want in radians
                            switch (rect.Rotation)
                            {
                            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 * scalePercent);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1, 0, 0, scalePercent, 1, 1);
                            //cb.AddTemplate(page, 1f, 0, 0, scalePercent, 0, 0);
#warning GESTIRE MEGLIO LA ROTAZIONE! Se il documento viene ruotato, la grafica eventualmente associata ai commenti PDF (linee rosse, fumetti, eccetera) viene disegnata sbagliata.
                            //cb.AddTemplate(page, 0, 0);
                        }
                        PrintRedirected.ExtractPdfComments(ref reader, ref writer, i);
                    }

                    if (coverWMF.Length > 0)    // retro copertina con le firme
                    {
                        document.NewPage();
                        P7Mmanager p7m = new P7Mmanager();
                        if (p7m.LastErr.Length > 0)
                        {
                            logger.Debug("Warning in creazione CompEd: " + p7m.LastErr);
                            p7m.LastErr = "";
                        }
                        logger.Debug("ExtractDocumentFromBuffer");
                        string originalFileName;
                        p7m.ExtractDocumentFromBuffer(originalFileBlob, out originalFileName);
                        p7m.PrintMetaFileDocumentToFile(null, cb, document, rect.Height);
                        p7m.Close();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    throw;
                }
                finally
                {
                    ////Try to dispose ITextSharp
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (document != null)
                    {
                        document.Close();
                    }
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
                return(File.ReadAllBytes(inFile));
            }
            finally
            {
                bool DelFile = string.Compare(ConfigurationManager.AppSettings["DeleteFile"], "true", true) == 0;
                if (DelFile)
                {
                    File.Delete(inFile);
                }
            }
        }