gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:14:25 25 4
gpt4 key购买 nike

这是我的源代码。

  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 后,图像大小调整完美。
再次感谢您。

最佳答案

您的指控“这一切都没有调整图像大小”是错误的,并且您使用了错误的方法来获取图像的宽度和高度。看看chapter 2 of my book :

There are different ways to change the dimensions of an image:

  • The width and height parameters of scaleToFit() define the maximum dimensions of the image. If the width/height ratio differs from the aspect ratio of the image, either the width, or the height, will be smaller than the corresponding parameter of this method.
  • The width and height parameters will be respected when using scaleAbsolute(). The resulting image risks being stretched in the X or Y direction if you don’t choose the parameters wisely. You can also use scaleAbsoluteWidth() and scaleAbsoluteHeight().
  • scalePercent() comes in two versions: one with two parameters, a percentage for the width and a percentage for the height; and another with only one parameter, a percentage that will be applied equally to the width and the height.

It’s a common misconception that resizing images in iText also changes the quality of the image. It’s important to understand that iText takes the image as is: iText doesn’t change the number of pixels in the image.

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

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

When creating an Image instance from a file, you won’t always know its dimensions before or even after scaling it. You can get the width and height of the image with these methods:

  • getWidth() and getHeight() are inherited from the Rectangle object. They return the original height and width of the image.
  • getPlainWidth() and getPlainHeight() return the width and height after scaling. These are the dimensions of the image used to print it on a page.
  • getScaledWidth() and getScaledHeight() return the width and height needed to print the image. These dimensions are equal to the plain width and height, except in cases where the image is rotated.

在您的代码中,您可以获得图像的原始尺寸。您更改图像的尺寸,然后询问原始尺寸。当您询问原始尺寸而不是原始尺寸时,您不会得到不同的尺寸,这应该不会令您感到惊讶。

关于java - 如何在java中调整itext中图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40523751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com