public static byte[] ConvertToPdf(Image iTextImage, PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions = null)
        {
            var pdfOptions = scalingOptions ?? PdfScalingOptions.Default;

            //BBernard
            //Statically ensure that Compression is enabled...
            Document.Compress = pdfOptions.EnableCompression;

            var marginInfo = pageSizeInfo.MarginSize;

            using (var outputMemoryStream = new MemoryStream())
                using (var pdfDocBuilder = new Document(pageSizeInfo.PageSize, marginInfo.Left, marginInfo.Right, marginInfo.Top, marginInfo.Bottom))
                    using (var pdfWriter = PdfWriter.GetInstance(pdfDocBuilder, outputMemoryStream))
                    {
                        if (pdfOptions.EnableScaling)
                        {
                            var scaledContent = PdfResizeHelper.ScalePdfContent(iTextImage, pageSizeInfo, scalingOptions);
                            pdfDocBuilder.SetPageSize(scaledContent.ScaledPageSize);

                            pdfDocBuilder.Open();
                            pdfDocBuilder.Add(scaledContent.ScaledPdfContent);
                        }
                        else
                        {
                            pdfDocBuilder.Open();
                            pdfDocBuilder.Add(iTextImage);
                        }

                        pdfDocBuilder.Close();

                        var pdfBytes = outputMemoryStream.ToArray();
                        return(pdfBytes);
                    }
        }
        public static byte[] MergePdfFiles(List <byte[]> pdfFileBytesList, PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions = null)
        {
            if (pdfFileBytesList == null || !pdfFileBytesList.Any())
            {
                throw new ArgumentNullException(nameof(pdfFileBytesList), "List of Pdf Files to be merged cannot be null or empty.");
            }
            if (pageSizeInfo == null)
            {
                throw new ArgumentNullException(nameof(pageSizeInfo), "ResizeInfo cannot be null.");
            }

            byte[] outputBytes = null;

            //NOTE: For Merging we DISABLE ALL SCALING by default, so that we can preserve Annotations, etc.
            //          but this can be easily set by the client code to enable scaling.
            var pdfScalingOptions = scalingOptions ?? PdfScalingOptions.DisableScaling;

            //Set static references for Pdf Processing...
            PdfReader.unethicalreading = pdfScalingOptions.EnableUnethicalReading;
            Document.Compress          = pdfScalingOptions.EnableCompression;

            var targetPageSize   = pageSizeInfo.PageSize;
            var targetMarginSize = pageSizeInfo.MarginSize;

            using (var outputMemoryStream = new MemoryStream())
                using (var pdfDocBuilder = new Document(targetPageSize, targetMarginSize.Left, targetMarginSize.Right, targetMarginSize.Top, targetMarginSize.Bottom))
                    using (var pdfSmartCopy = new PdfSmartCopy(pdfDocBuilder, outputMemoryStream))
                    {
                        pdfDocBuilder.Open();

                        foreach (var pdfBytes in pdfFileBytesList)
                        {
                            if (pdfScalingOptions.EnableScaling)
                            {
                                var scaledPdfBytes = PdfResizeHelper.ResizePdfPageSize(pdfBytes, pageSizeInfo, pdfScalingOptions);
                                pdfSmartCopy.AppendPdfDocument(scaledPdfBytes);
                            }
                            else
                            {
                                pdfSmartCopy.AppendPdfDocument(pdfBytes);
                            }
                        }

                        pdfDocBuilder.Close();
                        outputBytes = outputMemoryStream.ToArray();
                    }

            return(outputBytes);
        }
Beispiel #3
0
        public void PdfResizeHelperSimpleTest()
        {
            var originalPdfBytes = TestHelper.ReadTestDataFileBytes(@"TestDoc_01.pdf");

            //*************************************************
            //Setup & Execute Tests...
            //*************************************************
            //RESIZE & scale LETTER Doc down to Postcard size, and validate the last page with the SMALL image is scaled up!
            var resizeInfo   = new PdfResizeInfo(PageSize.POSTCARD, PdfMarginSize.None);
            var resizedBytes = PdfResizeHelper.ResizePdfPageSize(originalPdfBytes, resizeInfo, PdfScalingOptions.Default);

            //*************************************************
            //Validate Results...
            //*************************************************
            TestHelper.AssertThatPdfPageSizeIsAsExpected(resizedBytes, resizeInfo);

            File.WriteAllBytes($@"D:\Temp\PdfResizeHelper\RESIZED OUTPUT TEST - {Guid.NewGuid()}.pdf", resizedBytes);
        }