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); }
public static byte[] MergePdfFiles(PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions, params byte[][] pdfFileParams) { var pdfFileList = pdfFileParams.ToList(); return(MergePdfFiles(pdfFileList, pageSizeInfo, scalingOptions)); }
public static byte[] ConvertImageToPdf(string filePath, PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions = null) { var fileInfo = new FileInfo(filePath); if (!fileInfo.Exists) { throw new FileNotFoundException($"File specified could not be found [{filePath}]."); } var pdfBytes = ConvertImageToPdf(fileInfo, pageSizeInfo, scalingOptions); return(pdfBytes); }
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[] ConvertImageToPdf(byte[] imageBytes, PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions = null) { var iTextImage = Image.GetInstance(imageBytes); return(ConvertToPdf(iTextImage, pageSizeInfo, scalingOptions)); }
public static byte[] ConvertImageToPdf(FileInfo fileInfo, PdfResizeInfo pageSizeInfo, PdfScalingOptions scalingOptions = null) { var imageBytes = File.ReadAllBytes(fileInfo.FullName); var pdfBytes = ConvertImageToPdf(imageBytes, pageSizeInfo, scalingOptions); return(pdfBytes); }