Esempio n. 1
0
        /// <summary>
        ///   Fills the given pdf bytes with the given object.
        /// </summary>
        /// <param name="obj">The object to use.</param>
        /// <param name="pdfToFillBytes">The pdf to fill bytes.</param>
        /// <param name="setReadOnly">A value indicating whether to set the pdf read only or not.</param>
        /// <returns>The filled pdf.</returns>
        public static byte[] ToPdf(this object obj, byte[] pdfToFillBytes, bool setReadOnly = true)
        {
            Guard.AgainstNull(obj, "The object cannot be null.");
            Guard.AgainstEmpty(pdfToFillBytes, "Pdf to fill bytes cannot be null or empty.");

            using (var sourcePdf = new PdfReader(pdfToFillBytes))
                using (var destinationStream = new MemoryStream())
                    using (var destinationPdf = new PdfStamper(sourcePdf, destinationStream))
                    {
                        destinationPdf.SetFields(obj);

                        if (setReadOnly)
                        {
                            destinationPdf.SetReadOnly();
                        }

                        return(destinationStream.ToArray());
                    }
        }