@RequestMapping(value =
"/pdf"
,method = RequestMethod.POST)
public
void test(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
String filePath =
"D:\\blog\\exportPdf2.pdf"
;
String imagePath =
"D:\\blog\\exportImg2.png"
;
Document document =
new
Document();
try
{
Map getMap = request.getFileMap();
MultipartFile mfile = (MultipartFile) getMap.get(
"imgData"
);
InputStream file = mfile.getInputStream();
byte[] fileByte = FileCopyUtils.copyToByteArray(file);
FileImageOutputStream imageOutput =
new
FileImageOutputStream(
new
File(imagePath));
imageOutput.write(fileByte, 0, fileByte.length);
imageOutput.close();
PdfWriter.getInstance(document,
new
FileOutputStream(filePath));
document.open();
document.add(
new
Paragraph(
"JUST TEST ..."
));
Image image = Image.getInstance(imagePath);
float heigth = image.getHeight();
float width = image.getWidth();
int percent = getPercent2(heigth, width);
image.setAlignment(Image.MIDDLE);
image.scalePercent(percent+3);
document.add(image);
document.close();
}
catch
(DocumentException de) {
System.err.println(de.getMessage());
}
catch
(Exception e) {
e.printStackTrace();
}
}
private
static
int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.
round
(p2);
return
p;
}