Beispiel #1
0
        public static byte[] merge_files(List <FileInfo> docs)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream()) {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int document_page_counter = 0;

                foreach (FileInfo fi in docs)
                {
                    PdfReader reader = new PdfReader(fi.FullName);

                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        document_page_counter++;
                        PdfImportedPage ip = copy.GetImportedPage(reader, i);
#if PAGE_NUMBERS
                        PdfCopy.PageStamp ps = copy.CreatePageStamp(ip);

                        ColumnText.ShowTextAligned(ps.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase(string.Format("{0}", document_page_counter)), (ip.Width / 2), 5, ip.Width < ip.Height ? 0 : 1);
                        ps.AlterContents();
#endif
                        copy.AddPage(ip);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }
                document.Close();
                return(ms.GetBuffer());
            }
        }
Beispiel #2
0
        public virtual void TestCreatePdfA_1()
        {
            String f1       = RESOURCES + "copy\\pdfa-1a.pdf";
            String testName = "testCreatePdfA_1.pdf";

            FileStream outputPdfStream = new FileStream(outputDir + testName, FileMode.Create);
            Document   document        = new Document();
            PdfACopy   copy            = new PdfACopy(document, outputPdfStream, PdfAConformanceLevel.PDF_A_1B);

            copy.CreateXmpMetadata();
            document.Open();
            document.AddLanguage("en-US");
            PdfReader reader = new PdfReader(f1);

            PdfImportedPage page = copy.GetImportedPage(reader, 1);

            PdfCopy.PageStamp stamp = copy.CreatePageStamp(page);
            Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 24);

            ColumnText.ShowTextAligned(stamp.GetUnderContent(), Element.ALIGN_CENTER, new Phrase("Hello world!", font), 100, 500, 0);
            stamp.AlterContents();
            copy.AddPage(page);

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read,
                                                        FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);

            iccProfileFileStream.Close();

            copy.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            copy.Close();
        }
Beispiel #3
0
        public static void MergeFiles(List <string> sourceFiles, string rutaPdfJoin, bool addCopy)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                FileStream fl   = new FileStream(rutaPdfJoin, FileMode.Create, FileAccess.Write, FileShare.None);
                PdfCopy    copy = new PdfCopy(document, fl);
                document.Open();

                // Iterar pdfs
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    PdfReader reader        = new PdfReader(sourceFiles[fileCounter]);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterar paginas
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);

                        pageStamp.AlterContents();
                        copy.AddPage(importedPage);
                        if (addCopy)
                        {
                            copy.AddPage(importedPage);
                        }
                    }
                    copy.FreeReader(reader);
                    reader.Close();
                }
                document.Close();
            }
        }
Beispiel #4
0
        private void StampFooterDataIntoPdf(PdfCopy.PageStamp pageStamp, string labelFooter)
        {
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

            iTextSharp.text.Font fontText = new iTextSharp.text.Font(bf, 8);

            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(labelFooter, fontText), 315, 20, 0);
        }
Beispiel #5
0
    public static byte[] merge_files(List <FileInfo> docs)
    {
        using (Document document = new Document()) {
            using (MemoryStream ms = new MemoryStream()) {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int document_page_counter = 0;
                int total_pages           = count_pages(docs);
                int font_size             = ProtoDrawingCollector.csproj.Properties.Settings.Default.PageStampSize;
                foreach (FileInfo fi in docs)
                {
                    using (PdfReader reader = new PdfReader(fi.FullName)) {
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            document_page_counter++;
                            PdfImportedPage ip = copy.GetImportedPage(reader, i);
#if PAGE_NUMBERS
                            PdfCopy.PageStamp        ps       = copy.CreatePageStamp(ip);
                            PdfContentByte           cb       = ps.GetOverContent();
                            System.Drawing.Rectangle sdr      = ProtoDrawingCollector.csproj.Properties.Settings.Default.PageStampWhiteoutRectangle;
                            System.Drawing.Point     location = ProtoDrawingCollector.csproj.Properties.Settings.Default.PageStampLoc;
                            //System.Drawing.Rectangle sdr = new System.Drawing.Rectangle(1154, 20, 47, 16);
                            Rectangle size = reader.GetPageSize(i);
                            if ((size.Height * size.Width) < (1224 * 792))
                            {
                                sdr = ProtoDrawingCollector.csproj.Properties.Settings.Default.PageStampWhiteoutRectangleA4;
                                //sdr = new System.Drawing.Rectangle(720, 20, 47, 16);
                                location = ProtoDrawingCollector.csproj.Properties.Settings.Default.PageStampLocA4;
                            }
                            Rectangle r = new Rectangle(sdr.Left, sdr.Bottom, sdr.Right, sdr.Top);
                            //OnAppend(new AppendEventArgs(string.Format(@"{0} x {1}", size.Width, size.Height)));
                            r.BackgroundColor = BaseColor.WHITE;
                            cb.Rectangle(r);
                            Font  f = FontFactory.GetFont("Century Gothic", font_size);
                            Chunk c = new Chunk(string.Format("{0} OF {1}", document_page_counter, total_pages), f);
                            c.SetBackground(BaseColor.WHITE);
                            ColumnText.ShowTextAligned(ps.GetOverContent(),
                                                       Element.ALIGN_CENTER,
                                                       new Phrase(c),
                                                       location.X, location.Y,
                                                       ip.Width < ip.Height ? 0 : 1);
                            ps.AlterContents();
#endif
                            copy.AddPage(ip);
                        }

                        //copy.FreeReader(reader);
                        //reader.Close();
                    }
                }
                document.Close();
                return(ms.GetBuffer());
            }
        }
    }
        private byte[] _RedactUsefulDataFromDocument()
        {
            using (Document document = new Document()) {
                using (MemoryStream memoryStream = new MemoryStream()) {
                    try {
                        PdfCopy copy = new PdfCopy(document, memoryStream);
                        document.Open();
                        using (PdfReader pdfReader = new PdfReader(source)) {
                            for (int i = 1; i < pdfReader.NumberOfPages + 1; i++)
                            {
                                PdfImportedPage   importedPage = copy.GetImportedPage(pdfReader, i);
                                PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);
                                PdfContentByte    contentByte  = pageStamp.GetOverContent();

                                if (i == 1)
                                {
                                    float y_offset = 137F;
                                    for (int j = 0; j < 5; j++, y_offset -= 13.5F)
                                    {
                                        Rectangle sourceRevRectangle = new Rectangle(21, y_offset, 44, y_offset + 10);
                                        sourceRevRectangle.BackgroundColor = BaseColor.WHITE;
                                        Rectangle sourceECRRectangle = new Rectangle(46, y_offset, 71, y_offset + 10);
                                        sourceECRRectangle.BackgroundColor = BaseColor.WHITE;
                                        Rectangle sourceDescrRectangle = new Rectangle(73, y_offset, 988, y_offset + 10);
                                        sourceDescrRectangle.BackgroundColor = BaseColor.WHITE;
                                        Rectangle sourceDrwnBy = new Rectangle(991, y_offset, 1061, y_offset + 10);
                                        sourceDrwnBy.BackgroundColor = BaseColor.WHITE;
                                        Rectangle sourceAppBy = new Rectangle(1063, y_offset, 1133, y_offset + 10);
                                        sourceAppBy.BackgroundColor = BaseColor.WHITE;
                                        Rectangle sourceDate = new Rectangle(1135, y_offset, 1205, y_offset + 10);
                                        sourceDate.BackgroundColor = BaseColor.WHITE;

                                        contentByte.Rectangle(sourceRevRectangle);
                                        contentByte.Rectangle(sourceECRRectangle);
                                        contentByte.Rectangle(sourceDescrRectangle);
                                        contentByte.Rectangle(sourceDrwnBy);
                                        contentByte.Rectangle(sourceAppBy);
                                        contentByte.Rectangle(sourceDate);

                                        pageStamp.AlterContents();
                                    }
                                }
                                copy.AddPage(importedPage);
                            }
                        }
                    } catch (System.Exception e) {
                        throw e;
                    } finally {
                        document.Close();
                    }
                    return(memoryStream.GetBuffer());
                }
            }
        }
        //public void ConvertDocumentToPDF(byte[] bytes, string pathFileOut,string pathFileOutTemp,List<Archive> ImageRoutes)
        //{

        //    try {

        //        var inputAsString = Convert.ToBase64String(bytes.ToArray(), 0, bytes.ToArray().Length);
        //        File.WriteAllBytes(pathFileOutTemp, Convert.FromBase64String(inputAsString));

        //        //Incrustando Imagenes
        //        IncrustingImage(pathFileOutTemp, ImageRoutes,pathFileOut);
        //        //if (ImageRoutes != null && ImageRoutes.Count > 0)

        //        string textDelete = "Unlicensed version. Please register @ templater.info";

        //        DocumentCore document = DocumentCore.Load(pathFileOutTemp, new DocxLoadOptions());
        //        int countDel = 0;
        //        foreach (ContentRange cr in document.Content.Find(textDelete).Reverse())
        //        {
        //            cr.Delete();
        //            countDel++;
        //        }

        //        if (File.Exists(pathFileOut)) File.Delete(pathFileOut);

        //            if (ImageRoutes.Where(x=>x.typeAdjunte == TypeAdjunte.pdf).Count() > 0)
        //            {
        //                List<byte[]> listByte = new List<byte[]>();

        //                using (MemoryStream PDFGenerate = new MemoryStream())
        //                {
        //                    document.Save(PDFGenerate, SaveOptions.PdfDefault);
        //                    listByte.Add(PDFGenerate.ToArray());
        //                }

        //                foreach (var file in ImageRoutes.Where(x => x.typeAdjunte == TypeAdjunte.pdf))
        //                {
        //                   byte[] PdfBype = null;
        //                    if (File.Exists(file.RutaArchivo))
        //                    {
        //                        PdfBype = System.IO.File.ReadAllBytes(file.RutaArchivo);
        //                        if (PdfBype != null)
        //                        {
        //                            listByte.Add(PdfBype);
        //                        }
        //                    }
        //                }

        //                byte[] PdfFinale = MergeFiles(listByte);
        //                var inputFinale = Convert.ToBase64String(PdfFinale.ToArray(), 0, PdfFinale.ToArray().Length);
        //                File.WriteAllBytes(pathFileOut, Convert.FromBase64String(inputFinale));
        //            }

        //            else
        //            {
        //                document.Save(pathFileOut);
        //            }


        //        File.Delete(pathFileOutTemp);
        //    }
        //    catch(Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        public static byte[] MergeFiles(Dictionary <int, byte[]> sourceFiles)
        {
            iTextSharp.text.Document document = new iTextSharp.text.Document();
            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                sourceFiles = sourceFiles.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);


                // Iterate through all pdf documents

                foreach (KeyValuePair <int, byte[]> pair in sourceFiles)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(pair.Value);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);

                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), iTextSharp.text.Element.ALIGN_CENTER,
                                                   new Phrase(""), importedPage.Width / 2, importedPage.Height - 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);

                        // Write footer
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), iTextSharp.text.Element.ALIGN_CENTER,
                                                   new Phrase(String.Format("", documentPageCounter)), importedPage.Width / 2, 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);

                        pageStamp.AlterContents();

                        copy.AddPage(importedPage);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                return(ms.GetBuffer());
            }
        }
Beispiel #8
0
        public async Task <byte[]> MergeFiles(List <ClaimFilePage> ClaimFilePages)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                foreach (var claimPage in ClaimFilePages)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(claimPage.Page);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);

                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase(string.Empty), importedPage.Width / 2, importedPage.Height - 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);

                        // Write footer
                        if (claimPage.ClaimId > 0)
                        {
                            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                       new Phrase(String.Format($"Reclamo {claimPage.ClaimId}")), importedPage.Width / 2, 15,
                                                       importedPage.Width < importedPage.Height ? 0 : 1);
                        }

                        pageStamp.AlterContents();

                        copy.AddPage(importedPage);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                return(ms.GetBuffer());
            }
        }
Beispiel #9
0
        public static byte[] MergeFiles(List <byte[]> sourceFiles)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(sourceFiles[fileCounter]);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);

                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);

                        // Write footer
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                                                   new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                                                   importedPage.Width < importedPage.Height ? 0 : 1);

                        pageStamp.AlterContents();

                        copy.AddPage(importedPage);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                return(ms.GetBuffer());
            }
        }
Beispiel #10
0
        public byte[] PDF_Merger(List <string> li, string PrintedName)
        {
            List <byte[]> sourceFiles = FileReadIn(li);
            Document      document    = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(sourceFiles[fileCounter]);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);
                        pageStamp.AlterContents();
                        copy.AddPage(importedPage);
                    }
                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                if (PrintedName == "")
                {
                    PrintedName = "PM Merger Document ";
                }
                DateTime thisDay = DateTime.Today;
                System.IO.Directory.CreateDirectory(FolderUrl + "\\Merged Documents");
                System.IO.File.WriteAllBytes(FolderUrl + "\\Merged Documents\\" + PrintedName + " ( " + thisDay.ToString("yyyy. MM. dd.") + " ).pdf", ms.GetBuffer());
                Process.Start(FolderUrl + "\\Merged Documents");

                return(ms.GetBuffer());
            }
        }
Beispiel #11
0
        public string ProductQuoteToSmallPdf(ProductQuote productQuote, string productQuoteSmallPdfTemplate)
        {
            string fileTemplate   = Path.Combine(CommonHelper.MapPath("~/Documents/Templates"), productQuoteSmallPdfTemplate);
            string fileNameExport = string.Format("{0}_{1}_{2}.pdf", productQuote.ProductQuoteCode, "Rev08", Guid.NewGuid().ToString().Substring(0, 8));
            string fileExport     = Path.Combine(CommonHelper.MapPath("~/Documents/Export"), fileNameExport);


            PdfReader  reader = new PdfReader(fileTemplate);
            FileStream fs     = new FileStream(fileExport, FileMode.Create, FileAccess.Write);

            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);

            PdfCopy copy = new PdfCopy(document, fs);

            document.Open();

            PdfImportedPage page = null;

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                page = copy.GetImportedPage(reader, i);

                //En la primer pagina ponemos los datos
                if (i == 1)
                {
                    PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(page);
                    StampDataIntoSmallPdf(pageStamp, productQuote);
                    pageStamp.AlterContents();
                }

                PdfCopy.PageStamp pageStampFooter = copy.CreatePageStamp(page);
                StampFooterDataIntoPdf(pageStampFooter, productQuote.ProductQuoteSmallPDFFooter);
                pageStampFooter.AlterContents();

                copy.AddPage(page);
            }

            copy.FreeReader(reader);
            document.Close();
            reader.Close();

            return(fileNameExport);
        }
Beispiel #12
0
        public void TestPdf()
        {
            string fileTemplate   = Path.Combine(CommonHelper.MapPath("~/Documents/Templates"), ProductQuoteApp.Resources.Resources.FileNamePdfProductQuoteTemplate);
            string fileNameExport = string.Format("productQuote_{0}_{1}.pdf", 1, Guid.NewGuid().ToString());
            string fileExport     = Path.Combine(CommonHelper.MapPath("~/Documents/Export"), fileNameExport);


            PdfReader  reader = new PdfReader(fileTemplate);
            FileStream fs     = new FileStream(fileExport, FileMode.Create, FileAccess.Write);

            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);

            PdfCopy copy = new PdfCopy(document, fs);

            document.Open();

            PdfImportedPage page = null;

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                page = copy.GetImportedPage(reader, i);

                //En la primer pagina ponemos los datos
                if (i == 1)
                {
                    PdfCopy.PageStamp    pageStamp = copy.CreatePageStamp(page);
                    BaseFont             bf        = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    iTextSharp.text.Font fontText  = new iTextSharp.text.Font(bf, 9);

                    ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase("Buenos Aires, 15 de septiembre de 2017", fontText), 525, 720, 0);
                    //new Phrase("Buenos Aires, 15 de septiembre de 2017", fontText), page.Width / 2, page.Height - 30, page.Width < page.Height ? 0 : 1);

                    pageStamp.AlterContents();
                }

                copy.AddPage(page);
            }

            copy.FreeReader(reader);
            document.Close();
            reader.Close();
        }
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public byte[] MergeFiles(List <byte[]> sourceFiles)
        {
            Document document = new Document();

            using (MemoryStream ms = new MemoryStream())
            {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                    // Create pdf reader
                    PdfReader reader        = new PdfReader(sourceFiles[fileCounter]);
                    int       numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                    {
                        documentPageCounter++;
                        PdfImportedPage   importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp    = copy.CreatePageStamp(importedPage);

                        pageStamp.AlterContents();

                        copy.AddPage(importedPage);
                    }

                    copy.FreeReader(reader);
                    reader.Close();
                }

                document.Close();
                return(ms.GetBuffer());
            }
        }
        public void MergeAndStampPdf(bool resetStampEachPage, String[] input, String output, String stamp)
        {
            PdfReader        stampReader    = new PdfReader(pdfContent[stamp]);
            List <PdfReader> readersToClose = new List <PdfReader>();

            readersToClose.Add(stampReader);

            MemoryStream baos = new MemoryStream();

            try
            {
                Document document = new Document();

                PdfCopy writer = new PdfSmartCopy(document, baos);
                try
                {
                    document.Open();

                    int stampPageNum = 1;

                    foreach (string element in input)
                    {
                        // create a reader for the input document
                        PdfReader documentReader = new PdfReader(
                            new RandomAccessFileOrArray(
                                new RandomAccessSourceFactory().CreateSource(pdfContent[element])
                                )
                            , null);

                        for (int pageNum = 1; pageNum <= documentReader.NumberOfPages; pageNum++)
                        {
                            // import a page from the main file
                            PdfImportedPage mainPage = writer.GetImportedPage(documentReader, pageNum);

                            // make a stamp from the page and get under content...
                            PdfCopy.PageStamp pageStamp = writer.CreatePageStamp(mainPage);

                            // import a page from a file with the stamp...
                            if (resetStampEachPage)
                            {
                                stampReader = new PdfReader(pdfContent[stamp]);
                                readersToClose.Add(stampReader);
                            }
                            PdfImportedPage stampPage = writer.GetImportedPage(stampReader, stampPageNum++);

                            // add the stamp template, update stamp, and add the page
                            pageStamp.GetOverContent().AddTemplate(stampPage, 0, 0);
                            pageStamp.AlterContents();
                            writer.AddPage(mainPage);

                            if (stampPageNum > stampReader.NumberOfPages)
                            {
                                stampPageNum = 1;
                            }
                        }
                    }
                }
                finally
                {
                    writer.Close();
                    document.Close();
                }
            }
            finally
            {
                foreach (PdfReader stampReaderToClose in readersToClose)
                {
                    stampReaderToClose.Close();
                }
            }
            pdfContent[output] = baos.ToArray();
        }
Beispiel #15
0
        private void StampDataIntoSmallPdf(PdfCopy.PageStamp pageStamp, ProductQuote productQuote)
        {
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

            iTextSharp.text.Font fontText      = new iTextSharp.text.Font(bf, 9);
            const float          OFFSET_GRILLA = 215;

            string fechaString = productQuote.DateQuote.ToLongDateString();
            string text        = "Buenos Aires, " + fechaString.Substring(fechaString.IndexOf(",") + 2, fechaString.Length - (fechaString.IndexOf(",") + 2));

            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase(text, fontText), 525, 720, 0);

            //CONTACTO
            text = !string.IsNullOrEmpty(productQuote.CustomerContactName) ? productQuote.CustomerContactName : string.Empty;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 105, 685, 0);

            //EMPRESA
            text = !string.IsNullOrEmpty(productQuote.CustomerCompany) ? productQuote.CustomerCompany : string.Empty;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 105, 673, 0);

            //CODIGO COTIZACION
            text = productQuote.ProductQuoteCode;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 135, 652, 0);

            //PRODUCTO
            text = productQuote.ProductSingleName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 632, 0);

            //CANTIDAD
            text = productQuote.QuantityOpenPurchaseOrder.ToString("#,##0");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 614, 0);

            //ORIGEN
            text = productQuote.ProductBrandName.ToString();
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 597, 0);

            //ENVASE
            text = productQuote.ProductPackagingName.ToString();
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 580, 0);

            //PRECIO
            text = Helper.RoundDecimal(productQuote.Price, 3).ToString("#,###0.000") + " USD/Kg + Impuestos";
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 561, 0);

            //CONDICION DE PAGO
            text = productQuote.PaymentDeadlineName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 544, 0);

            //MONEDA
            text = productQuote.ExchangeTypeName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 526, 0);

            //VALIDEZ DEL PRECIO
            text = productQuote.ProductValidityOfPrice != null ? ((DateTime)productQuote.ProductValidityOfPrice).ToString("dd/MM/yyyy") : "";
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 508, 0);

            //LUGAR DE ENTREGA
            text = !string.IsNullOrEmpty(productQuote.DeliveryAddress) ? productQuote.DeliveryAddress : productQuote.GeographicAreaName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 491, 0);

            //FECHAS ENTREGA
            text = !string.IsNullOrEmpty(productQuote.DatesDeliveryInput) ? productQuote.DatesDeliveryInput : "";
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 473, 0);

            //DISPONIBILIDAD
            text = productQuote.AvailabilityDays != null?productQuote.AvailabilityDays.ToString() : "";

            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), OFFSET_GRILLA, 456, 0);

            //OBSERVACIONES
            text = productQuote.UserObservations + Environment.NewLine + productQuote.Observations;
            ColumnText ct = new ColumnText(pageStamp.GetOverContent());

            ct.SetSimpleColumn(OFFSET_GRILLA, 448, 520, 230, 14, Element.ALIGN_JUSTIFIED);
            ct.AddElement(new Paragraph(new Phrase(text, fontText)));
            ct.Go();

            //FIRMA
            text = productQuote.UserFullName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase(text, fontText), 480, 95, 0); //90
        }
Beispiel #16
0
        //https://www.codeproject.com/Articles/28283/Simple-NET-PDF-Merger
        private void StampDataIntoPdf(PdfCopy.PageStamp pageStamp, ProductQuote productQuote)
        {
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

            iTextSharp.text.Font fontText = new iTextSharp.text.Font(bf, 9);

            string fechaString = productQuote.DateQuote.ToLongDateString();
            string text        = "Buenos Aires, " + fechaString.Substring(fechaString.IndexOf(",") + 2, fechaString.Length - (fechaString.IndexOf(",") + 2));

            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase(text, fontText), 525, 720, 0);

            text = productQuote.CustomerCompany;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 102, 686, 0);

            if (productQuote.CustomerContactName != null)
            {
                text = productQuote.CustomerContactName.ToString();
                ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 102, 671, 0);
            }

            text = productQuote.ProductQuoteCode;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 225, 649, 0);//OK

            text = productQuote.ProductSingleName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 631, 0);

            text = productQuote.ProductBrandName.ToString();
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 613, 0);

            text = productQuote.ProductPackagingName.ToString();
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 595, 0);

            if (productQuote.ProductValidityOfPrice != null)
            {
                text = ((DateTime)productQuote.ProductValidityOfPrice).ToString("dd/MM/yyyy");
                ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 577, 0);
            }

            text = productQuote.SaleModalityName.ToString();
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 559, 0);

            if ((productQuote.DeliveryAddress != null) && (productQuote.DeliveryAddress != ""))
            {
                text = productQuote.DeliveryAddress;
            }
            else
            {
                text = productQuote.GeographicAreaName;
            }
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 542, 0);

            text = productQuote.PaymentDeadlineName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 524, 0);

            text = productQuote.QuantityOpenPurchaseOrder.ToString("#,##0");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 506, 0);

            text = productQuote.DeliveryAmount.ToString();
            if (productQuote.DeliveryAmount == 1)
            {
                text = text + " Entrega";
            }
            else
            {
                text = text + " Entregas";
            }
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 488, 0);

            text = productQuote.MinimumQuantityDelivery.ToString("#,##0");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 470, 0);

            text = productQuote.MaximumMonthsStock.ToString() + " Meses";
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 453, 0);

            text = productQuote.ExchangeTypeName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 436, 0);

            text = Helper.RoundDecimal(productQuote.Price, 3).ToString("#,###0.000");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 418, 0);

            text = Helper.RoundDecimal(productQuote.Price * productQuote.MinimumQuantityDelivery, 2).ToString("#,##0.00");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 400, 0);

            text = Helper.RoundDecimal(productQuote.Price * productQuote.QuantityOpenPurchaseOrder, 2).ToString("#,##0.00");
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_LEFT, new Phrase(text, fontText), 243, 382, 0);

            text = productQuote.UserObservations + Environment.NewLine + productQuote.Observations;
            ColumnText ct = new ColumnText(pageStamp.GetOverContent());

            ct.SetSimpleColumn(243, 375, 520, 230, 14, Element.ALIGN_JUSTIFIED);
            ct.AddElement(new Paragraph(new Phrase(text, fontText)));
            ct.Go();

            text = productQuote.UserFullName;
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase(text, fontText), 500, 120, 0);

            text = "Inquimex SACI";
            ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_RIGHT, new Phrase(text, fontText), 500, 105, 0);
        }
        private static byte[] merge_files(LinkedList <PageInfo> ll)
        {
            using (Document document = new Document()) {
                using (MemoryStream ms = new MemoryStream()) {
                    PdfCopy copy = new PdfCopy(document, ms);
                    document.Open();
                    int total_pages = count_pages(ll);

                    Rectangle sheet_number_blockhout_ = new Rectangle(1062, 17, 1180, 110);
                    sheet_number_blockhout_.BackgroundColor = BaseColor.WHITE;
                    Rectangle revisions_blockout_ = new Rectangle(817, 17, 1032, 110);
                    revisions_blockout_.BackgroundColor = BaseColor.WHITE;
                    Rectangle item_descr_blockout_ = new Rectangle(605, 17, 787, 110);
                    item_descr_blockout_.BackgroundColor = BaseColor.WHITE;
                    Rectangle item_textarea_ = new Rectangle(605, 78, 787, 110);
                    item_textarea_.BackgroundColor = BaseColor.WHITE;
                    Rectangle item_descr_textarea_ = new Rectangle(605, 17, 787, 78);
                    item_descr_textarea_.BackgroundColor = BaseColor.WHITE;

                    var nd_ = ll.First;
                    while (nd_ != null)
                    {
                        if (nd_.Value.fileInfo == null)
                        {
                            nd_ = nd_.Next;
                            continue;
                        }
                        using (PdfReader rdr_ = new PdfReader(nd_.Value.fileInfo.FullName)) {
                            var sn_    = nd_.Value.FirstSheetNo();
                            var descr_ = nd_.Value.FirstDescription();
                            int pg_    = 1;
                            int document_page_counter = 0;
                            while (sn_ != null)
                            {
                                if (pg_ > rdr_.NumberOfPages)
                                {
                                    sn_ = sn_.Next;
                                    continue;
                                }
                                PdfImportedPage   ip_ = copy.GetImportedPage(rdr_, pg_++);
                                PdfCopy.PageStamp ps_ = copy.CreatePageStamp(ip_);
                                PdfContentByte    cb_ = ps_.GetOverContent();
                                cb_.Rectangle(sheet_number_blockhout_);
                                cb_.Rectangle(revisions_blockout_);
                                cb_.Rectangle(item_descr_blockout_);

                                Font  sheet_number_font_ = FontFactory.GetFont(@"Arial", 31);
                                Chunk sheet_number_      = new Chunk(sn_.Value.ToString(), sheet_number_font_);
                                sheet_number_.SetBackground(BaseColor.WHITE);
                                ColumnText.ShowTextAligned(cb_,
                                                           Element.ALIGN_CENTER,
                                                           new Phrase(sheet_number_),
                                                           1127, 60,
                                                           0);

                                if (nd_.Value.VendorInfo)
                                {
                                    Font   item_descr_font_ = FontFactory.GetFont(@"Tw Cen MT", 17, Font.BOLD);
                                    string desc_            = @"VENDOR INFO";
                                    Chunk  item_descr_      = new Chunk(desc_, item_descr_font_);
                                    sheet_number_.SetBackground(BaseColor.WHITE);
                                    ColumnText.ShowTextAligned(cb_,
                                                               Element.ALIGN_CENTER,
                                                               new Phrase(item_descr_),
                                                               700, 60,
                                                               0);
                                }
                                else
                                {
                                    Font   item_font_ = FontFactory.GetFont(@"Tw Cen MT", 17, Font.BOLD);
                                    string item_name_ = string.Format("{0}", nd_.Value.Name);
                                    Chunk  item_      = new Chunk(item_name_, item_font_);
                                    sheet_number_.SetBackground(BaseColor.WHITE);
                                    ColumnText item_column_ = new ColumnText(cb_);
                                    item_column_.SetSimpleColumn(item_textarea_);
                                    item_column_.SetText(new Phrase(item_));
                                    item_column_.Alignment = Element.ALIGN_CENTER;
                                    item_column_.Go();

                                    Font   item_descr_font_ = FontFactory.GetFont(@"Tw Cen MT", 12, Font.BOLD);
                                    string desc_            = string.Format(@"{0}", descr_.Value);
                                    if (document_page_counter++ > 0 && rdr_.NumberOfPages > 1)
                                    {
                                        desc_ = string.Format(@"SEE SHEET {0}", nd_.Value.FirstSheetNo().Value);
                                    }
                                    Chunk      item_descr_        = new Chunk(desc_, item_descr_font_);
                                    Phrase     ph_                = new Phrase(item_descr_);
                                    ColumnText item_descr_column_ = new ColumnText(cb_);
                                    item_descr_column_.SetSimpleColumn(item_descr_textarea_);
                                    item_descr_column_.SetText(ph_);
                                    item_descr_column_.Alignment = Element.ALIGN_CENTER;
                                    item_descr_column_.Go();
                                }
                                ps_.AlterContents();
                                sn_    = sn_.Next;
                                descr_ = descr_.Next;
                                copy.AddPage(ip_);
                            }
                        }
                        nd_ = nd_.Next;
                    }
                    document.Close();
                    return(ms.GetBuffer());
                }
            }
        }