none
How Create itextsharp Pdf Add images in c# RRS feed

  • Question

  • I would like to have four images in 1 page in a pdf file. As well as two and down two. And there will be some writing on every image. Which has been viewed in the image https://drive.google.com/file/d/1WLP9crhzvy8vn4MfDKcqHtT9eNP0VBwl/view?usp=sharing

    Wednesday, March 13, 2019 7:24 AM

Answers

  • Hi

    Thank you for posting here.

    According to your description, you want to have four images in 1 page in a pdf file by using itext#. 

    You could try the following code.

    static void Main(string[] args)
            {
                Document doc = new Document(PageSize.A4);
                string path = "D:\\test.pdf";
                var output = new FileStream(path, FileMode.Create);
                var writer = PdfWriter.GetInstance(doc, output);
                doc.Open();
                
    
                var logo = iTextSharp.text.Image.GetInstance("D://pic/1.png");
                logo.SetAbsolutePosition(0, 550);
                logo.ScaleAbsoluteHeight(200);
                logo.ScaleAbsoluteWidth(280);
                doc.Add(logo);
    
                var logo1 = iTextSharp.text.Image.GetInstance("D://pic/2.png");
                logo1.SetAbsolutePosition(300, 550);
                logo1.ScaleAbsoluteHeight(200);
                logo1.ScaleAbsoluteWidth(280);
                doc.Add(logo1);
    
                var logo2= iTextSharp.text.Image.GetInstance("D://pic/4.png");
                logo2.SetAbsolutePosition(300, 0);
                logo2.ScaleAbsoluteHeight(200);
                logo2.ScaleAbsoluteWidth(280);
         
                doc.Add(logo2);
                var logo3 = iTextSharp.text.Image.GetInstance("D://pic/3.png");
                logo3.SetAbsolutePosition(0,0);
                logo3.ScaleAbsoluteHeight(200);
                logo3.ScaleAbsoluteWidth(280);
                doc.Add(logo3);
                doc.Close();
        
            }
    

    Result:

     

    Best regards,                                                              

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 5:13 AM
    Moderator

All replies

  • You can take a look at free spire.pdf, with which you can place images at specified X, Y offsets.

    PdfDocument doc = new PdfDocument();
    PdfPageBase page = doc.Pages.Add();
    
    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;
    
    //Draw image at specified position
    page.Canvas.DrawImage(image, x, 60, width, height);
    
    doc.SaveToFile("outputfilepath");


    • Edited by Jamesbn Thursday, March 14, 2019 1:55 AM
    Thursday, March 14, 2019 1:54 AM
  • Hi

    Thank you for posting here.

    According to your description, you want to have four images in 1 page in a pdf file by using itext#. 

    You could try the following code.

    static void Main(string[] args)
            {
                Document doc = new Document(PageSize.A4);
                string path = "D:\\test.pdf";
                var output = new FileStream(path, FileMode.Create);
                var writer = PdfWriter.GetInstance(doc, output);
                doc.Open();
                
    
                var logo = iTextSharp.text.Image.GetInstance("D://pic/1.png");
                logo.SetAbsolutePosition(0, 550);
                logo.ScaleAbsoluteHeight(200);
                logo.ScaleAbsoluteWidth(280);
                doc.Add(logo);
    
                var logo1 = iTextSharp.text.Image.GetInstance("D://pic/2.png");
                logo1.SetAbsolutePosition(300, 550);
                logo1.ScaleAbsoluteHeight(200);
                logo1.ScaleAbsoluteWidth(280);
                doc.Add(logo1);
    
                var logo2= iTextSharp.text.Image.GetInstance("D://pic/4.png");
                logo2.SetAbsolutePosition(300, 0);
                logo2.ScaleAbsoluteHeight(200);
                logo2.ScaleAbsoluteWidth(280);
         
                doc.Add(logo2);
                var logo3 = iTextSharp.text.Image.GetInstance("D://pic/3.png");
                logo3.SetAbsolutePosition(0,0);
                logo3.ScaleAbsoluteHeight(200);
                logo3.ScaleAbsoluteWidth(280);
                doc.Add(logo3);
                doc.Close();
        
            }
    

    Result:

     

    Best regards,                                                              

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 5:13 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 5:12 AM
    Moderator