How to convert multiple images. jpgs to a single pdf file in c#

Asked

Viewed 328 times

2

What I have is a directory with for example 10 scanned images.I would like to read these images in this directory and convert them into a single file .PDF.I know that there are Dlls in the market that do this but I would like to do the routine myself. Would it be possible?

  • If you don’t want to use a ready library, you can always look at the PDF specification and create PDF based on your images. PDF format is not too complicated, and the specification is quite complete.

1 answer

2

Because you haven’t done anything, I’ll give you what I have here. Maybe it’s not 100% what you want, but it will give you a way. A basic idea, maybe you have to readjust the image.

//Cria um novo documento
iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.LETTER, 20, 20, 20, 20);
//Salve o documento
string PDFOutput = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream(PDFOutput, FileMode.Create, FileAccess.Write, FileShare.Read));

//Abra o PDf
Doc.Open();

string Folder = "C:\\Images";
foreach (string F in System.IO.Directory.GetFiles(Folder, "*.jpg")) {
    // Inserir uma pagina
    Doc.NewPage();
    //Adicionar uma Imagem
    Doc.Add(new iTextSharp.text.Jpeg(new Uri(new FileInfo(F).FullName)));
}

//Fechar pdf
Doc.Close();
  • Perfect William, that’s what I wanted, I’ve already implemented a test and it came true"Thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.