簡體   English   中英

如何刪除單詞中的空格? (itextSharp,PdfReader,C#,ASP.NET)

[英]How can I delete spaces in words? (itextSharp, PdfReader, C#, ASP.NET)

When I transfer pdf files to the textbox with itextsharp (PdfReader), my all pdf's text looks like this: wo r d (long space) wo r d (long space) wo r d

(我將此代碼從一個視頻復制到我自己的項目中。我不知道它是如何工作的。所以,考慮到我什么都不知道,請建議我進行更改。)

string strText = string.Empty;
PdfReader reader = new PdfReader(path + dlg.FileName);
            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
                String s = PdfTextExtractor.GetTextFromPage(reader, page, its);

                s = System.Text.Encoding.UTF8.GetString(System.Text.ASCIIEncoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, System.Text.Encoding.Default.GetBytes(s)));
                strText = strText + s;
                txtPdf.Text = strText;
            }
            reader.Close();

您可以使用Replace 替換字符串中的空格。

這是您的案例的示例:

...
s = System.Text.Encoding.UTF8.GetString(System.Text.ASCIIEncoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, System.Text.Encoding.Default.GetBytes(s)));
strText = strText + s.Replace(" ", string.Empty);
...

還要檢查文檔以開始使用。

編輯:您也可能遇到制表符(“長空格”),您可以使用相同的模式將其替換為空格(其中\t用於表示制表符,有關詳細信息,請參閱字符串轉義序列):

strText = strText + s.Replace(" ", string.Empty).Replace('\t', ' ');

注意:這僅適用於使用制表符分隔單詞的文檔,您可能需要根據您處理的文檔調整替換邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM