简体   繁体   中英

How to reduce the size of image in header of the pdf using itextsharp

 public class itsEventsHandler : PdfPageEventHelper
         {
             PdfTemplate total;
             BaseFont helv;


             public override void OnEndPage(PdfWriter writer, Document document)
             {

iTextSharp.text.Image JPG = iTextSharp.text.Image.GetInstance("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");
             JPG.ScalePercent(35f);
             JPG.SetAbsolutePosition(130f, 240f);


             iTextSharp.text.Image imgfoot = JPG;
                 //Header Image 
                 iTextSharp.text.Image imghead = iTextSharp.text.Image.GetInstance("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");

                 imgfoot.SetAbsolutePosition(0, 0);
                 imghead.SetAbsolutePosition(0, 0);
                 imgfoot.ScaleAbsolute(826, 1100);
                 PdfContentByte cbhead = writer.DirectContent;
                 PdfTemplate tp = cbhead.CreateTemplate(2480, 370); // units are in pixels but I'm not sure if thats the correct units
                 tp.AddImage(imghead);

                 PdfContentByte cbfoot = writer.DirectContent;
                 PdfTemplate tpl = cbfoot.CreateTemplate(2480, 664);
                 tpl.AddImage(imgfoot);
                 cbhead.AddTemplate(tp, 0, 715);

                 helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

                 /*PdfContentByte cb = writer.DirectContent;
                 cbfoot.SaveState();
                 document.SetMargins(35, 35, 100, 82);
                 cb.RestoreState();*/

                 //document.NewPage(); 
                 base.OnStartPage(writer, document);
             }

             public override void OnOpenDocument(PdfWriter writer, Document document)
             {
                 total = writer.DirectContent.CreateTemplate(100, 100);
                 total.BoundingBox = new iTextSharp.text.Rectangle(-20, -20, 100, 100);
                 helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
             }
         }

Code for creating header images; but images looks too big.tried to use a imgfoot.ScaleAbsolute(826, 1100); but dosent worked it result to image shows half.Please anyone help me to resolve. ...............................................................................................

Assuming that you are talking about imgHead , then you are creating an image with a width of 826 user units and a height of 1100 user units. You are then adding this image at position (0, 0) of a form XObject with a width of 2480 user units (that's sufficient) and a height of 370 user units (that's not sufficient for the height of the image which is 1100 user units).

You say that the image shows only half. To be correct: you have clipped the image to 33.6% of its height (370 / 1100). Change the height value of the Form XObject tp so that the image fits and you've solved your problem.

Also: user units aren't pixels! By default 1 user unit corresponds with 1 point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM