Conversion from jpg to Pdf

@Allan_Zimmermann Can we convert jpg to pdf in openRpa and how many ways to do it.
I am stuck here and i don’t find any way to complete this task.

@Vivek_Sharma

You can use “Invoke Code” in OpenRPA and can use a python script to convert the file.
You can use the Python Imaging Library (PIL) to convert a JPG image to PDF format. Here’s an example code snippet:

from PIL import Image

img = Image.open('image.jpg')
pdf_path = 'image.pdf'

img.save(pdf_path, 'PDF', resolution=100.0)

You can put the “Invoke Code” inside a loop where you can pass a variable with path and filename to it… them save it in a folder.

Note that:

  1. You can also specify the resolution of the output PDF file (100 dpi in this example) using the resolution parameter. This parameter is optional and can be omitted if you don’t need to specify a specific resolution.

  2. Make sure to install the required libraries by running pip install Pillow in your command prompt or terminal before running the code above.

  3. Don’t forget to install “Pillow” it if needed.

I hope this helpes you… if you need any help just let me know.

1 Like

@kowts thanks for your help. But if i want to do it through vb.net or c# then how it is possible . Do you have any idea ?

Hi @Vivek_Sharma Sorry for the delay in responding to your question.
I think there is a way but I never use it (prefer python :smiley:), If you want to convert JPG to PDF using VB.NET or C#, you can utilize libraries like iTextSharp or PDFSharp.

I never tested them but hopefully they can point you to the right path.
First install the libraries via Nuget in OpenRPA Package Manager and add it in the “imports” panel.

Here are examples using both libraries:

using iTextSharp.text;
using iTextSharp.text.pdf;

// Create a Document object
Document document = new Document();

// Create a PdfWriter instance to write the document to a PDF file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create));

// Open the document
document.Open();

// Load the JPG image
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("input.jpg");

// Set the image position and size on the page
image.SetAbsolutePosition(0, 0);
image.ScaleToFit(document.PageSize.Width, document.PageSize.Height);

// Add the image to the document
document.Add(image);

// Close the document
document.Close();

// Dispose of the writer
writer.Dispose();

using PdfSharp.Drawing;
using PdfSharp.Pdf;

// Create a new PDF document
PdfDocument document = new PdfDocument();

// Add a new page to the document
PdfPage page = document.AddPage();

// Create an XGraphics object for drawing on the page
XGraphics gfx = XGraphics.FromPdfPage(page);

// Load the JPG image
XImage image = XImage.FromFile("input.jpg");

// Draw the image on the page
gfx.DrawImage(image, 0, 0, page.Width, page.Height);

// Save the document to a PDF file
document.Save("output.pdf");

// Close the document
document.Close();

2 Likes

Wow, i love these responses. Thank you :blush:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.