How to merge pdf and and .jpg file in new pdf

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to merge pdf and and .jpg file in new pdf

momer79
This post was updated on .
There are two files on disk .jpg and .pdf, i need to read both files and add them to new pdf and send to browser so that it can be downloaded.

New pdf file only contains pdf contents not jpeg file image.

               memoryStream myMemoryStream = new MemoryStream();

            //----pdf file--------------

            iTextSharp.text.pdf.PdfCopy writer2 = new iTextSharp.text.pdf.PdfCopy(doc, myMemoryStream);

            doc.Open();

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(imagepath + "/30244.pdf");
            reader.ConsolidateNamedDestinations();

            for (int i = 1; i <= reader.NumberOfPages; i++) {
                    iTextSharp.text.pdf.PdfImportedPage page = writer2.GetImportedPage(reader, i);
                    writer2.AddPage(page);
            }

            iTextSharp.text.pdf.PRAcroForm form = reader.AcroForm;
            if (form != null) {
                    writer2.CopyAcroForm(reader);
            }

            //-----------------jpeg file-------------------------------------
            MemoryStream myMemoryStream2 = new MemoryStream();
            System.Drawing.Image image = System.Drawing.Image.FromFile(imagepath + "/Vouchers.jpg");
            iTextSharp.text.Document doc2 = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
            iTextSharp.text.pdf.PdfWriter.GetInstance(doc2, myMemoryStream2);
            doc2.Open();
            iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] content = myMemoryStream.ToArray;

            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=LeftCorner568.pdf");
            Response.BinaryWrite(content);