How to Convert Image to Pdf

JPG to PDF Convertor in C#

iTextSharp does it pretty cleanly and is open source. Also, it has a very good accompanying book by the author which I recommend if you end up doing more interesting things like managing forms. For normal usage, there are plenty resources on mailing lists and newsgroups for samples of how to do common things.

EDIT: as alluded to in @Chirag's comment, @Darin's answer has code that definitely compiles with current versions.

Example usage:

public static void ImagesToPdf(string[] imagepaths, string pdfpath)
{
using(var doc = new iTextSharp.text.Document())
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
doc.Open();
foreach (var item in imagepaths)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(item);
doc.Add(image);
}
}
}

Convert image to pdf fail by unknow file extension

Another yet way to do things that makes use the pathlib module which allows file and folder paths to be tried as objects and manipulated via an object-oriented programming paradigm or model — which often results in very concise code:

from pathlib import Path
from PIL import Image

path = Path('./images')
exts = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff'}

for path_object in path.glob('**/*'):
if path_object.is_file() and path_object.suffix in exts:
img = Image.open(path_object)
img = img.convert('RGB')
img.save(path_object.parent / (path_object.stem+'.pdf'))

C# - How to convert an image to a PDF (using a free library)

I've come up with a way to do this using PDFSharp, hopefully will be useful for others as well.

        // Convert to PDF and delete image
PdfHelper.Instance.SaveImageAsPdf($"{fileName}.png", $"{fileName}.pdf", 1000, true);

The new class:

using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

public sealed class PdfHelper
{
private PdfHelper()
{
}

public static PdfHelper Instance { get; } = new PdfHelper();

internal void SaveImageAsPdf(string imageFileName, string pdfFileName, int width = 600, bool deleteImage = false)
{
using (var document = new PdfDocument())
{
PdfPage page = document.AddPage();
using (XImage img = XImage.FromFile(imageFileName))
{
// Calculate new height to keep image ratio
var height = (int)(((double)width / (double)img.PixelWidth) * img.PixelHeight);

// Change PDF Page size to match image
page.Width = width;
page.Height = height;

XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(img, 0, 0, width, height);
}
document.Save(pdfFileName);
}

if (deleteImage)
File.Delete(imageFileName);
}
}

How to convert Image to PDF?

I would suggest you to use iText pdf library. Here is the gradle dependency:

implementation 'com.itextpdf:itextg:5.5.10'

 Document document = new Document();

String directoryPath = android.os.Environment.getExternalStorageDirectory().toString();

PdfWriter.getInstance(document, new FileOutputStream(directoryPath + "/example.pdf")); // Change pdf's name.

document.open();

Image image = Image.getInstance(directoryPath + "/" + "example.jpg"); // Change image's name and extension.

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / image.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
image.scalePercent(scaler);
image.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP);

document.add(image);
document.close();

Convert image to pdf with Imagemagick keeping image resolution and placing it on top left corner

I found a solution of this problem.
Here the bash code:

#!/bin/bash

if [ -z "$1" ] || [ -z "$2" ]
then
echo "Usage: $0 <input> <output>"
exit 1;
fi

max () {
echo $(( $1 > $2 ? $1 : $2 ))
}

IMAGE_X=$(identify -format "%w" "$1");
IMAGE_Y=$(identify -format "%h" "$1");

IMAGE_DENSITY_X=$(( $IMAGE_X * 100 / 827 ))
IMAGE_DENSITY_Y=$(( $IMAGE_Y * 100 / 1169 ))
IMAGE_DENSITY=$(max $IMAGE_DENSITY_X $IMAGE_DENSITY_Y)

echo "Image density: $IMAGE_DENSITY"

PAGE_SIZE_X=$(( $IMAGE_DENSITY * 827 / 100 ))
PAGE_SIZE_Y=$(( $IMAGE_DENSITY * 1169 / 100 ))

echo "Page size X: $PAGE_SIZE_X"
echo "Page size Y: $PAGE_SIZE_Y"

OFFSET_X=$(( ($PAGE_SIZE_X - $IMAGE_X) / 2 * 72 / $IMAGE_DENSITY ))
OFFSET_Y=$(( ($PAGE_SIZE_Y - $IMAGE_Y) / 2 * 72 / $IMAGE_DENSITY ))

echo "$IMAGE_DENSITY $PAGE_SIZE_X $OFFSET_X $OFFSET_Y"

convert "$1" \
-page ${PAGE_SIZE_X}x${PAGE_SIZE_Y}+${OFFSET_X}+${OFFSET_Y} \
-units PixelsPerInch \
-density $IMAGE_DENSITY \
-format pdf \
"$2"


Related Topics



Leave a reply



Submit