TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
saifullah khan
NA
335
301.8k
Pass string[] to merge images to pdf ASP.NET
Mar 23 2018 1:40 AM
I'm trying to merg images from a folder to a pdf file. here is my code:
/// <summary>
/// Takes a collection of BMP files and converts them into a PDF document
/// </summary>
/// <param name="bmpFilePaths"></param>
/// <returns></returns>
private
byte
[] CreatePdf(
string
[] bmpFilePaths)
{
using
(var ms =
new
MemoryStream())
{
var document =
new
iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER.Rotate(), 0, 0, 0, 0);
iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
document.Open();
foreach
(var path
in
bmpFilePaths)
{
var imgStream = GetImageStream(path);
var image = iTextSharp.text.Image.GetInstance(imgStream);
image.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
document.Add(image);
}
document.Close();
return
ms.ToArray();
}
}
/// <summary>
/// Gets the image at the specified path, shrinks it, converts to JPG, and returns as a stream
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
private
Stream GetImageStream(
string
imagePath)
{
var ms =
new
MemoryStream();
using
(var img = Image.FromFile(imagePath))
{
var jpegCodec = ImageCodecInfo.GetImageEncoders()
.Where(x => x.MimeType ==
"image/jpeg"
)
.FirstOrDefault();
var encoderParams =
new
EncoderParameters(1);
encoderParams.Param[0] =
new
EncoderParameter(Encoder.Quality, (
long
)20);
int
dpi = 175;
var thumb = img.GetThumbnailImage((
int
)(11 * dpi), (
int
)(8.5 * dpi),
null
, IntPtr.Zero);
thumb.Save(ms, jpegCodec, encoderParams);
}
ms.Seek(0, SeekOrigin.Begin);
return
ms;
}
The images are in a folder. can somebody please tell me how to pass string[] with the list of images links to CreatePdf(string[] bmpFilesPath) from a button click function.
Reply
Answers (
1
)
How to upload image using webapi with class
video in asp.net