如何在java中的itext中调整图像大小

时间:2016-11-10 09:13:44

标签: java pdf itext

这是我的源代码。

  Document document = new Document();
    String txtVehNo = "";
    String fDate = "", tDate = "", remtype = "0";
    String fileName = "Reminders_" + Utility.sysDate("yyyy_MM_dd") + ".pdf";
    String asDate = Utility.sysDate("dd/MM/yyyy");
    ArrayList<ReminderPDF> reminderPDFs = null;
    UserDetails userDetails = null;
    String txtMarNo = "";
    String formattedDate = "";
    String address = "";



    Blob imageBlob = null;
    byte[] imageBytes = new byte[0];

    try {
        txtVehNo = request.getParameter("txtVehNo2");
        fDate = request.getParameter("txtDate3");
        tDate = request.getParameter("txtDate4");
        remtype = request.getParameter("remtype");
        txtMarNo = request.getParameter("txtMarNo");

        if (txtVehNo != null && txtVehNo != "") {

            reminderPDFs = DataManagerBean.downloadRemindersForRequirementLetter(txtVehNo, remtype);


        } else if (txtMarNo != null && txtMarNo != "") {

            reminderPDFs = DataManagerBean.downloadRemindersForRequirementLetterByMarNo(txtMarNo, remtype);


        } else {

            reminderPDFs = DataManagerBean.downloadRemindersForRequirementLetter(fDate, tDate, remtype);


        }
        if (reminderPDFs.size() > 0) {

            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
            PdfWriter.getInstance(document, response.getOutputStream());
            //PdfWriter.getInstance(document, new FileOutputStream(fileName));
            document.open();
            document.setPageSize(PageSize.A4);
            document.setMargins(85, 85, 85, 85);
            for (int i = 0; i < reminderPDFs.size(); i++) {
                String adrstr[] = reminderPDFs.get(i).address.split("\r\n");

                address = reminderPDFs.get(i).address.replaceAll(adrstr[adrstr.length - 1], "");
                document.newPage();
                Paragraph[] paragraph = new Paragraph[8];
                paragraph[0] = new Paragraph(new Phrase(reminderPDFs.get(i).mainHeader, FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph[0].setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph[0]);
                paragraph[1] = new Paragraph(new Phrase(reminderPDFs.get(i).subHeader, FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph[1].setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph[1]);
                if (reminderPDFs.get(i).print.equals("Y")) {
                    paragraph[2] = new Paragraph(new Phrase("(Reprint)", FontFactory.getFont(FontFactory.COURIER, 10)));
                } else {
                    paragraph[2] = new Paragraph(new Phrase("", FontFactory.getFont(FontFactory.COURIER, 11)));
                }
                paragraph[2].setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph[2]);
                paragraph[3] = new Paragraph(new Phrase("Date : " + reminderPDFs.get(i).asDate, FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph[3].setAlignment(Element.ALIGN_RIGHT);
                document.add(paragraph[3]);
                paragraph[4] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph[4].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph[4]);
                paragraph[5] = new Paragraph(new Phrase(address, FontFactory.getFont(FontFactory.COURIER, 11, Font.BOLD)));
                paragraph[5].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph[5]);
                paragraph[6] = new Paragraph(new Phrase("\n" + adrstr[adrstr.length - 1], FontFactory.getFont(FontFactory.COURIER, 11, Font.PLAIN)));
                paragraph[6].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph[6]);
                paragraph[7] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph[7].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph[7]);

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(100);


                formattedDate = Utility.dateFormatter(reminderPDFs.get(i).dol, "yyyy-MM-dd", "dd-MM-yyyy");

                table.addCell(new Phrase("Vehicle No: " + reminderPDFs.get(i).vehno, FontFactory.getFont(FontFactory.COURIER, 11)));
                table.addCell(new Phrase("MAR No       : " + reminderPDFs.get(i).marno, FontFactory.getFont(FontFactory.COURIER, 11)));
                table.addCell(new Phrase("Policy No : " + reminderPDFs.get(i).policyno, FontFactory.getFont(FontFactory.COURIER, 11)));
                table.addCell(new Phrase("Date of Loss : " + formattedDate, FontFactory.getFont(FontFactory.COURIER, 11)));
                document.add(table);

                Paragraph[] paragraph2 = new Paragraph[3];
                paragraph2[0] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph2[0].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph2[0]);

                String[] docs = reminderPDFs.get(i).content.split("\r\n");

                paragraph2[1] = new Paragraph(new Phrase(docs[0], FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph2[1].setAlignment(Element.ALIGN_JUSTIFIED);
                document.add(paragraph2[1]);
                paragraph2[2] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph2[2].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph2[2]);


                // if (reminderPDFs.get(i).docs.length() > 0) {
                int k = 1;
                for (int j = 1; j < docs.length; j++) {
                    if (!docs[j].trim().equalsIgnoreCase("")) {
                        document.add(new Paragraph(new Phrase("          " + k + ". " + docs[j].trim(), FontFactory.getFont(FontFactory.COURIER, 11))));
                        k++;
                    }
                }
                // }

                Paragraph[] paragraph3 = new Paragraph[7];
                paragraph3[0] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD)));
                paragraph3[0].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph3[0]);

                paragraph3[1] = new Paragraph(new Phrase("Thank You,", FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph3[1].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph3[1]);

                paragraph3[2] = new Paragraph(new Phrase("Yours Faithfully,", FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph3[2].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph3[2]);

                paragraph3[3] = new Paragraph(new Phrase("Union Assurance General Ltd", FontFactory.getFont(FontFactory.COURIER, 11, Font.BOLD)));
                paragraph3[3].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph3[3]);

                paragraph3[4] = new Paragraph(new Phrase(" ", FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD)));
                paragraph3[4].setAlignment(Element.ALIGN_CENTER);
                document.add(paragraph3[4]);


                imageBlob = DataManagerBean.getESignature(reminderPDFs.get(i).createdUser);
                if (imageBlob != null) {
                    imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
                    Image img = Image.getInstance(imageBytes);
                    float width = img.getWidth();
                    float hight = img.getHeight();
                    if (width > 200 || hight > 100) {
//                        img.scaleAbsoluteWidth(200);
//                        img.scaleAbsoluteHeight(100);
 //                       img.scaleAbsolute(200f, 100f);
                        img.scaleToFit(200f, 100f);
                    }
                    width = img.getScaledWidth();
                    hight = img.getScaledHeight();
                    document.add(img);
                }

                userDetails = UserBean.getUserDetailsByUserId(reminderPDFs.get(i).createdUser);

                paragraph3[5] = new Paragraph(new Phrase((userDetails.getuFName().concat(" ")).concat(userDetails.getuLName()), FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph3[5].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph3[5]);


                paragraph3[6] = new Paragraph(new Phrase(userDetails.getDesig(), FontFactory.getFont(FontFactory.COURIER, 11)));
                paragraph3[6].setAlignment(Element.ALIGN_LEFT);
                document.add(paragraph3[6]);


                //DataManagerBean.printStatusUpdate(reminderPDFs.get(i).rmid, "Y");
            }
            document.close();

            response.sendRedirect("Reminders.jsp");

        } else {
            if (txtVehNo != null && txtVehNo != "") {
                response.sendRedirect("Reminders.jsp?msgtype=3&msg=No Records for Policy/Vehicle No. - " + txtVehNo);
            } else if (txtMarNo != null && txtMarNo != "") {
                response.sendRedirect("Reminders.jsp?msgtype=4&msg=No Records for Mar No. - " + txtMarNo);
            } else if (fDate != null && fDate != "") {
                response.sendRedirect("Reminders.jsp?msgtype=5&msg=No Records between " + fDate + " to " + tDate);
            }
        }
    } catch (DocumentException e) {
        SystemMessage.getInstance().writeMessage("ERROR : DownloadRequirements.jsp - " + e.toString());
    } finally {
        try {
            //reminderPDFs = null;
        } catch (Exception ex) {
        }
    }

我尝试了 img.scaleAbsoluteWidth(200);           
img.scaleAbsoluteHeight(100);           
img.scaleAbsolute(200f,100f);           
img.scaleToFit(200f,100f);
但不是这个调整图像的大小。
当我调试代码时,我也得到 width = 200和height = 100 。我猜这意味着它的高度和宽度都发生了变化。
请告诉我这里的问题。
谢谢。

稍后注意
问题不是这些方法中的任何一种。问题是图像与该尺寸不兼容。在我将尺寸设置为100,50后,图像调整大小完美。
再次感谢你。

1 个答案:

答案 0 :(得分:2)

你的指控&#34;这一切都没有调整图像大小&#34;是错误的,你使用错误的方法来获得图像的宽度和高度。看看chapter 2 of my book

  

有多种方法可以更改图像的尺寸:

     
      
  • scaleToFit()的宽度和高度参数定义图像的最大尺寸。如果宽高比不同于   图像的宽高比(宽度或高度)将是   小于此方法的相应参数。
  •   
  • 使用scaleAbsolute()时,将遵循宽度和高度参数。由此产生的图像有可能在X中被拉伸   如果不明智地选择参数,则为Y方向。你也可以   使用scaleAbsoluteWidth()scaleAbsoluteHeight()
  •   
  • scalePercent()有两个版本:一个有两个参数,一个是宽度的百分比,另一个是高度的百分比;和另一个   只有一个参数,一个将被平均应用的百分比   宽度和高度。
  •   
     

在iText中调整图像大小也会发生变化,这是一种常见的误解   图像的质量。理解iText很重要   按原样拍摄图像:iText不会改变像素数   图像。

有很多例子证明这些方法有效地改变了文档中图像的大小。

如果您继续阅读文档,则会在代码中发现另一个问题:

  

从文件创建Image实例时,您并不总是知道它   缩放之前或之后的尺寸。你可以得到宽度和   使用这些方法的图像高度:

     
      
  • getWidth()getHeight()继承自Rectangle对象。它们返回图像的原始高度和宽度。
  •   
  • getPlainWidth()getPlainHeight()会在缩放后返回宽度和高度。这些是用于打印图像的尺寸   在页面上。
  •   
  • getScaledWidth()getScaledHeight()返回打印图像所需的宽度和高度。这些尺寸等于   平面宽度和高度,但图像旋转的情况除外。
  •   

在您的代码中,您获得了图片的原始尺寸。您更改图像的尺寸,然后您要求原始尺寸。在询问原始尺寸而不是原始尺寸时,您不会感到惊讶。