2017-04-11 3 views
3

내 PDF를 만들 때 처음에는 체크 상자에 아무런 문제가 없습니다. 내가 클릭 한 후 (onBlur) 어떤 종류의 그늘이 보이고 초점이 맞춰지면 더 이상 보이지 않습니다. 어떻게하면 항상 보이게 할 수 있습니까? 그리고 어떤 종류의 테두리를 어떻게 추가 할 수 있습니까 (직사각형 클래스로 수동으로하지 않고)?확인란에 테두리를 추가하여 항상 보이게 만드는 방법

enter image description here

public class CheckBoxWriter { 

    public static void main(String[] args) throws IOException { 

     PDDocument document = new PDDocument(); 
     PDPage page = new PDPage(); 
     document.addPage(page); 

     // just a rectangle to be able to locate the checkbox 
     PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false); 
     contentStream.addRect(100, 100, 50, 50); 
     contentStream.setLineWidth(1); 
     contentStream.setNonStrokingColor(Color.WHITE); 
     contentStream.setStrokingColor(Color.BLACK); 
     contentStream.stroke(); 
     contentStream.close(); 

     PDAcroForm acroForm = new PDAcroForm(document); 
     PDCheckBox checkBox = new PDCheckBox(acroForm); 
     checkBox.setPartialName("xxx"); 

     PDAnnotationWidget widget = new PDAnnotationWidget(); 
     widget.setRectangle(new PDRectangle(100, 100, 10, 10)); 
     widget.setAnnotationFlags(4); 
     widget.setPage(page); 
     widget.setParent(checkBox); 

     List<PDAnnotationWidget> widgets = new ArrayList<>(); 
     widgets.add(widget); 
     page.getAnnotations().add(widget); 
     checkBox.setWidgets(widgets); 

     acroForm.getFields().add(checkBox); 
     document.getDocumentCatalog().setAcroForm(acroForm); 

     File file = new File("test.pdf"); 

     document.save(file); 
     document.close(); 
    } 
} 
+0

체크 표시 대신 "x"가있는 해결책이 유용할까요? –

+1

예. 의도적으로 체크 표시를 선택하지 않았습니다. – user489872

답변

3

하면이 시도 - 그것은 "예"및 "Off"로 표시 체크 상자 모양 스트림, 즉 콘텐츠 스트림을 생성한다. 또한 위젯을 사용하는 방식을 수정하여 파일이 더 간단하게 만들었습니다. 사용한 전략 (필드와 별도의 위젯)은 한 필드에 여러 위젯이있는 경우에만 유용합니다.

운영자가 수행하는 작업 (횡설수설)을 보려면 PDF specification을보고 "운영자 요약"을 검색하십시오.

public static void main(String[] args) throws IOException 
{ 
    PDDocument document = new PDDocument(); 
    PDPage page = new PDPage(); 
    document.addPage(page); 

    // just a rectangle to be able to locate the checkbox 
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false); 
    contentStream.addRect(100, 100, 50, 50); 
    contentStream.setLineWidth(1); 
    contentStream.setNonStrokingColor(Color.WHITE); 
    contentStream.setStrokingColor(Color.BLACK); 
    contentStream.stroke(); 
    contentStream.close(); 

    PDAcroForm acroForm = new PDAcroForm(document); 
    PDCheckBox checkBox = new PDCheckBox(acroForm); 
    checkBox.setPartialName("xxx"); 


    //PDAnnotationWidget widget = new PDAnnotationWidget(); 
    PDAnnotationWidget widget = checkBox.getWidgets().get(0); 
    widget.setRectangle(new PDRectangle(100, 100, 10, 10)); 
    widget.setAnnotationFlags(4); 
    widget.setPage(page); 
    //widget.setParent(checkBox); 

    // inspired by annot 92 of file from PDFBOX-563 
    // annot 172 has "checkmark" instead, but more more complex, needs ZaDb 

    String offNString = "1 g\n" 
      + "0 0 9.5 9.5 re\n" 
      + "f\n" 
      + "0.5 0.5 9.5 9.5 re\n" 
      + "s"; 
    String offDString = "0.75 g\n" 
      + "0 0 9.5 9.5 re\n" 
      + "f\n" 
      + "0.5 0.5 9.5 9.5 re\n" 
      + "s"; 
    String yesNString = "1 g\n" 
      + "0 0 9 9.5 re\n" 
      + "f\n" 
      + "0.5 0.5 9.5 9.5 re\n" 
      + "s\n" 
      + "q\n" 
      + " 1 1 9 9 re\n" 
      + " W\n" 
      + " n\n" 
      + " 2 8 m\n" 
      + " 8 2 l\n" 
      + " 8 8 m\n" 
      + " 2 2 l\n" 
      + " s\n" 
      + "Q"; 
    String yesDString = "0.75 g\n" 
      + "0 0 9 9.5 re\n" 
      + "f\n" 
      + "0.5 0.5 9.5 9.5 re\n" 
      + "s\n" 
      + "q\n" 
      + " 1 1 9 9 re\n" 
      + " W\n" 
      + " n\n" 
      + " 2 8 m\n" 
      + " 8 2 l\n" 
      + " 8 8 m\n" 
      + " 2 2 l\n" 
      + " s\n" 
      + "Q"; 

    COSDictionary apNDict = new COSDictionary(); 
    COSStream offNStream = new COSStream(); 
    offNStream.setItem(COSName.BBOX, new PDRectangle(10, 10)); 
    offNStream.setItem(COSName.FORMTYPE, COSInteger.ONE); 
    offNStream.setItem(COSName.TYPE, COSName.XOBJECT); 
    offNStream.setItem(COSName.SUBTYPE, COSName.FORM); 
    offNStream.setItem(COSName.MATRIX, new Matrix().toCOSArray()); 
    offNStream.setItem(COSName.RESOURCES, new COSDictionary()); 
    OutputStream os = offNStream.createOutputStream(); 
    os.write(offNString.getBytes()); 
    os.close(); 
    apNDict.setItem(COSName.Off, offNStream); 

    COSStream yesNStream = new COSStream(); 
    yesNStream.setItem(COSName.BBOX, new PDRectangle(10, 10)); 
    yesNStream.setItem(COSName.FORMTYPE, COSInteger.ONE); 
    yesNStream.setItem(COSName.TYPE, COSName.XOBJECT); 
    yesNStream.setItem(COSName.SUBTYPE, COSName.FORM); 
    yesNStream.setItem(COSName.MATRIX, new Matrix().toCOSArray()); 
    yesNStream.setItem(COSName.RESOURCES, new COSDictionary()); 
    os = yesNStream.createOutputStream(); 
    os.write(yesNString.getBytes()); 
    os.close(); 
    apNDict.setItem(COSName.getPDFName("Yes"), yesNStream); 

    COSDictionary apDDict = new COSDictionary(); 
    COSStream offDStream = new COSStream(); 
    offDStream.setItem(COSName.BBOX, new PDRectangle(16, 16)); 
    offDStream.setItem(COSName.FORMTYPE, COSInteger.ONE); 
    offDStream.setItem(COSName.TYPE, COSName.XOBJECT); 
    offDStream.setItem(COSName.SUBTYPE, COSName.FORM); 
    offDStream.setItem(COSName.MATRIX, new Matrix().toCOSArray()); 
    offDStream.setItem(COSName.RESOURCES, new COSDictionary()); 
    os = offDStream.createOutputStream(); 
    os.write(offDString.getBytes()); 
    os.close(); 
    apDDict.setItem(COSName.Off, offDStream); 

    COSStream yesDStream = new COSStream(); 
    yesDStream.setItem(COSName.BBOX, new PDRectangle(16, 16)); 
    yesDStream.setItem(COSName.FORMTYPE, COSInteger.ONE); 
    yesDStream.setItem(COSName.TYPE, COSName.XOBJECT); 
    yesDStream.setItem(COSName.SUBTYPE, COSName.FORM); 
    yesDStream.setItem(COSName.MATRIX, new Matrix().toCOSArray()); 
    yesDStream.setItem(COSName.RESOURCES, new COSDictionary()); 
    os = yesDStream.createOutputStream(); 
    os.write(yesDString.getBytes()); 
    os.close(); 
    apDDict.setItem(COSName.getPDFName("Yes"), yesDStream); 

    PDAppearanceDictionary appearance = new PDAppearanceDictionary(); 
    PDAppearanceEntry appearanceNEntry = new PDAppearanceEntry(apNDict); 
    appearance.setNormalAppearance(appearanceNEntry); 

    // push appearance doesn't look nice enough, has weird effect when pushing 
//  PDAppearanceEntry appearanceDEntry = new PDAppearanceEntry(apDDict); 
//  appearance.setDownAppearance(appearanceDEntry); 

    widget.setAppearance(appearance); 

    // MK (appearance characteristics dictionary) 
    COSDictionary acdDict = new COSDictionary(); 
    acdDict.setItem(COSName.CA, new COSString("8")); // 8 is X, 4 is checkmark 
    COSArray bcArray = new COSArray(); 
    bcArray.add(COSInteger.ZERO); 
    acdDict.setItem(COSName.BC, bcArray); 
    COSArray bgArray = new COSArray(); 
    bgArray.add(COSInteger.ONE); 
    acdDict.setItem(COSName.BG, bgArray); 
    PDAppearanceCharacteristicsDictionary acd = new PDAppearanceCharacteristicsDictionary(acdDict); 
    widget.setAppearanceCharacteristics(acd); 

//  List<PDAnnotationWidget> widgets = new ArrayList<>(); 
//  widgets.add(widget); 
    page.getAnnotations().add(widget); 
//  checkBox.setWidgets(widgets); 

    checkBox.setValue("Yes"); 

    acroForm.getFields().add(checkBox); 
    document.getDocumentCatalog().setAcroForm(acroForm); 

    File file = new File("checkbox.pdf"); 

    document.save(file); 
    document.close(); 
} 
+0

완벽하게 작동합니다. – user489872

관련 문제