• R/O
  • SSH
  • HTTPS

jpki: Commit


Commit MetaInfo

Revisão62 (tree)
Hora2022-08-21 16:09:37
Autorhirukawa_ryo

Mensagem de Log

* jpki-pdf-signer 1.2.6
用紙が回転している(rotation != 90)のPDFに印影を付けるときに回転を考慮して画像の向きと位置を調整するようにしました。

Mudança Sumário

Diff

--- jpki-pdf-signer/trunk/src/main/java/net/osdn/jpki/pdf_signer/MainApp.java (revision 61)
+++ jpki-pdf-signer/trunk/src/main/java/net/osdn/jpki/pdf_signer/MainApp.java (revision 62)
@@ -46,7 +46,12 @@
4646 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties;
4747 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSignDesigner;
4848
49+import javax.imageio.ImageIO;
4950 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;
5055 import java.io.File;
5156 import java.io.FileInputStream;
5257 import java.io.FileOutputStream;
@@ -489,18 +494,69 @@
489494
490495 PDDocument document = pdfView.getDocument();
491496 int pageIndex = pdfView.getPageIndex();
497+ int rotation = document.getPage(pageIndex).getRotation();
498+ System.out.println("rotation=" + rotation);
492499 PDRectangle pageMediaBox = document.getPage(pageIndex).getMediaBox();
493- double xPt = x * pageMediaBox.getWidth() / renderBounds.getWidth();
494- double yPt = y * pageMediaBox.getHeight() / renderBounds.getHeight();
495500
496501 PDVisibleSignDesigner designer;
497502 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+ }
503558 }
559+
504560 PDVisibleSigProperties props = new PDVisibleSigProperties();
505561 props.setPdVisibleSignature(designer);
506562 props.visualSignEnabled(true);
Show on old repository browser