Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working to replace the specific word inside pdf document using iTextSharp and C#.net, while I am debugging getting the proper value but the outputted pdf getting zero bytes(empty),its not filling with the content. Here is my code:

C#
ReplacePDFText("Mumbai",StringComparison.CurrentCultureIgnoreCase,Application.StartupPath + "\\test.pdf","D:\\test_words_replaced.pdf"); //Do Everything

public void ReplacePDFText(string strSearch, StringComparison scCase, string strSource, string strDest)
    {
        PdfStamper psStamp = null; //PDF Stamper Object
        PdfContentByte pcbContent = null; //Read PDF Content

        if (File.Exists(strSource)) //Check If File Exists
        {

            PdfReader pdfFileReader = new PdfReader(strSource); //Read Our File
            psStamp = new PdfStamper(pdfFileReader, new FileStream(strDest, FileMode.Create)); //Read Underlying Content of PDF File
            pbProgress.Value = 0; //Set Progressbar Minimum Value
            pbProgress.Maximum = pdfFileReader.NumberOfPages; //Set Progressbar Maximum Value

            for (int intCurrPage = 1; intCurrPage <= pdfFileReader.NumberOfPages; intCurrPage++) //Loop Through All Pages
            {
                LocTextExtractionStrategy lteStrategy = new LocTextExtractionStrategy(); //Read PDF File Content Blocks
                pcbContent = psStamp.GetUnderContent(intCurrPage); //Look At Current Block
                //Determine Spacing of Block To See If It Matches Our Search String
                lteStrategy.UndercontentCharacterSpacing = pcbContent.CharacterSpacing;
                lteStrategy.UndercontentHorizontalScaling = pcbContent.HorizontalScaling;
                //Trigger The Block Reading Process
                string currentText = PdfTextExtractor.GetTextFromPage(pdfFileReader, intCurrPage, lteStrategy);
                //Determine Match(es)
                List<itextsharp.text.rectangle> lstMatches = lteStrategy.GetTextLocations(strSearch, scCase);
                PdfLayer pdLayer = default(PdfLayer); //Create New Layer
                pdLayer = new PdfLayer("Overrite", psStamp.Writer); //Enable Overwriting Capabilities

                //Set Fill Colour Of Replacing Layer
                pcbContent.SetColorFill(BaseColor.BLACK);
                foreach (iTextSharp.text.Rectangle rctRect in lstMatches) //Loop Through Each Match
                {
                    pcbContent.Rectangle(rctRect.Left, rctRect.Bottom, rctRect.Width, rctRect.Height); //Create New Rectangle For Replacing Layer
                    pcbContent.Fill(); //Fill With Colour Specified
                    pcbContent.BeginLayer(pdLayer); //Create Layer
                    pcbContent.SetColorFill(BaseColor.BLACK); //Fill aLyer
                    pcbContent.Fill(); //Fill Underlying Content
                    PdfGState pgState = default(PdfGState); //Create GState Object
                    pgState = new PdfGState();
                    pcbContent.SetGState(pgState); //Set Current State
                    pcbContent.SetColorFill(BaseColor.WHITE); //Fill Letters
                    pcbContent.BeginText(); //Start Text Replace Procedure
                    pcbContent.SetTextMatrix(rctRect.Left, rctRect.Bottom); //Get Text Location
                    //Set New Font And Size
                    pcbContent.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 9);
                    pcbContent.ShowText("AMAZING!!!!"); //Replacing Text
                    pcbContent.EndText(); //Stop Text Replace Procedure
                    pcbContent.EndLayer(); //Stop Layer replace rocedure
                }                   
                pbProgress.Value++; //Increase Progressbar Value
                pdfFileReader.Close(); //Close File                 
            }
            //psStamp.Close(); //Close Stamp Object             
        }
        }</itextsharp.text.rectangle>
Posted
Updated 6-Apr-14 7:51am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900