簡體   English   中英

如何在itextsharp表中調整圖像大小

[英]how to resize images in itextsharp table

我正在嘗試使用網絡表單從數據庫中打印出收據。 我想在第一個表格的左側包含一個徽標。 徽標(logo.gif)無法調整大小並在單元格內傳播。 我嘗試使用 absolutescale 和 scalepercent 更改圖像的大小,但圖像仍然填充了單元格。 有沒有辦法操縱圖像,使其可以更小?

這些是我的代碼

    protected void btn_con_Click(object sender, EventArgs e)
    {

        PdfPTable pdfTbl = new PdfPTable(GridView1.HeaderRow.Cells.Count);
        foreach (TableCell headcell in GridView1.HeaderRow.Cells)
        {
            PdfPCell pdcell = new PdfPCell(new Phrase(headcell.Text));
            pdfTbl.AddCell(pdcell);
        }
        foreach (GridViewRow gvrow in GridView1.Rows)
        {

            foreach (TableCell tblcell in gvrow.Cells)
            {
                PdfPCell pdcell = new PdfPCell(new Phrase(tblcell.Text));
                pdfTbl.AddCell(pdcell);
            }
        }



        Document pfddoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
        PdfWriter.GetInstance(pfddoc, Response.OutputStream);


        pfddoc.Open();


        string imageURL = Server.MapPath(".") + "/logo.gif";

        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);


        //Resize image depend upon your need

        jpg.ScaleToFit(50f, 50f);

        //Give space before image

        jpg.SpacingBefore = 5f;

        //Give some space after the image

        jpg.SpacingAfter = 0f;

        jpg.Alignment = Element.ALIGN_LEFT;


        Paragraph para = new Paragraph("\b Putri Sejinjang Spa \b \n Rumah Kedai Mara, Lot 12953, \n Jalan Tun Datuk Patinggi Haji Abdul Rahman Yakub \n Taman Heritage, Sarawak,93050 Kuching, Sarawak \n " +
            "Tel: 082-428 660, Fax: 082-417660, Whatsapp: 013-8448616 \n website:PutriSejinjangSpa.com.my, e-mail: putri_sejinjang07@yahoo.com.my \n\n", new Font(Font.FontFamily.HELVETICA, 12));
        para.Alignment = Element.ALIGN_LEFT;
        jpg.SpacingAfter = 1f;


        //start
        PdfPTable table = new PdfPTable(2);
        table.TotalWidth = 50f;

        PdfPCell cell = new PdfPCell(new Phrase("Invois Putri Sejinjang Spa"));
        table.DefaultCell.Border = Rectangle.NO_BORDER;
        cell.Colspan = 2;

        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        table.AddCell(cell);

        cell.Colspan = 2;
        table.AddCell(jpg);
        jpg.ScalePercent(24f);

        //jpg.ScaleAbsolute(10f, 10f);
        table.AddCell(para);



        pfddoc.Add(table);
        //end
        pfddoc.Add(pdfTbl);


        pfddoc.Close();

        Response.ContentType = "application/pdf";
        Response.AppendHeader("content-disposition", "attachment;filename=Resit.pdf");
        Response.Write(pfddoc);
        Response.Flush();
        Response.End();
    }

在服務器端設置圖像高度和寬度與 ScaleToFit 圖像大小完全相同。

jpg.ScaleAbsolute(40f, 40f);

jpg.ScaleToFit(120f, 155.25f);

暫無
暫無

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

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