locked
How to use iTextSharp add an image to exist PDF and not replace original content? RRS feed

  • Question

  • I want to add a new image to exist PDF, and not new PDF.

    I try to use iTextSharp.dll, and I found it was create new PDF and add image, but I want to add image to exist PDF and not create new PDF.

    and I had some code, you can refer to:

    string imagePath = "image";

    PdfReader reader = new PdfReader(@"C:\Users\Desktop\PDF.pdf");

    int n = reader.NumberOfPages; 

    iTextSharp.text.Rectangle psize = reader.GetPageSize(1);    

    float width = psize.Width;            

    float height = psize.Height;                        

    Document document = new Document(psize, 50, 50, 50, 50); 

    PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(filename, FileMode.Append));            

    document.Open();            

    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);      

    img.SetAbsolutePosition(440, 800);            

    writer.DirectContent.AddImage(img);      

    document.Close();

    If you have some way to realize the goal, can you help me resolve this question?

    Thursday, March 26, 2015 4:48 PM

Answers

  • You can try Spire.PDF for .NET. download and add dll to your project,you can also set  size, transparency and insert place for your image.

    //Create a pdf document.
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(@"sample.pdf");
                //get the page
                PdfPageBase page = doc.Pages[0];
    
                //get the image
                PdfImage image = PdfImage.FromFile(@"sample.jpg");
                float width = image.Width * 0.75f;
                float height = image.Height * 0.75f;
                float x = (page.Canvas.ClientSize.Width - width) / 2;
    
                // insert image
    
                page.Canvas.DrawImage(image, x, 60, width, height);
    
                //Save pdf file.
                doc.SaveToFile("Sample.pdf");
                doc.Close();
    
                //Launching the Pdf file.
                System.Diagnostics.Process.Start("Sample.pdf");
                Console.ReadKey();

    • Marked as answer by guangwen.gao Wednesday, April 1, 2015 12:15 PM
    Friday, March 27, 2015 2:53 AM

All replies

  •  Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    
            string pdfFilePath = Server.MapPath(".") + "/PDFFiles";      
    
            PdfWriter writer = PdfAWriter.GetInstance(doc, new FileStream(pdfFilePath + "/Default.pdf", FileMode.Create));
    
            doc.Open();
    
            try
    
            {
    
                Paragraph paragraph = new Paragraph("Getting Started ITextSharp.");
    
                string imageURL = Server.MapPath(".") + "/image2.jpg";
    
                iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
    
                //Resize image depend upon your need
    
                jpg.ScaleToFit(140f, 120f);
    
                //Give space before image
    
                jpg.SpacingBefore = 10f;
    
                //Give some space after the image
    
                jpg.SpacingAfter = 1f;
    
                jpg.Alignment = Element.ALIGN_LEFT;
    
     
    
                doc.Add(paragraph);
    
                doc.Add(jpg);
    
     

    Thursday, March 26, 2015 5:40 PM
  • You can try Spire.PDF for .NET. download and add dll to your project,you can also set  size, transparency and insert place for your image.

    //Create a pdf document.
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(@"sample.pdf");
                //get the page
                PdfPageBase page = doc.Pages[0];
    
                //get the image
                PdfImage image = PdfImage.FromFile(@"sample.jpg");
                float width = image.Width * 0.75f;
                float height = image.Height * 0.75f;
                float x = (page.Canvas.ClientSize.Width - width) / 2;
    
                // insert image
    
                page.Canvas.DrawImage(image, x, 60, width, height);
    
                //Save pdf file.
                doc.SaveToFile("Sample.pdf");
                doc.Close();
    
                //Launching the Pdf file.
                System.Diagnostics.Process.Start("Sample.pdf");
                Console.ReadKey();

    • Marked as answer by guangwen.gao Wednesday, April 1, 2015 12:15 PM
    Friday, March 27, 2015 2:53 AM
  • Hi,

    iText is a third party library to create PDF  originally written for java. iTextSharp is the C# adaptation of that
    library. Question regarding iText are better asked on the iText forum, rather than the Microsoft Forum:

    http://itextpdf.com/support

    Thanks for your understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 27, 2015 8:35 AM
  • Thank you for your answer.

    I am sorry too late to reply. and these days I worked on some other project.

    at last, thank you very much.   And my points is zero, if I have some points, I will give it to you.

    Best regards.

    Wednesday, April 1, 2015 12:18 PM