【问题标题】:Outputted pdf not filling with the content in iTextsharp and C#.net, creating with Zero bytes输出的 pdf 未填充 iTextsharp 和 C#.net 中的内容,使用零字节创建
【发布时间】:2014-04-05 04:40:12
【问题描述】:

我正在使用 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             
        }

        }

【问题讨论】:

标签: c# itextsharp


【解决方案1】:

你只是在写你的控制台:

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 只是一种虚假的方法,用于向您显示代码中的错误。这是一个很好的resource
  • LRNAB:根据您的指示,我可以使用已编辑的文本创建新的 pdf,但此 pdf 看起来不像原始 pdf。我的基本目的是在不改变原始 pdf 的外观和感觉的情况下替换一些单词。你能帮我解决这个问题吗,非常感谢你的帮助。
  • @user1133737 您只提取文本,因此只会保存文本,从而删除任何图像、格式等。看看this。我想这个问题也得到了回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 2019-04-30
相关资源
最近更新 更多