Skip to content

Instantly share code, notes, and snippets.

@gythialy
Created January 19, 2014 04:03
Show Gist options
  • Save gythialy/8500364 to your computer and use it in GitHub Desktop.
Save gythialy/8500364 to your computer and use it in GitHub Desktop.
convert jpg to pdf by c#
void ConvertJPG2PDF(string jpgfile, string pdf)
{
var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = Image.GetInstance(imageStream);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.Add(image);
}
document.Close();
}
}
@tusharuit25
Copy link

tusharuit25 commented Feb 23, 2021

How to add text below this added image? Solution is below code

@tusharuit25
Copy link

void ConvertJPG2PDF(string jpgfile, string pdf,string text)
        {
            var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
            PdfWriter pdfWriter;
            using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //QR Code

                pdfWriter = PdfWriter.GetInstance(document, stream);
                document.Open();
                iTextSharp.text.Image image;
                using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    image = iTextSharp.text.Image.GetInstance(imageStream);
                    if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                    document.Add(image);
                    
                }

                //Adding text
                var textY = ((PageSize.A4.Height - image.ScaledHeight) / 2) - 5;
                var textX = PageSize.A4.Width / 2;
                var chunk = new Chunk(text, FontFactory.GetFont("Helvetica", 22.0f, BaseColor.BLACK));
                PdfContentByte cb = pdfWriter.DirectContent;
                cb.BeginText();
                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(chunk), textX, textY, 0);
                cb.EndText();

                document.Close();
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment