输出的pdf未填充iTextsharp和C#.net中的内容,使用零字节创建。 [英] Outputted pdf not filling with the content in iTextsharp and C#.net, creating with Zero bytes.

查看:72
本文介绍了输出的pdf未填充iTextsharp和C#.net中的内容,使用零字节创建。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp和C#.net替换pdf文档中的特定单词,而我正在调试获取正确的值,但输出的pdf得到零字节(空),它没有填充内容。这是我的代码:



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:

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>

推荐答案

这篇关于输出的pdf未填充iTextsharp和C#.net中的内容,使用零字节创建。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆