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

为君一笑 提交于 2019-12-02 07:23:49

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!