简体   繁体   English

如何使用itextsharp减小pdf标头中的图像大小

[英]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); 但是图片看起来太大了。尝试使用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. 假设您正在谈论imgHead ,那么您正在创建一个宽度为826个用户单位,高度为1100个用户单位的图像。 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). 然后,您将该图像添加到XObject窗体的位置(0, 0) ,其宽度为2480个用户单位(足够),高度为370个用户单位(对于图像的高度为1100个用户单位是不够的) )。

You say that the image shows only half. 您说图像只显示一半。 To be correct: you have clipped the image to 33.6% of its height (370 / 1100). 正确无误:您已将图片裁剪到其高度(370/1100)的33.6%。 Change the height value of the Form XObject tp so that the image fits and you've solved your problem. 更改Form XObject tp的高度值,以使图像适合并且您已经解决了问题。

Also: user units aren't pixels! 另外:用户单位不是像素! By default 1 user unit corresponds with 1 point. 默认情况下,1个用户单位对应1点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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