From 48f8e5362fa6c7b2f0a708e2dd61cac1dd366397 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Fri, 28 Nov 2014 11:03:34 +0100 Subject: Page Rotation fixes, automatic positioning for rotated pages is not supported yet --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 18 +++++----- .../impl/stamping/pdfbox/PDFAsTemplateCreator.java | 6 ++-- .../pdfbox/PDFAsVisualSignatureBuilder.java | 41 ++++++++++++++++++++-- .../pdfbox/PDFAsVisualSignatureDesigner.java | 29 ++++++++++++--- .../pdfbox/PDFAsVisualSignatureProperties.java | 32 ++++++++++++++++- .../impl/stamping/pdfbox/PdfBoxVisualObject.java | 4 +-- .../lib/impl/stamping/pdfbox/TableDrawUtils.java | 10 +++--- 7 files changed, 115 insertions(+), 25 deletions(-) diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index f050a977..c36ae653 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -257,6 +257,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { .determineTablePositioning(tablePos, "", doc, visualObject, legacy32Position); + logger.debug("Positioning: {}" , positioningInstruction.toString()); + if (positioningInstruction.isMakeNewPage()) { int last = doc.getNumberOfPages() - 1; PDDocumentCatalog root = doc.getDocumentCatalog(); @@ -267,7 +269,7 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { rootPages.getCOSObject().setNeedToBeUpdate(true); PDPage p = new PDPage(lastPage.findMediaBox()); p.setResources(new PDResources()); - + p.setRotation(lastPage.findRotation()); doc.addPage(p); } @@ -281,8 +283,8 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { //rootPages.getAllKids(kids); PDPage targetPage = documentPagesKids.get(targetPageNumber-1); int rot = targetPage.findRotation(); - logger.debug("adding Page rotation: " + rot); - positioningInstruction.setRotation(positioningInstruction.getRotation() + rot); + logger.debug("Page rotation: " + rot); + //positioningInstruction.setRotation(positioningInstruction.getRotation() + rot); logger.debug("resulting Sign rotation: " + positioningInstruction.getRotation()); SignaturePositionImpl position = new SignaturePositionImpl(); @@ -486,13 +488,13 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { helper.deleteFile(fisTmpFile); } catch (IOException e) { - logger.error(MessageResolver.resolveMessage("error.pdf.sig.01"), e); + logger.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); throw new PdfAsException("error.pdf.sig.01", e); } catch (SignatureException e) { - logger.error(MessageResolver.resolveMessage("error.pdf.sig.01"), e); + logger.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); throw new PdfAsException("error.pdf.sig.01", e); } catch (COSVisitorException e) { - logger.error(MessageResolver.resolveMessage("error.pdf.sig.01"), e); + logger.warn(MessageResolver.resolveMessage("error.pdf.sig.01"), e); throw new PdfAsException("error.pdf.sig.01", e); } finally { if (doc != null) { @@ -639,10 +641,10 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { .getHeight() * factor)), null); return cutOut; } catch (PdfAsException e) { - logger.error("PDF-AS Exception", e); + logger.warn("PDF-AS Exception", e); throw ErrorExtractor.searchPdfAsError(e, status); } catch (Throwable e) { - logger.error("Throwable Exception", e); + logger.warn("Unexpected Throwable Exception", e); throw ErrorExtractor.searchPdfAsError(e, status); } } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java index feacc52d..8e03c4d4 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsTemplateCreator.java @@ -90,7 +90,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { //AffineTransform transform = pdfStructure.getAffineTransform(); // rectangle, formatter, image. /AcroForm/DR/XObject contains that form - this.pdfBuilder.createSignatureRectangle(pdSignatureField, properties, properties.getRotation()); + this.pdfBuilder.createSignatureRectangle(pdSignatureField, properties, properties.getRotation() + properties.getPageRotation()); this.pdfBuilder.createFormaterRectangle(properties.getFormaterRectangleParams()); PDRectangle formater = pdfStructure.getFormaterRectangle(); @@ -105,7 +105,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { // that is /AP entry the appearance dictionary. this.pdfBuilder.createAppearanceDictionary(pdfStructure.getHolderForm(), pdSignatureField, - properties.getRotation()); + properties.getRotation() + properties.getPageRotation()); // inner formstream, form and resource (hlder form containts inner form) this.pdfBuilder.createInnerFormStreamPdfAs(template); @@ -157,7 +157,7 @@ public class PDFAsTemplateCreator extends PDFTemplateCreator { } catch (COSVisitorException e) { - logger.error("COSVisitorException: can't get apereance stream ", e); + logger.warn("COSVisitorException: can't get apereance stream ", e); } logger.debug("stream returning started, size= " + in.available()); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index 826124ee..9c1adcec 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -257,7 +257,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { logger.debug("Strean of another form (inner form - it would be inside holder form) has been created"); } catch (Throwable e) { - logger.error("Failed to create visual signature block", e); + logger.warn("Failed to create visual signature block", e); throw new PdfAsException("Failed to create visual signature block", e); } } @@ -330,6 +330,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { PDPage page = new PDPage(); page.setMediaBox(new PDRectangle(properties.getPageWidth(), properties .getPageHeight())); + page.setRotation(properties.getPageRotation()); getStructure().setPage(page); logger.debug("PDF page has been created"); } @@ -339,7 +340,6 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { template.getDocumentCatalog().setAcroForm(theAcroForm); getStructure().setAcroForm(theAcroForm); logger.debug("Acro form page has been created"); - } public void createSignatureField(PDAcroForm acroForm) throws IOException { @@ -388,18 +388,55 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { Point2D llSrc = new Point2D.Float(); llSrc.setLocation(properties.getxAxis(), properties.getPageHeight() - properties.getyAxis() - properties.getHeight()); + + rect.setUpperRightX((float) upSrc.getX()); + rect.setUpperRightY((float) upSrc.getY()); + rect.setLowerLeftY((float) llSrc.getY()); + rect.setLowerLeftX((float) llSrc.getX()); + logger.debug("orig rectangle of signature has been created: {}", + rect.toString()); + AffineTransform transform = new AffineTransform(); transform.setToIdentity(); if (degrees % 360 != 0) { transform.setToRotation(Math.toRadians(degrees), llSrc.getX(), llSrc.getY()); } + + Point2D upDst = new Point2D.Float(); transform.transform(upSrc, upDst); Point2D llDst = new Point2D.Float(); transform.transform(llSrc, llDst); + + float xPos = properties.getxAxis(); + float yPos = properties.getPageHeight() - properties.getyAxis(); + logger.debug("POS {} x {}", xPos, yPos); + logger.debug("SIZE {} x {}", properties.getWidth(), properties.getHeight()); + // translate according to page! rotation + int pageRotation = properties.getPageRotation(); + AffineTransform translate = new AffineTransform(); + switch(pageRotation) { + case 90: + translate.setToTranslation(properties.getPageHeight() + - (properties.getPageHeight() + - properties.getyAxis()) - properties.getxAxis() + properties.getHeight(), properties.getxAxis() + properties.getHeight() - (properties.getPageHeight() + - properties.getyAxis())); + break; + case 180: + //translate.setToTranslation(properties.getPageWidth() - properties.getxAxis() - properties.getxAxis(), + // properties.getPageHeight() - properties.getyAxis() + properties.getHeight()); + translate.setToTranslation(properties.getPageWidth() - 2 * xPos,properties.getPageHeight() - 2 * (yPos - properties.getHeight())); + break; + case 270: + translate.setToTranslation(-properties.getHeight() + yPos - xPos, properties.getPageWidth() - (yPos - properties.getHeight()) - xPos); + break; + } + translate.transform(upDst, upDst); + translate.transform(llDst, llDst); + rect.setUpperRightX((float) upDst.getX()); rect.setUpperRightY((float) upDst.getY()); rect.setLowerLeftY((float) llDst.getY()); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureDesigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureDesigner.java index 17b02d9d..1a8bd38c 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureDesigner.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureDesigner.java @@ -55,6 +55,7 @@ public class PDFAsVisualSignatureDesigner { private float imageSizeInPercents; private PDDocument document = null; private int page = 0; + private int pageRotation = 0; private boolean newpage = false; PDFAsVisualSignatureProperties properties; @@ -93,13 +94,25 @@ public class PDFAsVisualSignatureDesigner { if(newpage) { PDPage lastPage = (PDPage) pages.get(pages.size()-1); PDRectangle mediaBox = lastPage.findMediaBox(); - this.pageHeight(mediaBox.getHeight()); - this.pageWidth = mediaBox.getWidth(); + pageRotation = lastPage.findRotation() % 360; + if(pageRotation == 90 || pageRotation == 270) { + this.pageHeight(mediaBox.getWidth()); + this.pageWidth = mediaBox.getHeight(); + } else { + this.pageHeight(mediaBox.getHeight()); + this.pageWidth = mediaBox.getWidth(); + } } else { PDPage firstPage = (PDPage) pages.get(page - 1); PDRectangle mediaBox = firstPage.findMediaBox(); - this.pageHeight(mediaBox.getHeight()); - this.pageWidth = mediaBox.getWidth(); + pageRotation = firstPage.findRotation() % 360; + if(pageRotation == 90 || pageRotation == 270) { + this.pageHeight(mediaBox.getWidth()); + this.pageWidth = mediaBox.getHeight(); + } else { + this.pageHeight(mediaBox.getHeight()); + this.pageWidth = mediaBox.getWidth(); + } } float x = this.pageWidth; float y = 0; @@ -405,6 +418,14 @@ public class PDFAsVisualSignatureDesigner { return pageHeight; } + /** + * + * @return page height + */ + public int getPageRotation() { + return pageRotation; + } + /** * get image size in percents * diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java index cb1dfc38..55a6d832 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java @@ -24,8 +24,11 @@ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; import java.io.IOException; +import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,8 +64,35 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties { PDDocument origDoc = object.getDocument(); designer = new PDFAsVisualSignatureDesigner(origDoc, pos.getPage(), this, pos.isMakeNewPage()); + List pages = origDoc.getDocumentCatalog().getAllPages(); + PDPage page = null; + if(pos.isMakeNewPage()) { + page = (PDPage) pages.get(pages.size()-1); + } else { + page = (PDPage) pages.get(pos.getPage() - 1); + } + logger.debug("PAGE width {} HEIGHT {}", designer.getPageWidth(), designer.getPageHeight()); + logger.debug("POS X {} Y {}", pos.getX(), pos.getY()); + int rot = page.findRotation(); float posy = designer.getPageHeight() - pos.getY(); - designer.coordinates(pos.getX(), posy); + float posx = pos.getX(); + /*switch (rot) { + case 90: // CW + posx = designer.getPageHeight() - pos.getY(); + posy = designer.getPageWidth() - main.getWidth(); + break; + case 180: + posy = pos.getY(); + posx = designer.getPageWidth() - pos.getX(); + break; + case 270: // CCW + posx = pos.getY(); + posy = designer.getPageWidth() - pos.getX(); + break; + }*/ + logger.debug("ROT {}", rot); + logger.debug("COORD X {} Y {}", posx, posy); + designer.coordinates(posx, posy); float[] form_rect = new float[] {0,0, main.getWidth() + 2, main.getHeight() + 2}; logger.debug("AP Rect: {} {} {} {}", form_rect[0], form_rect[1], form_rect[2], form_rect[3]); designer.formaterRectangleParams(form_rect); diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java index cc2912ea..ca363a8e 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PdfBoxVisualObject.java @@ -61,9 +61,9 @@ public class PdfBoxVisualObject implements IPDFVisualObject { try { table = new PDFBoxTable(abstractTable, null, this.width, settings); } catch (IOException e) { - logger.error("Failed to fix width of Table!", e); + logger.warn("Failed to fix width of Table!", e); } catch (PdfAsException e) { - logger.error("Failed to fix width of Table!", e); + logger.warn("Failed to fix width of Table!", e); } } diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java index 5156d846..1530ab03 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java @@ -251,7 +251,7 @@ public class TableDrawUtils { contentStream.endText(); } catch (IOException e) { - logger.error("IO Exception", e); + logger.warn("IO Exception", e); throw new PdfAsException("Error", e); } } @@ -285,7 +285,7 @@ public class TableDrawUtils { padding, abstractTable, doc, cell, fontSize, textHeight, valign, halign, tlines, textFont, formResources, settings); } catch (IOException e) { - logger.error("IO Exception", e); + logger.warn("IO Exception", e); throw new PdfAsException("Error", e); } } @@ -319,7 +319,7 @@ public class TableDrawUtils { padding, abstractTable, doc, cell, fontSize, textHeight, valign, halign, tlines, textFont, formResources, settings); } catch (IOException e) { - logger.error("IO Exception", e); + logger.warn("IO Exception", e); throw new PdfAsException("Error", e); } } @@ -336,7 +336,7 @@ public class TableDrawUtils { String img_ref = (String) cell.getValue(); if (!images.containsKey(img_ref)) { - logger.error("Image not prepared! : " + img_ref); + logger.warn("Image not prepared! : " + img_ref); throw new PdfAsException("Image not prepared! : " + img_ref); } ImageObject image = images.get(img_ref); @@ -375,7 +375,7 @@ public class TableDrawUtils { contentStream.drawXObject(pdImage, imgx, imgy - image.getHeight(), image.getWidth(), image.getHeight()); } catch (IOException e) { - logger.error("IO Exception", e); + logger.warn("IO Exception", e); throw new PdfAsException("Error", e); } -- cgit v1.2.3