如何使用iTextSharp進(jìn)行文本格式化我正在使用iTextSharp從PDF中讀取文本內(nèi)容。我也能讀到這一點(diǎn)。但我正在丟失文字格式,如字體,顏色等。有沒有辦法獲得格式。以下是我用于確切文本的代碼段 -PdfReader reader = new PdfReader("F:\\EBooks\\AspectsOfAjax.pdf");textBox1.Text = ExtractTextFromPDFBytes(reader.GetPageContent(1));private string ExtractTextFromPDFBytes(byte[] input){
if (input == null || input.Length == 0) return "";
try
{
string resultString = "";
// Flag showing if we are we currently inside a text object
bool inTextObject = false;
// Flag showing if the next character is literal e.g. '\\' to get a '\' character or '\(' to get '('
bool nextLiteral = false;
// () Bracket nesting level. Text appears inside ()
int bracketDepth = 0;
// Keep previous chars to get extract numbers etc.:
char[] previousCharacters = new char[_numberOfCharsToKeep];
for (int j = 0; j < _numberOfCharsToKeep; j++) previousCharacters[j] = ' ';
for (int i = 0; i < input.Length; i++)
{
char c = (char)input[i];
if (inTextObject)
{
// Position the text
if (bracketDepth == 0)
{
if (CheckToken(new string[] { "TD", "Td" }, previousCharacters))
{
resultString += "\n\r";
}
else
{
if (CheckToken(new string[] {"'", "T*", "\""}, previousCharacters))
{
resultString += "\n";
}
else
{
if (CheckToken(new string[] { "Tj" }, previousCharacters))
{
resultString += " ";
}
}
}
}
- 2 回答
- 0 關(guān)注
- 706 瀏覽
添加回答
舉報(bào)
0/150
提交
取消