コード例 #1
0
ファイル: PdfLabeler.cs プロジェクト: maurbone/DocSuitePA
        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);
        }
コード例 #2
0
ファイル: PdfLabeler.cs プロジェクト: maurbone/DocSuitePA
        public byte[] AddSignatureToPageAndResizeNew(byte[] fileBlob, byte[] originalFileBlob)
        {
            logger.DebugFormat("AddSignatureToPageAndResizeNew..New");

            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(reader.GetPageSizeWithRotation(1)))
                        using (writer = PdfWriter.GetInstance(document, ms))
                        {
                            try
                            {
                                writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
                                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.NewPage(rect);
                                    var page = writer.GetImportedPage(reader, i);

                                    var tx = PdfTransformerFactory.Default().SetOrigin(page, rect);
                                    tx.ContentScaling = Convert.ToInt32(scalePercent * 100F);
                                    logger.DebugFormat("AddSignatureToPageAndResizeNew -> tx.ContentScaling: {0}", tx.ContentScaling);
                                    writer.DirectContent.TransformTo(tx);

                                    //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();
                                }

                                document.Close();
                                result = ms.ToDeepCopyArray();
                            }
                            catch (Exception ex)
                            {
                                logger.Error(ex);
                                throw;
                            }
                        }


            return(result);
        }