codequery18
codequery18

Reputation: 151

c# - itextsharp image scaletofit

im having a problem with scaling image to fit properly

this is my problem. the red border of the image must not overlap the blue border of the pdf page

the size of the image below i used is 173*292

enter image description here

and here is my code

using (MemoryStream ms = new MemoryStream())
            {
                Document doc = new Document(PageSize.A4, 10, 10, 20, 35);
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(System.IO.Path.Combine(filepath, pdfLocation), FileMode.Create));
                doc.AddTitle("Document Title");

                doc.Open();

                iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(strFilename);
                image1.Alignment = iTextSharp.text.Image.ALIGN_CENTER;

                image1.Border = iTextSharp.text.Rectangle.BOX;
                image1.BorderWidth = 3.0f;
                image1.BorderColor = iTextSharp.text.BaseColor.RED;

                PdfContentByte content = writer.DirectContent;
                iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(doc.PageSize);
                rectangle.Left += doc.LeftMargin;
                rectangle.Right -= doc.RightMargin;
                rectangle.Top -= doc.TopMargin;
                rectangle.Bottom += doc.BottomMargin;
                content.SetColorStroke(iTextSharp.text.BaseColor.BLUE);
                content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
                content.Stroke();

                float pageWidth = doc.PageSize.Width - (35 + 35);
                float pageHeight = doc.PageSize.Height - (35 + 35);
                image1.ScaleToFit(pageWidth, pageHeight);

                image1.SetAbsolutePosition((PageSize.A4.Width - image1.ScaledWidth) / 2, (PageSize.A4.Height - image1.ScaledHeight) / 2);
                doc.Add(image1);
                doc.Close();

            }

i thought my code works properly.

the screenshot below is i used an image with the size of 3000*2363 and the image border doesn't overlap the border of the pdf size

enter image description here

Upvotes: 2

Views: 6313

Answers (1)

Vision
Vision

Reputation: 21

I assume the problem arises because different values are used for margin top and bot. These, however, are no longer observed afterwards. In fact, you calculate the remaining space between the whole page and the image and then start the image in half. Whereby the borders are ignored. This works as long as the borders are the same size (like left+right). Because of the scaling the image then becomes so small that the edges are indirectly taken into account again.

To determine the correct start, the margins must be observed:

image1.SetAbsolutePosition((PageSize.A4.Width - image1.ScaledWidth) / 2, (PageSize.A4.Height - doc.BottomMargin - doc.TopMargin - image1.ScaledHeight) / 2 + doc.TopMargin);

This means that the remaining space within the blue rectangle is determined. Half of this + the margin distance above. If one also distinguishes left and right, these must be supplemented there in the same way.

Upvotes: 1

Related Questions