Revision | 62 (tree) |
---|---|
Time | 2022-08-21 16:09:37 |
Author | hirukawa_ryo |
* jpki-pdf-signer 1.2.6
用紙が回転している(rotation != 90)のPDFに印影を付けるときに回転を考慮して画像の向きと位置を調整するようにしました。
@@ -46,7 +46,12 @@ | ||
46 | 46 | import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties; |
47 | 47 | import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSignDesigner; |
48 | 48 | |
49 | +import javax.imageio.ImageIO; | |
49 | 50 | import java.awt.Desktop; |
51 | +import java.awt.Graphics2D; | |
52 | +import java.awt.image.BufferedImage; | |
53 | +import java.io.ByteArrayInputStream; | |
54 | +import java.io.ByteArrayOutputStream; | |
50 | 55 | import java.io.File; |
51 | 56 | import java.io.FileInputStream; |
52 | 57 | import java.io.FileOutputStream; |
@@ -489,18 +494,69 @@ | ||
489 | 494 | |
490 | 495 | PDDocument document = pdfView.getDocument(); |
491 | 496 | int pageIndex = pdfView.getPageIndex(); |
497 | + int rotation = document.getPage(pageIndex).getRotation(); | |
498 | + System.out.println("rotation=" + rotation); | |
492 | 499 | PDRectangle pageMediaBox = document.getPage(pageIndex).getMediaBox(); |
493 | - double xPt = x * pageMediaBox.getWidth() / renderBounds.getWidth(); | |
494 | - double yPt = y * pageMediaBox.getHeight() / renderBounds.getHeight(); | |
495 | 500 | |
496 | 501 | PDVisibleSignDesigner designer; |
497 | 502 | try(InputStream is = new FileInputStream(signature.getFile())) { |
498 | - designer = new PDVisibleSignDesigner(is); | |
499 | - designer.width((float)mm2px(signature.getWidthMillis())); | |
500 | - designer.height((float)mm2px(signature.getHeightMillis())); | |
501 | - designer.xAxis((float)xPt - designer.getWidth() / 2); | |
502 | - designer.yAxis((float)yPt - designer.getHeight() / 2 - pageMediaBox.getHeight()); | |
503 | + if(rotation == 0) { | |
504 | + designer = new PDVisibleSignDesigner(is); | |
505 | + designer.width((float)mm2px(signature.getWidthMillis())); | |
506 | + designer.height((float)mm2px(signature.getHeightMillis())); | |
507 | + double xPt = x * pageMediaBox.getWidth() / renderBounds.getWidth(); | |
508 | + double yPt = y * pageMediaBox.getHeight() / renderBounds.getHeight(); | |
509 | + designer.xAxis((float)xPt - designer.getWidth() / 2); | |
510 | + designer.yAxis((float)yPt - designer.getHeight() / 2 - pageMediaBox.getHeight()); | |
511 | + } else { | |
512 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); | |
513 | + BufferedImage image = ImageIO.read(is); | |
514 | + int w1 = image.getWidth(); | |
515 | + int h1 = image.getHeight(); | |
516 | + int w2 = (rotation % 180 == 0) ? w1 : h1; | |
517 | + int h2 = (rotation % 180 == 0) ? h1 : w1; | |
518 | + BufferedImage rotated = new BufferedImage(w2, h2, image.getType()); | |
519 | + Graphics2D g2d = rotated.createGraphics(); | |
520 | + g2d.translate((w2 - w1) / 2, (h2 - h1) / 2); | |
521 | + g2d.rotate(Math.toRadians(rotation * -1), w1 / 2, h1 / 2); | |
522 | + g2d.drawImage(image, null, 0, 0); | |
523 | + g2d.dispose(); | |
524 | + ImageIO.write(rotated, "PNG", buf); | |
525 | + | |
526 | + try(InputStream is2 = new ByteArrayInputStream(buf.toByteArray())) { | |
527 | + designer = new PDVisibleSignDesigner(is2); | |
528 | + } | |
529 | + double xPt; | |
530 | + double yPt; | |
531 | + switch(rotation) { | |
532 | + case 90: | |
533 | + designer.width((float)mm2px(signature.getHeightMillis())); | |
534 | + designer.height((float)mm2px(signature.getWidthMillis())); | |
535 | + xPt = x * pageMediaBox.getHeight() / renderBounds.getWidth(); | |
536 | + yPt = y * pageMediaBox.getWidth() / renderBounds.getHeight(); | |
537 | + designer.xAxis((float)yPt - designer.getWidth() / 2); | |
538 | + designer.yAxis(- (float)xPt - designer.getHeight() / 2); | |
539 | + break; | |
540 | + case 180: | |
541 | + designer.width((float)mm2px(signature.getWidthMillis())); | |
542 | + designer.height((float)mm2px(signature.getHeightMillis())); | |
543 | + xPt = x * pageMediaBox.getWidth() / renderBounds.getWidth(); | |
544 | + yPt = y * pageMediaBox.getHeight() / renderBounds.getHeight(); | |
545 | + designer.xAxis(- (float)xPt - designer.getWidth() / 2 + pageMediaBox.getWidth()); | |
546 | + designer.yAxis(- (float)yPt - designer.getHeight() / 2); | |
547 | + break; | |
548 | + case 270: | |
549 | + designer.width((float)mm2px(signature.getHeightMillis())); | |
550 | + designer.height((float)mm2px(signature.getWidthMillis())); | |
551 | + xPt = x * pageMediaBox.getHeight() / renderBounds.getWidth(); | |
552 | + yPt = y * pageMediaBox.getWidth() / renderBounds.getHeight(); | |
553 | + designer.xAxis(- (float)yPt - designer.getWidth() / 2 + pageMediaBox.getWidth()); | |
554 | + designer.yAxis((float)xPt - designer.getHeight() / 2 - pageMediaBox.getHeight()); | |
555 | + break; | |
556 | + } | |
557 | + } | |
503 | 558 | } |
559 | + | |
504 | 560 | PDVisibleSigProperties props = new PDVisibleSigProperties(); |
505 | 561 | props.setPdVisibleSignature(designer); |
506 | 562 | props.visualSignEnabled(true); |