Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输出的pdf未填充iTextsharp和C#.net中的内容,创建时使用零字节_C#_Itextsharp - Fatal编程技术网

输出的pdf未填充iTextsharp和C#.net中的内容,创建时使用零字节

输出的pdf未填充iTextsharp和C#.net中的内容,创建时使用零字节,c#,itextsharp,C#,Itextsharp,我正在使用iTextSharp和C#.net替换pdf文档中的特定单词,同时调试获得正确的值,但输出的pdf得到零字节(空),它没有填充内容 ReplacePDFText("Mumbai",StringComparison.CurrentCultureIgnoreCase,Application.StartupPath + "\\test.pdf","D:\\test_words_replaced.pdf"); //Do Everything public void ReplacePDFTex

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

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             
        }

        }
replacepdfext(“孟买”,StringComparison.CurrentCultureInoRecase,Application.StartupPath+”\\test.pdf,“D:\\test\u words\u replaced.pdf”)//无所不能
public void ReplacePDFText(字符串strearch、字符串比较scCase、字符串strSource、字符串strDest)
{
PdfStamper psStamp=null;//PDF母版对象
PdfContentByte pcbContent=null;//读取PDF内容
if(File.Exists(strSource))//检查文件是否存在
{
PdfReader pdfFileReader=newpdfreader(strSource);//读取我们的文件
psStamp=newpdfstamper(pdfFileReader,newfilestream(strDest,FileMode.Create));//读取PDF文件的底层内容
pbProgress.Value=0;//设置Progressbar的最小值
pbProgress.Maximum=PdfileReader.NumberOfPages;//设置Progressbar的最大值

对于(int intCurrPage=1;intCurrPage,您仅向控制台写入内容:

Console.WriteLine(ExtractTextFromPdf(@"C:\temp\MyPdf.pdf");
您没有将PDF保存到磁盘,因此无法期望在PDF文件中看到任何更改。您的代码应将修改后的文本保存到磁盘:

var editedText = ExtractTextFromPdf(@"C:\temp\MyPdf.pdf");
Console.WriteLine(editedText);
SaveTextToPdf(editedText, @"C:\temp\MyPdf1.pdf");

LRNAB:谢谢你的回复。SaveTextToPdf是内置的iTextSharp方法,或者需要编写自定义方法?我是新使用iTextSharp库的人。我建议你阅读文档。SaveTextToPdf只是一个假的方法,用于向你显示代码中的错误。以下是一个很好的LRNAB:根据你的说明,我能够用编辑过的文本创建新的pdf但此pdf看起来不像原始pdf。我的基本目的是在不改变原始pdf的外观的情况下替换某些单词。您能帮我解决这个问题吗?非常感谢您的帮助。@user1133737您提取文本只是为了保存文本,从而删除任何图像、格式等。请看下面的问题我想,这个问题已经得到了回答。请阅读我的《iText in Action-Second Edition》一书第6章的导言,以了解为什么你的问题是错误的。你可以在这里下载这一章: