1

I need a method or library that convert a image jpg to file pdf. I tried find in web, but I did find only the SautinSoft, but this is to pdf to jpg and is not free. Can someone help me?

This function is for C# 3.5.

leppie
  • 115,091
  • 17
  • 196
  • 297
Diego Moreno
  • 131
  • 1
  • 6

1 Answers1

4

What you need is a library to create PDFs, like http://www.pdfsharp.net/

Then you can create a pdf document and inject your image into it. You're not looking to convert a JPG to a PDF, that doesn't make much sense, you're trying to create a PDF with your JPG inside it.

Sample code from another answer: Overlay image onto PDF using PDFSharp

private void GeneratePDF(string filename, string imageLoc)
{
    PdfDocument document = new PdfDocument();

    // Create an empty page or load existing
    PdfPage page = document.AddPage();

    // Get an XGraphics object for drawing
    XGraphics gfx = XGraphics.FromPdfPage(page);
    DrawImage(gfx, imageLoc, 50, 50, 250, 250);

    // Save and start View
    document.Save(filename);
    Process.Start(filename);
}

void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
    XImage image = XImage.FromFile(jpegSamplePath);
    gfx.DrawImage(image, x, y, width, height);
}
Community
  • 1
  • 1