title: Creating PDF push buttons with PDFBox 2.0.5 that shows/hides another button on the page. Only the last set created works properly
body: <p>The goal of this code is to have a thumbnail image on the page, and when hovered over by the mouse it shows the image in full size. The code here works with the major caveat that only the final thumbnail-full image combo created will work unless in Adobe Acrobat Pro, I go to Tools > Interactive Objects > Add Button. then all of the buttons visible on the screen will work. If I pass the mouse over any thumbnail, then the large button associated with it is removed from the opened document. I have tried setNeedAppearances(true) and the actions will work for all combos, but the widget formatting is removed. Attached below is my test code. Any help would be greatly appreciated. Thanks.</p>
  
  <pre><code>import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;
  
  import java.io.IOException;
  
  public class TestMain {
  
      public static void main(String[] args) throws IOException {
          PDDocument document = new PDDocument();
          PDPage page = new PDPage();
          document.addPage(page);
  
          float[] location = new float[]{100 , 600 , 60 , 80};
          TestButton.makeButton(document , page , location , "1.jpg");
  
          float[] location2 = new float[]{100 , 400 , 60 , 80};
          TestButton.makeButton(document , page , location2 , "2.jpg");
  
          document.save("mytest.pdf");
          document.close();
  
      }
  }
  </code></pre>
  
  <pre><code>import org.apache.pdfbox.cos.COSDictionary;
  import org.apache.pdfbox.cos.COSName;
  import org.apache.pdfbox.cos.COSString;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;
  import org.apache.pdfbox.pdmodel.PDPageContentStream;
  import org.apache.pdfbox.pdmodel.PDResources;
  import org.apache.pdfbox.pdmodel.common.PDRectangle;
  import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
  import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
  import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
  import org.apache.pdfbox.pdmodel.interactive.action.PDActionHide;
  import org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions;
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
  import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
  import org.apache.pdfbox.pdmodel.interactive.form.PDPushButton;
  
  import java.io.IOException;
  import java.util.ArrayList;
  
  public class TestButton {
  
      public static void makeButton(PDDocument document , PDPage page , float[] thumbLocation, String imageName) throws IOException {
          PDImageXObject image = PDImageXObject.createFromFile("src//main/resources/" + imageName, document);
          // thumbLocation array
          float thumbX = thumbLocation[0];
          float thumbY = thumbLocation[1];
          float thumbWidth = thumbLocation[2];
          float thumbHeight = thumbLocation[3];
  
          float lowX = page.getMediaBox().getLowerLeftX();
          float lowY = page.getMediaBox().getLowerLeftY();
          float highY = page.getMediaBox().getUpperRightY();
  
          float fullX = thumbX - image.getWidth() - 10;
          if (fullX &lt; lowX + 10){
              fullX = thumbX + thumbWidth + 10;
          }
          float fullY = (thumbY + thumbHeight / 2) - (image.getHeight() / 2);
          if (fullY &lt; lowY + 10){
              fullY = thumbY + thumbHeight + 10;
          }
          if (fullY + image.getHeight() &gt; highY - 10){
              fullY = thumbY - image.getHeight() - 10;
          }
          float fullWidth = image.getWidth();
          float fullHeight = image.getHeight();
  
          String thumbImage = imageName + "_Thumb";
          String fullImage = imageName + "_Full";
          PDColor colourBlack = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
  
          // Set up thumbnail
          COSDictionary thumbDict = new COSDictionary();
          PDAcroForm thumbAcro = new PDAcroForm(document , thumbDict);
          document.getDocumentCatalog().setAcroForm(thumbAcro);
          thumbAcro.setFields(new ArrayList&lt;&gt;());
  
          PDPushButton thumbButton = new PDPushButton(thumbAcro);
          thumbButton.setPartialName(thumbImage);
          // Thumbnail actions
          thumbDict.setItem(COSName.T, new COSString(fullImage));
  
          PDActionHide actionHide = new PDActionHide();
          actionHide.setT(thumbDict);
  
          PDActionHide actionShow = new PDActionHide();
          actionShow.setT(thumbDict);
          actionShow.setH(false);
  
          PDAnnotationAdditionalActions additionalActions = new PDAnnotationAdditionalActions();
          additionalActions.setE(actionShow);
          additionalActions.setX(actionHide);
          // Thumbnail widget
          PDAnnotationWidget thumbWidget = thumbButton.getWidgets().get(0);
          thumbWidget.setActions(additionalActions);
          thumbWidget.setRectangle(new PDRectangle(thumbX, thumbY, thumbWidth, thumbHeight));
          // Thumbnail appearance
          PDAppearanceDictionary thumbAppearanceDict = new PDAppearanceDictionary();
          PDAppearanceStream thumbAppearanceStream = new PDAppearanceStream(document);
          thumbAppearanceStream.setResources(new PDResources());
          PDAppearanceCharacteristicsDictionary thumbFieldAppearance
                  = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
          thumbFieldAppearance.setBorderColour(colourBlack);
          thumbWidget.setAppearanceCharacteristics(thumbFieldAppearance);
          try (PDPageContentStream thumbContent = new PDPageContentStream(document, thumbAppearanceStream))
          {
              PDRectangle thumbBox = new PDRectangle(
                      thumbWidget.getRectangle().getWidth(), thumbWidget.getRectangle().getHeight());
              thumbAppearanceStream.setBBox(thumbBox);
              thumbContent.setNonStrokingColor(0, 0, 0);
              thumbContent.setLineWidth(0.5f);
              thumbContent.addRect(thumbBox.getLowerLeftX() + 0.5f, thumbBox.getLowerLeftY() + 0.5f,
                      thumbBox.getWidth() - 1, thumbBox.getHeight() - 1);
              thumbContent.stroke();
              thumbContent.drawImage(image , thumbBox.getLowerLeftX() + 0.5f, thumbBox.getLowerLeftY() + 0.5f,
                      thumbBox.getWidth() - 1, thumbBox.getHeight() - 1);
          }
          thumbAppearanceDict.setNormalAppearance(thumbAppearanceStream);
          thumbWidget.setAppearance(thumbAppearanceDict);
          thumbWidget.setHidden(false);
          thumbWidget.setPrinted(true);
  
          page.getAnnotations().add(thumbWidget);
          thumbAcro.getFields().add(thumbButton);
  
          // Set up full
          COSDictionary fullDict = new COSDictionary();
          PDAcroForm fullAcro = new PDAcroForm(document , fullDict);
          document.getDocumentCatalog().setAcroForm(fullAcro);
          fullAcro.setFields(new ArrayList&lt;&gt;());
  
          PDPushButton fullButton = new PDPushButton(fullAcro);
          fullButton.setPartialName(fullImage);
          // Full widget
          PDAnnotationWidget fullWidget = fullButton.getWidgets().get(0);
          fullWidget.setRectangle(new PDRectangle(fullX - 1 , fullY - 1, fullWidth + 2 , fullHeight + 2));
          // Full appearance
          PDAppearanceDictionary fullAppearanceDict = new PDAppearanceDictionary();
          PDAppearanceStream fullAppearanceStream = new PDAppearanceStream(document);
          fullAppearanceStream.setResources(new PDResources());
          PDAppearanceCharacteristicsDictionary fullFieldAppearance
                  = new PDAppearanceCharacteristicsDictionary(new COSDictionary());
          fullFieldAppearance.setBorderColour(colourBlack);
          fullWidget.setAppearanceCharacteristics(fullFieldAppearance);
          try (PDPageContentStream fullContent = new PDPageContentStream(document, fullAppearanceStream))
          {
              PDRectangle fullBox = new PDRectangle(
                      fullWidget.getRectangle().getWidth(), fullWidget.getRectangle().getHeight());
              fullAppearanceStream.setBBox(fullBox);
              fullContent.setNonStrokingColor(0, 0, 0);
              fullContent.setLineWidth(1);
              fullContent.addRect(fullBox.getLowerLeftX() + 1, fullBox.getLowerLeftY() + 1,
                      fullBox.getWidth() - 2, fullBox.getHeight() - 2);
              fullContent.stroke();
              fullContent.drawImage(image, fullBox.getLowerLeftX() + 1, fullBox.getLowerLeftY() + 1,
                      fullBox.getWidth() - 2, fullBox.getHeight() - 2);
          }
          fullAppearanceDict.setNormalAppearance(fullAppearanceStream);
          fullWidget.setAppearance(fullAppearanceDict);
          fullWidget.setHidden(true);
          fullWidget.setPrinted(false);
  
          page.getAnnotations().add(fullWidget);
          fullAcro.getFields().add(fullButton);
      }
  }
  </code></pre>