How to delete an optional content group alongwith its content from pdf using pdfbox?

V K

I have implemented functionality to delete the layer from pdf, but the problem is that, the content that I had drawn on the layer, does not get delete.Here is the code that I am using to delete the layer:

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));

      }
V K

To delete the markup data , I hade to modify the PDPage's content.I just searched the contents for BDC and EMC pair, and then searched whether that pair belongs to the concerned layer, if so then I delete that part from the contents.Below is the C# code that I used:

                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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a PDF file from HTML using PDFBox?

From Dev

How to extract bold text from pdf using pdfbox?

From Dev

Using PHP to fetch content from a DOM node but not from its children

From Dev

How to create tags from one content to another content using PERL

From Dev

How to find and disable a content control by tag to delete it and its contents?

From Dev

How to put the ByteArrayInputStream content into a PDF using iText?

From Dev

Editing content in pdf using PDFBox removes last line from pdf

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

delete directory object and its content in powershell

From Dev

delete a folder and its content

From Dev

How to POST PDF content as application/pdf using cURL

From Dev

how to auto delete content using JavaScript or jQuery

From Dev

How to delete all the folder except one folder and its content using command prompt?

From Dev

Find tagged content in PDF/A-1a using pdfbox

From Dev

How do determine location of actual PDF content with PDFBox?

From Dev

How to read multi-line content between two words from a PDF file using java?

From Dev

How to check for Optional @ViewBuilder content

From Dev

How to write a big ISO file into a few disks for using its content?

From Dev

How to get page content height using pdfbox

From Dev

Save 'data-' attributes alongwith other html content

From Dev

Using image from sprite in list item after its' content

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How do I extract an icon class name and its content value from a css file using PHP?

From Dev

How to read the content of a .pdf file using nodejs?

From Dev

How do I make modifications to existing layer(Optional Content Group) in pdf?

From Dev

PDFBOX merge several PDF without render Optional Content Group visibility

From Dev

How to get parent group content from svg using javascript?

From Dev

How to set attribute to element from its content?

From Dev

How can I select an element using its text content?

Related Related

  1. 1

    How to create a PDF file from HTML using PDFBox?

  2. 2

    How to extract bold text from pdf using pdfbox?

  3. 3

    Using PHP to fetch content from a DOM node but not from its children

  4. 4

    How to create tags from one content to another content using PERL

  5. 5

    How to find and disable a content control by tag to delete it and its contents?

  6. 6

    How to put the ByteArrayInputStream content into a PDF using iText?

  7. 7

    Editing content in pdf using PDFBox removes last line from pdf

  8. 8

    How can I delete the "sheet" node keeping its content intact?

  9. 9

    delete directory object and its content in powershell

  10. 10

    delete a folder and its content

  11. 11

    How to POST PDF content as application/pdf using cURL

  12. 12

    how to auto delete content using JavaScript or jQuery

  13. 13

    How to delete all the folder except one folder and its content using command prompt?

  14. 14

    Find tagged content in PDF/A-1a using pdfbox

  15. 15

    How do determine location of actual PDF content with PDFBox?

  16. 16

    How to read multi-line content between two words from a PDF file using java?

  17. 17

    How to check for Optional @ViewBuilder content

  18. 18

    How to write a big ISO file into a few disks for using its content?

  19. 19

    How to get page content height using pdfbox

  20. 20

    Save 'data-' attributes alongwith other html content

  21. 21

    Using image from sprite in list item after its' content

  22. 22

    How can I delete the "sheet" node keeping its content intact?

  23. 23

    How do I extract an icon class name and its content value from a css file using PHP?

  24. 24

    How to read the content of a .pdf file using nodejs?

  25. 25

    How do I make modifications to existing layer(Optional Content Group) in pdf?

  26. 26

    PDFBOX merge several PDF without render Optional Content Group visibility

  27. 27

    How to get parent group content from svg using javascript?

  28. 28

    How to set attribute to element from its content?

  29. 29

    How can I select an element using its text content?

HotTag

Archive