Read JPEG image, convert it to PDF and save to disk - Visual Basic 6

Asked

Viewed 315 times

1

I will create a button, which when clicking it, it reads a JPEG image and can convert it to PDF and save to disk.

I would like a tip in source code so I can start this project.

Thank you very much.

1 answer

2

The answer to follow comes from the source Macoratti.
I copied it in its entirety to answer the author of the question, even if the link goes out.

public void ImagensParaPDF(string ImagemCaminhoOrigem, string caminhoSaidaPDF)
{
    string[] caminhoImagens = GetImageFiles(ImagemCaminhoOrigem);

    if (caminhoImagens.Length > 0)
    {
        this.progressBar1.Minimum = 1;
        this.progressBar1.Maximum = caminhoImagens.Length;
        string pdfpath = caminhoSaidaPDF + ImagemCaminhoOrigem.Substring(ImagemCaminhoOrigem.LastIndexOf("\\")) + ".pdf";

        if (File.Exists(pdfpath))
        {
            pdfpath = SetNewName(caminhoSaidaPDF, ImagemCaminhoOrigem.Substring(ImagemCaminhoOrigem.LastIndexOf("\\") + 1));
        }

        using (var doc = new iTextSharp.text.Document())
        {
            iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
            doc.Open();

            foreach (var item in caminhoImagens)
            {
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(item);

                //image.SetAbsolutePosition(30f, 30f);
                if (this.chkScalebyImageSize.Checked)
                {
                    image.ScaleToFit(doc.PageSize.Width - 60, doc.PageSize.Height - 60);
                }
                else
                {
                    image.ScaleAbsoluteHeight(doc.PageSize.Height - 60);
                    image.ScaleAbsoluteWidth(doc.PageSize.Width - 60);
                }
                doc.Add(image);

                if (this.progressBar1.Maximum <= caminhoImagens.Length)
                    this.progressBar1.Increment(1);
            }
        }
    }
    else
    {
        MessageBox.Show("Imagem não encontrada.", "Pare!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

The above function makes use of the iTextSharp, a PDF library that allows you to create, adapt, Inspecione and keep documents in format PDF - Portable Document Format.
Some sources:


In this post (by Stackoverflow himself) also addresses this same need.

Browser other questions tagged

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