1

im using itextsharp for exporting the image to pdf. ---- i want to make the edges of image smooth (curved edges), ---- and by itextsharp image property to get the image width and height(while getting the image from disk) ---- and also how to set the background color of the pdf page

following is for getting image and adding to pdf:

 pdfDoc.Open();
        //pdfDoc.Add(new iTextSharp.text.Paragraph("Welcome to dotnetfox"));
        iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(@"C:\Users\Admin\Desktop\logoall.bmp");
       // gif.ScaleToFit(500, 100);
        pdfDoc.Add(gif);

following is making grid to image and saving to disk:

Grid companysnapshot = values[0] as Grid;    //companysnap shot

        companysnapshot.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
        int companywidth = (int)Math.Round(companysnapshot.ActualWidth);
        int companyheight = (int)Math.Round(companysnapshot.ActualHeight);
        companywidth = companywidth == 0 ? 1 : companywidth;
        companyheight = companyheight == 0 ? 1 : companyheight;

        RenderTargetBitmap rtbmp = new RenderTargetBitmap(companywidth, companyheight, 96d, 96d, PixelFormats.Default);
        rtbmp.Render(companysnapshot);
        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(rtbmp));
        FileStream fs1 = File.Create(@"C:\Users\Admin\Desktop\companyss.bmp");
        encoder.Save(fs1);
        fs1.Close();

please code me out for this!!

Annadate Piyush
  • 458
  • 5
  • 18

1 Answers1

1

You have combined multiple question into one post. That's not the way you should post questions.

Anyway:

Question 1: What is the size of an image?

You have an Image instance gif. The witdh of this image is gif.ScaledWidthand jpg.ScaledHeight. There are other ways to get the width and the height, but this way always gives you the size in user units that will be used in the PDF.

If you do not scale the image, ScaledWidth and ScaledHeight will give you the original size of the image in pixels. Pixels will be treated as user units by iText. In PDF, a user unit corresponds with a point by default (and 72 points correspond with 1 inch).

Question 2: How do you display the image with rounded corners?

Some image formats (such as PNG) allow transparency. You could create an image in such a way that the effect of rounded corners is mimicked by making the corners transparent.

If this is not an option, you should apply a clipping path. This is demonstrated in the ClippingPath example in chapter 10 of my book.

Ported to C#, the example would be something like this:

Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h);
t.Ellipse(0, 0, w, h);
t.Clip();
t.NewPath();
t.AddImage(img, w, 0, 0, h, 0, -600);
Image clipped = Image.GetInstance(t);

Of course: this clips the image into an ellipse as shown in the resulting PDF. You need to replace the Ellipse() method from the example with the RoundRectangle() method.

Question3: How to give each page a background color?

This is a duplicate question. Please read the answers to the following questions:

Adding a background color is done using page events and you'll find the code on how to do this in the questions mentioned above.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • after making changes pdf is getting generated, but the clipped image is not getting displayed in the pdf.. – Annadate Piyush Sep 20 '14 at 12:29
  • the clipped images is getting the gif and template in its property...still the clipped is not getting displayed in the pdf – Annadate Piyush Sep 20 '14 at 12:39
  • 1
    I hope that you aren't copying the example literally. If you did, you are adding the image at a position `y = -600`. That is way outside the visible area of the page. If you *did* think before applying the code and if you *did* adapt the `x` and `y` coordinate, please show us *what* you did. – Bruno Lowagie Sep 20 '14 at 16:05
  • sir, issue is solved,, there was a issue with itextsharp.dll, updated it again n executed ur code. issue is now solved, but still looking for the background of pdf page.!! – Annadate Piyush Sep 21 '14 at 16:03