簡體   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);
             }
         }

用於創建標題圖像的代碼; 但是圖片看起來太大了。嘗試使用imgfoot.ScaleAbsolute(826,1100); 但劑量有效,結果顯示為圖像的一半。請任何人幫助我解決。 .................................................. .............................................

假設您正在談論imgHead ,那么您正在創建一個寬度為826個用戶單位,高度為1100個用戶單位的圖像。 然后,您將該圖像添加到XObject窗體的位置(0, 0) ,其寬度為2480個用戶單位(足夠),高度為370個用戶單位(對於圖像的高度為1100個用戶單位是不夠的) )。

您說圖像只顯示一半。 正確無誤:您已將圖片裁剪到其高度(370/1100)的33.6%。 更改Form XObject tp的高度值,以使圖像適合並且您已經解決了問題。

另外:用戶單位不是像素! 默認情況下,1個用戶單位對應1點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM