2017-07-12 5 views
0

Ich habe Funktionalität implementiert, um den Layer aus PDF zu löschen, aber das Problem ist, dass der Inhalt, den ich auf dem Layer gezeichnet habe, nicht gelöscht wird. Hier ist der Code, den ich benutze, um den Layer zu löschen:Wie lösche ich eine optionale Inhaltsgruppe zusammen mit ihrem Inhalt von pdf mit pdfbox?

PDDocumentCatalog documentCatalog = doc.getDocumentCatalog(); 
PDOptionalContentProperties ocgProps = documentCatalog.getOCProperties(); 
PDOptionalContentGroup ocg = ocgProps.getGroup(markupLayerName); 

    COSDictionary ocgsDict = (COSDictionary)ocgProps.getCOSObject(); 
    COSArray ocgs = (COSArray)ocgsDict.getItem(COSName.OCGS); 
    int indexToBeDeleted = -1; 
    for (int index = 0; index < ocgs.size(); index++) 
    { 
     COSBase o = ocgs.get(index); 
     COSDictionary ocgDict = ToCOSDictionary(o); 
      if (ocgDict.getString(COSName.NAME) == markupLayerName) 
      { 
       indexToBeDeleted = index; 
       break; 
      } 
    } 
    if (indexToBeDeleted >= 0) 
    { 
     cgs.remove(indexToBeDeleted); 
     ocgsDict.setItem(COSName.OCGS, ocgs); 
     documentCatalog.setOCProperties(new PDOptionalContentProperties(ocgsDict)); 

     } 
+0

Hat Ihre andere Frage diese Frage nicht beantwortet? –

Antwort

0

Um die Markup-Daten zu löschen, musste ich den Inhalt der PDPage ändern.Ich habe gerade den Inhalt nach BDC und EMC-Paar durchsucht und dann gesucht, ob das Paar zu der betroffenen Ebene gehört, wenn ja, dann lösche ich diesen Teil aus Inhalt. Nachstehend ist der C# -Code, den ich verwendet habe:

   PDPage page = (PDPage)doc.getDocumentCatalog().getPages().get(pageNum); 
       PDResources resources = page.getResources(); 
       PDFStreamParser parser = new PDFStreamParser(page); 
       parser.parse(); 
       java.util.Collection tokens = parser.getTokens(); 
       java.util.List newTokens = new java.util.ArrayList(); 
       List<Tuple<int, int>> deletionIndexList = new List<Tuple<int, int>>(); 
       object[] tokensArray = tokens.toArray(); 
       for (int index = 0; index < tokensArray.Count(); index++) 
       { 
        object obj = tokensArray[index]; 
        if (obj is COSName && (((COSName)obj) == COSName.OC)) 
        { 
         int startIndex = index; 
         index++; 
         if (index < tokensArray.Count()) 
         { 
          obj = tokensArray[index]; 
          if (obj is COSName) 
          { 
           PDPropertyList prop = resources.getProperties((COSName)obj);//Check if the COSName found is the resource name of layer which contains the markup to be deleted. 
           if (prop != null && (prop is PDOptionalContentGroup)) 
           { 
            if (((PDOptionalContentGroup)prop).getName() == markupLayerName) 
            { 
             index++; 
             if (index < tokensArray.Count()) 
             { 
              obj = tokensArray[index]; 
              if (obj is Operator && ((Operator)obj).getName() == "BDC")//Check if the token specifies the start of markup 
              { 

               int endIndex = -1; 
               index++; 
               while (index < tokensArray.Count()) 
               { 
                obj = tokensArray[index]; 
                if (obj is Operator && ((Operator)obj).getName() == "EMC")//Check if the token specifies the end of markup 
                { 
                 endIndex = index; 
                 break; 
                } 
                index++; 
               } 
               if (endIndex >= 0) 
               { 
                deletionIndexList.Add(new Tuple<int, int>(startIndex, endIndex)); 
               } 
              } 

             } 
            } 
           } 
          } 
         } 
        } 
       } 
       int tokensListIndex = 0; 
       for (int index = 0; index < deletionIndexList.Count(); index++) 
       { 
        Tuple<int, int> indexes = deletionIndexList.ElementAt(index); 
        while (tokensListIndex < indexes.Item1) 
        { 
         newTokens.add(tokensArray[tokensListIndex]); 
         tokensListIndex++; 
        } 
        tokensListIndex = indexes.Item2 + 1; 
       } 
       while (tokensListIndex < tokensArray.Count()) 
       { 
        newTokens.add(tokensArray[tokensListIndex]); 
        tokensListIndex++; 
       } 
       PDStream newContents = new PDStream(doc); 
       OutputStream output = newContents.createOutputStream(COSName.FLATE_DECODE); 
       ContentStreamWriter writer = new ContentStreamWriter(output); 
       writer.writeTokens(newTokens); 
       output.close(); 
       page.setContents(newContents); 
+0

Ihre Verwendung von '==' für String-Vergleich lässt mich fragen, ob das im Allgemeinen funktioniert ... – mkl

+0

@mkl das ist ein C# -Code, und es funktioniert. Ich habe es mehrmals getestet. Vielleicht beziehen Sie sich auf Java, –

+0

Ah, ok. Wahrscheinlich sollten Sie angeben, dass der häufigste Kontext, in dem PDFBox verwendet wird, Java ist. – mkl

Verwandte Themen