Free Support Forum - aspose.com

双层pdf转TXT,生成的TXT文档有空格,我该如何删除这些空格。

感谢您的查看,

6665.jpg (76.9 KB)

// Open document 打开PDF文件
        Aspose.Pdf.Document pdfDocument1 = new Aspose.Pdf.Document(fi);

        // Create TextAbsorber object to extract text
        TextAbsorber textAbsorber = new TextAbsorber();
        // Accept the absorber for all the pages  
        pdfDocument1.Pages.Accept(textAbsorber);
        // Get the extracted text
        string extractedText = textAbsorber.Text;
        // Create a writer and open the file Create a writer and open the file
        var dir = System.IO.Path.GetDirectoryName(fi);

        string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fi);

        string wb = dir+"\\"+ fileNameWithoutExtension+".txt";

        //不要空格
        extractedText.Trim().Replace(" ", "");
        extractedText.Replace(" ", "");
        extractedText.Replace("\n", "");

        extractedText.Replace("  ", string.Empty);
        extractedText.Replace("\n", string.Empty);

        //创建txt文本和写入权限
        FileStream fs1 = new FileStream(wb, FileMode.Create, FileAccess.Write);//创建写入

        TextWriter tw = new StreamWriter(fs1);
        // Write a line of text to the file 在文件中写入一行文本
        tw.WriteLine(extractedText);
       
        // Close the stream 关闭小溪
        tw.Close();

@fhn123456

请与我们分享您的样本PDF文档。我们将在我们的环境中测试该场景并相应地解决它。