pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1798498 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
Date Mon, 12 Jun 2017 18:02:00 GMT
Author: tilman
Date: Mon Jun 12 18:02:00 2017
New Revision: 1798498

URL: http://svn.apache.org/viewvc?rev=1798498&view=rev
Log:
PDFBOX-2852: use jdk7 syntax

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1798498&r1=1798497&r2=1798498&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
Mon Jun 12 18:02:00 2017
@@ -374,125 +374,125 @@ class AppearanceGeneratorHelper
                                            PDAppearanceStream appearanceStream,
                                            OutputStream output) throws IOException
     {
-        PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream,
output);
-        
-        PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
-        
-        // Acrobat calculates the left and right padding dependent on the offset of the border
edge
-        // This calculation works for forms having been generated by Acrobat.
-        // The minimum distance is always 1f even if there is no rectangle being drawn around.
-        float borderWidth = 0;
-        if (widget.getBorderStyle() != null)
-        {
-            borderWidth = widget.getBorderStyle().getWidth();
-        }
-        PDRectangle clipRect = applyPadding(bbox, Math.max(1f, borderWidth));
-        PDRectangle contentRect = applyPadding(clipRect, Math.max(1f, borderWidth));
-        
-        contents.saveGraphicsState();
-        
-        // Acrobat always adds a clipping path
-        contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(),
-                clipRect.getWidth(), clipRect.getHeight());
-        contents.clip();
-        
-        // get the font
-        PDFont font = defaultAppearance.getFont();
-        
-        // calculate the fontSize (because 0 = autosize)
-        float fontSize = defaultAppearance.getFontSize();
-        
-        if (fontSize == 0)
-        {
-            fontSize = calculateFontSize(font, contentRect);            
-        }
-        
-        // for a listbox generate the highlight rectangle for the selected
-        // options
-        if (field instanceof PDListBox)
-        {
-            insertGeneratedSelectionHighlight(contents, appearanceStream, font, fontSize);
-        }
-        
-        // start the text output
-        contents.beginText();
-
-        // write the /DA string
-        defaultAppearance.writeTo(contents, fontSize);
-       
-        // calculate the y-position of the baseline
-        float y;
-        
-        // calculate font metrics at font size
-        float fontScaleY = fontSize / FONTSCALE;
-        float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY;
-        float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY;
-        float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY;
-        
-        if (field instanceof PDTextField && ((PDTextField) field).isMultiline())
-        {
-            y = contentRect.getUpperRightY() - fontBoundingBoxAtSize;
-        }
-        else
+        try (PDAppearanceContentStream contents = new PDAppearanceContentStream(appearanceStream,
output))
         {
-            // Adobe shows the text 'shiftet up' in case the caps don't fit into the clipping
area
-            if (fontCapAtSize > clipRect.getHeight())
+            PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
+            
+            // Acrobat calculates the left and right padding dependent on the offset of the
border edge
+            // This calculation works for forms having been generated by Acrobat.
+            // The minimum distance is always 1f even if there is no rectangle being drawn
around.
+            float borderWidth = 0;
+            if (widget.getBorderStyle() != null)
+            {
+                borderWidth = widget.getBorderStyle().getWidth();
+            }
+            PDRectangle clipRect = applyPadding(bbox, Math.max(1f, borderWidth));
+            PDRectangle contentRect = applyPadding(clipRect, Math.max(1f, borderWidth));
+            
+            contents.saveGraphicsState();
+            
+            // Acrobat always adds a clipping path
+            contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(),
+                    clipRect.getWidth(), clipRect.getHeight());
+            contents.clip();
+            
+            // get the font
+            PDFont font = defaultAppearance.getFont();
+            
+            // calculate the fontSize (because 0 = autosize)
+            float fontSize = defaultAppearance.getFontSize();
+            
+            if (fontSize == 0)
+            {
+                fontSize = calculateFontSize(font, contentRect);
+            }
+            
+            // for a listbox generate the highlight rectangle for the selected
+            // options
+            if (field instanceof PDListBox)
             {
-                y = clipRect.getLowerLeftY() + -fontDescentAtSize;
+                insertGeneratedSelectionHighlight(contents, appearanceStream, font, fontSize);
+            }
+            
+            // start the text output
+            contents.beginText();
+            
+            // write the /DA string
+            defaultAppearance.writeTo(contents, fontSize);
+            
+            // calculate the y-position of the baseline
+            float y;
+            
+            // calculate font metrics at font size
+            float fontScaleY = fontSize / FONTSCALE;
+            float fontBoundingBoxAtSize = font.getBoundingBox().getHeight() * fontScaleY;
+            float fontCapAtSize = font.getFontDescriptor().getCapHeight() * fontScaleY;
+            float fontDescentAtSize = font.getFontDescriptor().getDescent() * fontScaleY;
+            
+            if (field instanceof PDTextField && ((PDTextField) field).isMultiline())
+            {
+                y = contentRect.getUpperRightY() - fontBoundingBoxAtSize;
             }
             else
             {
-                // calculate the position based on the content rectangle
-                y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize) / 2;
-    
-                // check to ensure that ascents and descents fit
-                if (y - clipRect.getLowerLeftY() < -fontDescentAtSize) {
-    
-                    float fontDescentBased = -fontDescentAtSize + contentRect.getLowerLeftY();
-                    float fontCapBased = contentRect.getHeight() - contentRect.getLowerLeftY()
- fontCapAtSize;
-    
-                    y = Math.min(fontDescentBased, Math.max(y, fontCapBased));
+                // Adobe shows the text 'shiftet up' in case the caps don't fit into the
clipping area
+                if (fontCapAtSize > clipRect.getHeight())
+                {
+                    y = clipRect.getLowerLeftY() + -fontDescentAtSize;
+                }
+                else
+                {
+                    // calculate the position based on the content rectangle
+                    y = clipRect.getLowerLeftY() + (clipRect.getHeight() - fontCapAtSize)
/ 2;
+                    
+                    // check to ensure that ascents and descents fit
+                    if (y - clipRect.getLowerLeftY() < -fontDescentAtSize) {
+                        
+                        float fontDescentBased = -fontDescentAtSize + contentRect.getLowerLeftY();
+                        float fontCapBased = contentRect.getHeight() - contentRect.getLowerLeftY()
- fontCapAtSize;
+                        
+                        y = Math.min(fontDescentBased, Math.max(y, fontCapBased));
+                    }
                 }
             }
+            
+            // show the text
+            float x = contentRect.getLowerLeftX();
+            
+            // special handling for comb boxes as these are like table cells with individual
+            // chars
+            if (shallComb()) {
+                insertGeneratedCombAppearance(contents, appearanceStream, font, fontSize);
+            }
+            else if (field instanceof PDListBox)
+            {
+                insertGeneratedListboxAppearance(contents, appearanceStream, contentRect,
font, fontSize);
+            }
+            else
+            {
+                PlainText textContent = new PlainText(value);
+                AppearanceStyle appearanceStyle = new AppearanceStyle();
+                appearanceStyle.setFont(font);
+                appearanceStyle.setFontSize(fontSize);
+                
+                // Adobe Acrobat uses the font's bounding box for the leading between the
lines
+                appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY);
+                
+                PlainTextFormatter formatter = new PlainTextFormatter
+                        .Builder(contents)
+                        .style(appearanceStyle)
+                        .text(textContent)
+                        .width(contentRect.getWidth())
+                        .wrapLines(isMultiLine())
+                        .initialOffset(x, y)
+                        .textAlign(field.getQ())
+                        .build();
+                formatter.format();
+            }
+            
+            contents.endText();
+            contents.restoreGraphicsState();
         }
-        
-        // show the text
-        float x = contentRect.getLowerLeftX();
-        
-        // special handling for comb boxes as these are like table cells with individual
-        // chars
-        if (shallComb()) {
-            insertGeneratedCombAppearance(contents, appearanceStream, font, fontSize);
-        }
-        else if (field instanceof PDListBox)
-        {
-            insertGeneratedListboxAppearance(contents, appearanceStream, contentRect, font,
fontSize);
-        }
-        else
-        {         
-            PlainText textContent = new PlainText(value);
-            AppearanceStyle appearanceStyle = new AppearanceStyle();
-            appearanceStyle.setFont(font);
-            appearanceStyle.setFontSize(fontSize);
-            
-            // Adobe Acrobat uses the font's bounding box for the leading between the lines
-            appearanceStyle.setLeading(font.getBoundingBox().getHeight() * fontScaleY);
-            
-            PlainTextFormatter formatter = new PlainTextFormatter
-                                                .Builder(contents)
-                                                    .style(appearanceStyle)
-                                                    .text(textContent)
-                                                    .width(contentRect.getWidth())
-                                                    .wrapLines(isMultiLine())
-                                                    .initialOffset(x, y)
-                                                    .textAlign(field.getQ())
-                                                    .build();
-            formatter.format();
-        }
-    
-        contents.endText();
-        contents.restoreGraphicsState();
-        contents.close();
     }
     
     private AffineTransform calculateMatrix(PDRectangle bbox, int rotation)
@@ -699,9 +699,10 @@ class AppearanceGeneratorHelper
      */
     private void writeToStream(byte[] data, PDAppearanceStream appearanceStream) throws IOException
     {
-        OutputStream out = appearanceStream.getCOSObject().createOutputStream();
-        out.write(data);
-        out.close();
+        try (OutputStream out = appearanceStream.getCOSObject().createOutputStream())
+        {
+            out.write(data);
+        }
     }
 
     /**



Mime
View raw message