Пример #1
0
        /**
         * Processes PDFObjects and returns words
         *
         */
        public List <Words> GetWords(PDFTextPageObject TextObject, List <Words> WordObjects)
        {
            Words              Word;
            bool               firstCharacter;
            FontsProcessor     fontsProcessor  = new FontsProcessor();
            PDFGlyphCollection GlyphCollection = TextObject.Glyphs;

            if (WordObjects.Count == 0 || WordObjects[WordObjects.Count - 1].WordComplete)
            {
                firstCharacter = true;
                Word           = new Words();
            }
            else
            {
                firstCharacter = false;
                Word           = WordObjects[WordObjects.Count - 1];
                Word.Added     = false;
                WordObjects.RemoveAt(WordObjects.Count - 1);
            }

            for (int i = 0; i < GlyphCollection.Count; i++)
            {
                if (String.IsNullOrWhiteSpace(GlyphCollection[i].Text))
                {
                    firstCharacter = true;

                    if (!String.IsNullOrEmpty(Word.Word))
                    {
                        Word.WordComplete = true;

                        if (!Word.Added)
                        {
                            Word.Added = true;
                            WordObjects.Add(Word);
                        }

                        Word = new Words();
                    }
                }
                else
                {
                    Word.Word = Word.Word + GlyphCollection[i].Text;

                    if (firstCharacter)
                    {
                        firstCharacter = false;

                        Word.FontBase  = fontsProcessor.GetFontBase(TextObject.FontName, TextObject.FontSize);
                        Word.FontBrush = new PDFBrush(TextObject.FillColor);
                        Word.Top       = GlyphCollection[i].DisplayBounds.Top;
                        Word.Left      = GlyphCollection[i].DisplayBounds.Left;
                    }
                }
            }

            if (Word.Word != "")
            {
                Word.Added = true;
                WordObjects.Add(Word);
            }

            return(WordObjects);
        }
Пример #2
0
        /**
         * Processes PDFObjects and returns blocks
         *
         */
        public List <Blocks> GetBlocks(PDFTextPageObject TextObject, List <Blocks> BlockList)
        {
            Blocks             block;
            bool               firstCharacter;
            FontsProcessor     fontsProcessor  = new FontsProcessor();
            PDFGlyphCollection GlyphCollection = TextObject.Glyphs;

            if (BlockList.Count == 0 || BlockList[BlockList.Count - 1].BlockComplete)
            {
                firstCharacter = true;
                block          = new Blocks();
            }
            else
            {
                firstCharacter = false;
                block          = BlockList[BlockList.Count - 1];
                BlockList.RemoveAt(BlockList.Count - 1);
            }

            for (int i = 0; i < GlyphCollection.Count; i++)
            {
                block.Block = block.Block + GlyphCollection[i].Text;

                if (block.Block.Length == 16)
                {
                    block.BlockComplete = true;
                    BlockList.Add(block);

                    if (++i >= GlyphCollection.Count)
                    {
                        return(BlockList);
                    }

                    firstCharacter = true;
                    block          = new Blocks();
                    block.Block    = block.Block + GlyphCollection[i].Text;
                }

                if (firstCharacter)
                {
                    firstCharacter = false;

                    block.FontBase  = fontsProcessor.GetFontBase(TextObject.FontName, TextObject.FontSize);
                    block.FontBrush = new PDFBrush(TextObject.FillColor);
                    block.Top.Add(GlyphCollection[i].DisplayBounds.Top);
                    block.Left.Add(GlyphCollection[i].DisplayBounds.Left);
                }
                else
                {
                    if (GlyphCollection[i].DisplayBounds.Top > block.TopOfLastCharacter)
                    {
                        block.Chunks.Add(block.Block.Length - 1);
                        block.Top.Add(GlyphCollection[i].DisplayBounds.Top);
                        block.Left.Add(GlyphCollection[i].DisplayBounds.Left);
                    }
                }

                block.TopOfLastCharacter = GlyphCollection[i].DisplayBounds.Top;
            }

            BlockList.Add(block);

            return(BlockList);
        }
Пример #3
0
        /**
         * Recontructs the encrypted shares of the PDF to get original PDF.
         *  Hightlights every occurrences of the word that a user has searched for.
         *
         */
        public void SharesReconstructor()
        {
            List <List <PDFPageObjectCollection> > pDFPageObjects = ExtractObjects();
            List <PDFPageObjectCollection>         ObjectsPerPage;
            PDFBrush redBrush = new PDFBrush(new PDFRgbColor(204, 255, 51));
            PDFPen   pen      = new PDFPen(new PDFRgbColor(204, 255, 51), 1);

            string[] shares;

            for (int pageIndex = 0; pageIndex < pDFPageObjects.Count; pageIndex++)
            {
                _decryptedPDF.AddPage();
                ObjectsPerPage = pDFPageObjects[pageIndex];

                for (int wordIndex = 0; wordIndex < ObjectsPerPage[0].Count; wordIndex++)
                {
                    shares = new string[_shareforRec];
                    PDFTextPageObject TextObject     = null;
                    FontsProcessor    fontsProcessor = null;
                    bool shouldProcess = false;

                    for (int shareIndex = 0; shareIndex < _shareforRec; shareIndex++)
                    {
                        PDFPageObjectCollection collection = ObjectsPerPage[shareIndex];

                        if (collection[wordIndex] is PDFTextPageObject)
                        {
                            TextObject = (PDFTextPageObject)collection[wordIndex];

                            if (TextObject.Text == "PDF4NET evaluation version 5.0.1.0")
                            {
                                shouldProcess = false;
                                break;
                            }

                            shares[shareIndex] = TextObject.Text;
                            fontsProcessor     = new FontsProcessor();
                            shouldProcess      = true;
                        }
                    }

                    if (shouldProcess)
                    {
                        string decryptedText = ShareAssembler.TextReconstruction(shares, _shareNumbers, _numOfShares, _shareforRec);

                        PDFBrush    brush    = new PDFBrush(TextObject.FillColor);
                        PDFFontBase FontBase = fontsProcessor.GetFontBase(TextObject.FontName, TextObject.FontSize);

                        if (decryptedText.ToLower().Equals(_keyword.ToLower()))
                        {
                            _decryptedPDF.Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0);

                            for (int i = 0; i < _encryptedPdf.Count; i++)
                            {
                                _encryptedPdf[i].Pages[pageIndex].Canvas.DrawRectangle(pen, redBrush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top, 4 * decryptedText.Length, FontBase.Size, 0);
                                _encryptedPdf[i].Pages[pageIndex].Canvas.DrawText(shares[i > shares.Length - 1 ? 0 : i], FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top);
                            }
                        }

                        _decryptedPDF.Pages[pageIndex].Canvas.DrawText(decryptedText, FontBase, brush, TextObject.DisplayBounds.Left, TextObject.DisplayBounds.Top);
                    }
                }
            }

            for (int i = 0; i < _encryptedPdf.Count; i++)
            {
                _encryptedPdf[i].Save(_encryptedFilePath + (i + 1) + ".pdf");
            }

            _decryptedPDF.Save(_decryptedFilePath);
        }