public static byte[] concatAndAddContent(List <byte[]> pdfByteContent)
        {
            using (var ms = new MemoryStream())
            {
                using (var doc = new Document())
                {
                    using (var copy = new PdfSmartCopy(doc, ms))
                    {
                        doc.Open();

                        //Loop through each byte array
                        foreach (var p in pdfByteContent)
                        {
                            //Create a PdfReader bound to that byte array
                            using (var reader = new PdfReader(p))
                            {
                                //Add the entire document instead of page-by-page
                                copy.AddDocument(reader);
                            }
                        }

                        doc.Close();
                    }
                }

                //Return just before disposing
                return(ms.ToArray());
            }
        }
Exemplo n.º 2
0
        public void Merge(List <string> inputfiles)
        {
            inputfiles.Sort();
            var outputpath = FileUtilities.GetOutputPath(inputfiles.First(), Libraries.CommonUtilities.Models.ActionType.MERGE);

            Document     doc = null;
            PdfSmartCopy pdf = null;

            try
            {
                var stream = new FileStream(outputpath, FileMode.Create);

                doc = new Document();
                pdf = new PdfSmartCopy(doc, stream);

                doc.Open();

                foreach (string file in inputfiles)
                {
                    pdf.AddDocument(new PdfReader(file));
                }

                Console.WriteLine("Merged {0} into {1}", inputfiles.Count, Path.GetFileName(outputpath));
            }
            catch (Exception)
            {
            }
            finally
            {
                pdf?.Dispose();
                doc?.Dispose();
            }
        }
Exemplo n.º 3
0
        public static void Merge(List <string> filesContents, string filePath)
        {
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                using (var document = new Document(PageSize.A4))
                {
                    using (var copyWriter = new PdfSmartCopy(document, fileStream))
                    {
                        document.Open();
                        copyWriter.RegisterFonts();

                        foreach (var fileContent in filesContents)
                        {
                            using (var pdfReader = new PdfReader(fileContent))
                            {
                                copyWriter.AddDocument(pdfReader);
                            }
                        }

                        document.Close();
                        copyWriter.RegisterProperties();
                    }
                }
            }

            AddPdfAFlag(filePath);
        }
Exemplo n.º 4
0
        public void concatAndAddContent(List <string> pdfsFile, string sOutputFile)
        {
            byte[] result;

            using (var ms = new MemoryStream())
            {
                using (var doc = new Document())
                {
                    using (var copy = new PdfSmartCopy(doc, ms))
                    {
                        doc.Open();

                        foreach (var item in pdfsFile)
                        {
                            using (var reader = new PdfReader(item))
                            {
                                copy.AddDocument(reader);
                            }
                        }

                        doc.Close();
                    }
                }

                result = ms.ToArray();
            }

            using (var fs = new FileStream(sOutputFile, FileMode.Create, FileAccess.Write))
            {
                fs.Write(result, 0, result.Length);
            }
        }
Exemplo n.º 5
0
        public static byte[] Merge(List <byte[]> filesContents)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var document = new Document(PageSize.A4))
                {
                    using (var copyWriter = new PdfSmartCopy(document, memoryStream))
                    {
                        document.Open();
                        copyWriter.RegisterFonts();

                        foreach (var fileContent in filesContents)
                        {
                            using (var pdfReader = new PdfReader(fileContent))
                            {
                                copyWriter.AddDocument(pdfReader);
                            }
                        }

                        document.Close();
                        copyWriter.RegisterProperties();
                    }
                }

                return(AddPdfAFlag(memoryStream.ToArray()));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Merges the PDFS.
        /// </summary>
        /// <param name="pdfsToMerge">The PDFS to merge.</param>
        /// <returns>The merged PDF's resulting bytes.</returns>
        public byte[] MergePdfs(IEnumerable <byte[]> pdfsToMerge)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (Document doc = new Document())
                {
                    using (PdfSmartCopy copy = new PdfSmartCopy(doc, ms))
                    {
                        doc.Open();

                        // Loop through each byte array
                        foreach (byte[] p in pdfsToMerge)
                        {
                            // Create a PdfReader bound to that byte array
                            using (PdfReader reader = new PdfReader(p))
                            {
                                // Add the entire document instead of page-by-page
                                copy.AddDocument(reader);
                            }
                        }

                        doc.Close();
                    }
                }

                // Return just before disposing
                return(ms.ToArray());
            }
        }
Exemplo n.º 7
0
        public static void appendSignaturePage(PdfSmartCopy copy, byte[] signaturePage, byte[] signature, String signingOfficerName, String signingOfficerTitle)
        {
            using (var second_ms = new MemoryStream())
            {
                PdfReader      pdfr  = new PdfReader(signaturePage);
                PdfStamper     pdfs  = new PdfStamper(pdfr, second_ms);
                Image          image = iTextSharp.text.Image.GetInstance(signature);
                Rectangle      rect;
                PdfContentByte content;

                rect    = pdfr.GetPageSize(1);
                content = pdfs.GetOverContent(1);

                image.SetAbsolutePosition(84.0F, 475.0F);
                image.ScalePercent(29.0F, 25.0F);

                content.AddImage(image);

                PdfLayer layer = new PdfLayer("info-layer", pdfs.Writer);
                content.BeginLayer(layer);
                content.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 20);

                String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

                DateTime today       = DateTime.Now;
                String   monthString = months[today.Month - 1];
                String   yearString  = today.Year.ToString().Substring(2);
                var      now         = DateTime.Now;
                String   daySuffix   = (now.Day % 10 == 1 && now.Day != 11) ? "st"
                : (now.Day % 10 == 2 && now.Day != 12) ? "nd"
                : (now.Day % 10 == 3 && now.Day != 13) ? "rd"
                : "th";
                String dayString = today.Day.ToString() + daySuffix;


                content.SetColorFill(BaseColor.BLACK);
                content.BeginText();
                content.SetFontAndSize(BaseFont.CreateFont(), 9);
                content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, signingOfficerName, 84.0F, 420.0F, 0.0F);
                content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, signingOfficerTitle, 84.0F, 370.0F, 0.0F);
                content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, dayString, 152.0F, 624.0F, 0.0F);
                content.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, monthString, 285.0F, 624.0F, 0.0F);
                content.ShowTextAligned(PdfContentByte.ALIGN_LEFT, yearString, 304.0F, 624.5F, 0.0F);
                content.EndText();

                content.EndLayer();

                pdfs.Close();

                using (var reader = new PdfReader(second_ms.ToArray()))
                {
                    //Add the entire document instead of page-by-page
                    copy.AddDocument(reader);
                }
            }
        }
Exemplo n.º 8
0
        public static byte[] concatAndAddContent(List <byte[]> pdfByteContent, byte[] signaturePage, int signaturePosition, byte[] signature, String signingOfficerName, String signingOfficerTitle)
        {
            using (var ms = new MemoryStream())
            {
                using (var doc = new Document())
                {
                    using (var copy = new PdfSmartCopy(doc, ms))
                    {
                        doc.Open();
                        int  index          = 0;
                        bool addedSignature = false;

                        //Loop through each byte array
                        foreach (var p in pdfByteContent)
                        {
                            //Create a PdfReader bound to that byte array
                            using (var reader = new PdfReader(p))
                            {
                                //Add the entire document instead of page-by-page
                                copy.AddDocument(reader);
                            }

                            if (index == signaturePosition && !addedSignature)
                            {
                                appendSignaturePage(copy, signaturePage, signature, signingOfficerName, signingOfficerTitle);
                                addedSignature = true;
                            }

                            ++index;
                        }

                        if (!addedSignature)
                        {
                            appendSignaturePage(copy, signaturePage, signature, signingOfficerName, signingOfficerTitle);
                            addedSignature = true;
                        }

                        doc.Close();
                    }
                }

                //Return just before disposing
                return(ms.ToArray());
            }
        }
Exemplo n.º 9
0
 public byte[] MergePDF(IEnumerable <byte[]> contents)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         using (Document doc = new Document())
             using (PdfSmartCopy copy = new PdfSmartCopy(doc, ms))
             {
                 doc.Open();
                 foreach (byte[] p in contents)
                 {
                     using (PdfReader reader = new PdfReader(p))
                     {
                         copy.AddDocument(reader);
                     }
                 }
                 doc.Close();
             }
         return(ms.ToArray());
     }
 }
Exemplo n.º 10
0
        public static byte[] CombineMultipleByteArrays(List <byte[]> lstByteArray)
        {
            using (var ms = new MemoryStream())
            {
                using (var doc = new iTextSharp.text.Document())
                {
                    using (var copy = new PdfSmartCopy(doc, ms))
                    {
                        doc.Open();
                        foreach (var p in lstByteArray)
                        {
                            using (var reader = new PdfReader(p))
                            {
                                copy.AddDocument(reader);
                            }
                        }

                        doc.Close();
                    }
                }
                return(ms.ToArray());
            }
        }
Exemplo n.º 11
0
        public override Object Merge(int companyId, object pdfFiles, string blobPath)
        {
            string tempUploadPath = HttpContext.Current.Server.MapPath("~/App_data/uploads/" + Path.GetFileName(blobPath));

            using (FileStream stream = new FileStream(tempUploadPath, FileMode.Create))
            {
                PdfReader     reader         = null;
                Document      sourceDocument = new Document();
                List <string> lstfiles       = pdfFiles as List <string>;
                util.ContainerName = "company-" + companyId;
                Cloudblob          = util.BlobContainer.GetBlockBlobReference(blobPath);

                PdfWriter    writer = PdfWriter.GetInstance(sourceDocument, stream);
                PdfSmartCopy copy   = new PdfSmartCopy(sourceDocument, stream);
                sourceDocument.Open();

                lstfiles.ForEach(file =>
                {
                    util.ContainerName    = "company-" + companyId;
                    string path           = util.getBlob(file);
                    CloudBlockBlob _cblob = util.BlobContainer.GetBlockBlobReference(path);
                    var ms = new MemoryStream();
                    _cblob.DownloadToStream(ms);
                    long fileByteLength = _cblob.Properties.Length;
                    byte[] fileContents = new byte[fileByteLength];
                    _cblob.DownloadToByteArray(fileContents, 0);
                    reader = new PdfReader(fileContents);
                    copy.AddDocument(reader);
                    reader.Close();
                }
                                 );
                copy.Close();
            }
            var blobURL = this.Upload(blobPath, tempUploadPath, companyId);

            return((object)blobURL);
        }
Exemplo n.º 12
0
        // method to merge List<binaries> into one binary (bianires class)
        private binaries mergeDoc(List <binaries> SignedBinaries, string mergedFileName)
        {
            MemoryStream memoStream = new MemoryStream();

            iTextSharp.text.Document doc  = new iTextSharp.text.Document();
            PdfSmartCopy             copy = new PdfSmartCopy(doc, memoStream);

            doc.Open();

            List <byte[]> ListOfPDFS = SignedBinaries.Select(x => x.ficheirPDF).ToList();

            //Loop through each byte array (each iteration represent a single PDF)
            foreach (byte[] pdf in ListOfPDFS)
            {
                PdfReader pdfReader = new PdfReader(pdf);
                copy.AddDocument(pdfReader);
            }
            doc.Close();

            return(new binaries {
                ficheirPDF = memoStream.ToArray(),
                nomFichie = mergedFileName
            });
        }
Exemplo n.º 13
0
        private static void GenerateReportPdf(Report report, string outputDirectory)
        {
            var path = $"{outputDirectory}\\{report.Id} Receipts.pdf";

            try
            {
                using (var outputStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (var doc = new Document())
                    {
                        using (var merge = new PdfSmartCopy(doc, outputStream))
                        {
                            doc.Open();
                            foreach (var entry in report.Entries)
                            {
                                try
                                {
                                    if (!string.IsNullOrEmpty(entry.Path))                                     //If we have already set a path for the entry, just pull that file
                                    {
                                        var bytes = File.ReadAllBytes(entry.Path);
                                        if (bytes.Length == 0)
                                        {
                                            throw new Exception($"File Was Empty: {entry.Path}");
                                        }
                                        merge.AddDocument(new PdfReader(bytes));
                                    }
                                    else if (entry.Type == ReportEntryType.Concur)                                    //If we don't have a path and we are a concur receipt, process that image
                                    {
                                        if (entry.Image == null)
                                        {
                                            throw new Exception("No Image Found for this Entry.");
                                        }

                                        var receipt = entry.Image;
                                        ConcurClient.DownloadImage(receipt);
                                        switch (receipt.ContentType)
                                        {
                                        case "pdf":
                                            merge.AddDocument(new PdfReader(receipt.Data));
                                            break;

                                        case "image":
                                        default:
                                            using (var output = new MemoryStream())
                                            {
                                                using (var document = new Document())
                                                {
                                                    PdfWriter.GetInstance(document, output);
                                                    document.Open();

                                                    var image = Image.GetInstance(receipt.Data);
                                                    image.ScaleToFit(document.PageSize);
                                                    document.Add(image);
                                                    document.NewPage();
                                                }
                                                var bytes = output.ToArray();
                                                merge.AddDocument(new PdfReader(bytes));
                                            }
                                            break;
                                        }
                                    }
                                    else                                     //If we don't have a path, and we aren't a concur document, wtf...
                                    {
                                        throw new Exception($"No File for: {entry.Path}");
                                    }
                                }
                                catch (Exception e)
                                {
                                    report.HasError = true;
                                    ErrorLogger.LogError(entry.Key, e.Message);
                                }
                            }

                            if (merge.CurrentPageNumber <= 1)
                            {
                                merge.PageEmpty = true;
                                merge.NewPage();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                report.HasError = true;
                ErrorLogger.LogError(report.Id, e.Message);
            }

            if (report.HasError)
            {
                //Lets get rid of our problem child
                File.Delete(path);
            }
        }
Exemplo n.º 14
0
        public ServiceResult CreateMultiPagePDFFile(List <Dictionary <string, string> > pdfDataLst, string pdfTemplate, bool lockForm)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug("Start");
            }

            ServiceResult wsResult = new ServiceResult();

            try
            {
                if (pdfDataLst == null || pdfDataLst.Count == 0)
                {
                    wsResult.KodOdpovede = 1;
                    wsResult.StatusText  = "Nevyplnený vstupný parameter pdfData";

                    if (log.IsDebugEnabled)
                    {
                        log.Debug(wsResult.StatusText);
                    }

                    return(wsResult);
                }

                if (string.IsNullOrWhiteSpace(pdfTemplate))
                {
                    wsResult.KodOdpovede = 1;
                    wsResult.StatusText  = "Nevyplnený vstupný parameter pdfTemplate";

                    if (log.IsDebugEnabled)
                    {
                        log.Debug(wsResult.StatusText);
                    }

                    return(wsResult);
                }

                pdfTemplate = HostingEnvironment.MapPath(pdfTemplate);

                using (Document document = new Document())
                {
                    using (MemoryStream msDoc = new MemoryStream())
                    {
                        PdfCopy copy = new PdfSmartCopy(document, msDoc);
                        document.Open();

                        foreach (Dictionary <string, string> pdfData in pdfDataLst)
                        {
                            using (PdfReader pdfReaderDoc = new PdfReader(CreatePdf(pdfData, pdfTemplate, lockForm)))
                            {
                                copy.AddDocument(pdfReaderDoc);
                                pdfReaderDoc.Close();
                            }
                        }

                        document.Close();
                        wsResult.FileData = msDoc.ToArray();
                    }
                }

                if (log.IsDebugEnabled)
                {
                    log.Debug("Stop");
                }

                return(wsResult);
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("Interna chyba: ", ex);
                }

                wsResult.KodOdpovede = -1;
                wsResult.StatusText  = string.Concat("Chyba pri spracovaní dokumentu: ", ex.Message);
                return(wsResult);
            }
        }