Set Image size before adding to PDF document using iTextSharp in ASP.Net

Last Reply 5 days ago By dharmendr

Posted 5 days ago

 Please help after converting multiple images into a single pdf file. so images appear to be cutoff especially  from the right side . yet if you look at the original image it is clear. how can i make it resized properly with loosing it part of it thanks

Protected Sub OnMerge(ByVal sender As Object, ByVal e As EventArgs)
    If fuUpload.HasFiles Then
        Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
        Using memoryStream As MemoryStream = New MemoryStream()
            Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
            pdfDoc.Open()
            For Each file As HttpPostedFile In fuUpload.PostedFiles
                Dim img As Image = Image.GetInstance(file.InputStream)
                pdfDoc.Add(img)
            Next

            pdfDoc.Close()
            Dim bytes As Byte() = memoryStream.ToArray()
            memoryStream.Close()
            Dim mm As MailMessage = New MailMessage("sender@gmail.com", "reciever@gmail.com")
            mm.Subject = "Merged PDF"
            mm.Body = "Merged PDF Attachment"
            mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "Merged.pdf"))
            mm.IsBodyHtml = True
            Dim smtp As SmtpClient = New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
            Dim NetworkCred As NetworkCredential = New NetworkCredential()
            NetworkCred.UserName = "sender@gmail.com"
            NetworkCred.Password = "password"
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
        End Using
    End If
End Sub

 

 <asp:FileUpload runat="server" ID="fuUpload" AllowMultiple="true" />
<asp:Button Text="Upload Merge" runat="server" OnClick="OnMerge" />
    </div>
    </form>
</body>
</html>

 

You are viewing reply posted by: dharmendr 5 days ago.
Posted 5 days ago

Hi makumbi,

Set the iTextSharp Image ScaleAbsolute property which accepts Width and Height.

Code

C#

protected void OnMerge(object sender, EventArgs e)
{
    if (fuUpload.HasFiles)
    {
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();
            foreach (HttpPostedFile file in fuUpload.PostedFiles)
            {
                Image img = Image.GetInstance(file.InputStream);
                // Set Image Width and Height.
                img.ScaleAbsolute(500f, img.Height); 
                pdfDoc.Add(img);
            }
            pdfDoc.Close();
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();

            MailMessage mm = new MailMessage("sender@gmail.com", "reciever@gmail.com");
            mm.Subject = "Merged PDF";
            mm.Body = "Merged PDF Attachment";
            mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Merged.pdf"));
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "sender@gmail.com";
            NetworkCred.Password = "password";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }
}

VB.Net

Protected Sub OnMerge(ByVal sender As Object, ByVal e As EventArgs)
    If fuUpload.HasFiles Then
        Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
        Using memoryStream As MemoryStream = New MemoryStream()
            Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
            pdfDoc.Open()
            For Each file As HttpPostedFile In fuUpload.PostedFiles
                Dim img As Image = Image.GetInstance(file.InputStream)
                ' Set Image Width and Height.
                img.ScaleAbsolute(500.0F, img.Height)
                pdfDoc.Add(img)
            Next

            pdfDoc.Close()
            Dim bytes As Byte() = memoryStream.ToArray()
            memoryStream.Close()
            Dim mm As MailMessage = New MailMessage("sender@gmail.com", "reciever@gmail.com")
            mm.Subject = "Merged PDF"
            mm.Body = "Merged PDF Attachment"
            mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "Merged.pdf"))
            mm.IsBodyHtml = True
            Dim smtp As SmtpClient = New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
            Dim NetworkCred As NetworkCredential = New NetworkCredential()
            NetworkCred.UserName = "sender@gmail.com"
            NetworkCred.Password = "password"
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
        End Using
    End If
End Sub