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

1

I'll create a button, which when I click it, it reads a JPEG image and can convert it to PDF and save to disk.

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

Thank you very much.

    
asked by anonymous 13.03.2015 / 20:58

1 answer

2

The following answer comes from the source Macoratti . I have copied in full to answer the author's question, inclusive, 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 iTextSharp , a PDF library that allows you to create, adapt , inspect and maintain documents in the PDF - Portable Document Format format. Some fonts :

In this post (from Stackoverflow itself) also addresses this same need.

    
08.12.2016 / 16:28