{"id":18785,"date":"2022-11-01T23:25:25","date_gmt":"2022-11-01T17:55:25","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/"},"modified":"2022-11-01T23:25:25","modified_gmt":"2022-11-01T17:55:25","slug":"solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/","title":{"rendered":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-21928831\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"21928831\" data-parentid=\"21927641\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Please read the documentation before posting semi-duplicate questions, such as:<\/p>\n<ul>\n<li>Edit an existing PDF file using iTextSharp<\/li>\n<li>How to Read and Mark(Highlight) a pdf file using C#<\/li>\n<\/ul>\n<p>You have received some very good feedback, such as the answer from Nenotlep that  was initially deleted (I asked the moderators to have it restored). Especially the comment by mkl should have been very useful to you. It refers to Retrieve the respective coordinates of all words on the page with itextsharp and that&#8217;s exactly what you&#8217;re asking now, making your question a <em>duplicate<\/em> (a possible reason to have it removed from StackOverflow).<\/p>\n<p>In his answer, mkl explains that you&#8217;re taking your assignment too lightly. Instead of extracting pure text, you should extract <code>TextRenderInfo<\/code> objects. These objects contain information about the content (the actual text) as well as the position on the page. See for instance the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/itextpdf.com\/examples\/iia.php?id=275\">ParsingHelloWorld<\/a> example from chapter 15 of my book.<\/p>\n<p>The method you&#8217;re using returns the content of the PDF as a string. Similar to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/examples.itextpdf.com\/results\/part4\/chapter15\/result1.txt\">result1.txt<\/a> which is the output of said example:<\/p>\n<blockquote>\n<p>Hello World<\/p>\n<\/blockquote>\n<p>In the same example, we parse a different PDF that has the exact same content when looked at by the human eye. However, when you parse the document, the content looks like this (see  <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/examples.itextpdf.com\/results\/part4\/chapter15\/result2.txt\">result2.txt<\/a>):<\/p>\n<blockquote>\n<p>ld<br \/>\n  Wor<br \/>\n  llo<br \/>\n  He<\/p>\n<\/blockquote>\n<p>The reason for this difference is inherent to the nature of PDF: the concept of lines doesn&#8217;t really exist: you can add characters to a page in any which order you want. You don&#8217;t even need to add complete words!<\/p>\n<p>When you use the <code>GetTextFromPage()<\/code> method, you tell iText you don&#8217;t want to get any info about the position of the text. Mlk has tried explaining this to you, but I&#8217;ll try explaining it once more. In the example from my book, I have extended the <code>RenderListener<\/code> in a class named <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/itextpdf.com\/examples\/iia.php?id=282\"><code>MyTextRenderListener<\/code><\/a>. Now the output looks like this (see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/examples.itextpdf.com\/results\/part4\/chapter15\/result3.txt\">result3.txt<\/a>).<\/p>\n<pre><code>&lt;&gt;\n&lt;&lt;ld&gt;&lt;Wor&gt;&lt;llo&gt;&lt;He&gt;&gt;\n&lt;&lt;Hello People&gt;&gt;\n<\/code><\/pre>\n<p>This is the output of the same PDF we parsed when getting result2.txt. As you can see, we missed the words <em>Hello People<\/em> in the previous attempt.<\/p>\n<p>The example is really simple: it just shows you have to text snippets are stored in the PDF. We get all the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.itextpdf.com\/itext\/com\/itextpdf\/text\/pdf\/parser\/TextRenderInfo.html\"><code>TextRenderInfo<\/code><\/a> objects and we use the <code>GetText()<\/code> method to get the text. The order in which we get the text is the order that is used in the PDF&#8217;s content stream.<\/p>\n<p>When using a specific strategy, such as the <code>LocationTextExtractionStrategy<\/code>, iText retrieves all these objects and it used the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/api.itextpdf.com\/itext\/com\/itextpdf\/text\/pdf\/parser\/TextRenderInfo.html#getBaseline%28%29\"><code>GetBaseline()<\/code><\/a> method to sort all the text snippets.<\/p>\n<pre><code>&lt;&lt;ld&gt;&lt;Wor&gt;&lt;llo&gt;&lt;He&gt;&gt;\n<\/code><\/pre>\n<p>results in:<\/p>\n<pre><code>&lt;&lt;He&gt;&lt;llo&gt;&lt;Wor&gt;&lt;ld&gt;&gt;\n<\/code><\/pre>\n<p>Then iText looks at the distance between the different snippets. In this case, iText adds a space between the <code>&lt;llo&gt;<\/code> and <code>&lt;Wor&gt;<\/code> snippet.<\/p>\n<p>You are now looking to do the same thing: you are going to write a system that is going to retrieve all the text snippets, that is going to order them, examine them, and based on the composed content, you are going to add a background at those locations.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Please read the documentation before posting semi-duplicate questions, such as: Edit an existing PDF file using iTextSharp How to Read and Mark(Highlight) a pdf file using C# You have received some very good feedback, such as the answer from Nenotlep that was initially deleted (I asked the moderators to have it restored). Especially the &#8230; <a title=\"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\" aria-label=\"More on [Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,1773,4525,489,362],"class_list":["post-18785","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-itext","tag-itextsharp","tag-pdf","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Please read the documentation before posting semi-duplicate questions, such as: Edit an existing PDF file using iTextSharp How to Read and Mark(Highlight) a pdf file using C# You have received some very good feedback, such as the answer from Nenotlep that was initially deleted (I asked the moderators to have it restored). Especially the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T17:55:25+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#\",\"datePublished\":\"2022-11-01T17:55:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\"},\"wordCount\":543,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"itext\",\"itextsharp\",\"pdf\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\",\"name\":\"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T17:55:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb","og_description":"[ad_1] Please read the documentation before posting semi-duplicate questions, such as: Edit an existing PDF file using iTextSharp How to Read and Mark(Highlight) a pdf file using C# You have received some very good feedback, such as the answer from Nenotlep that was initially deleted (I asked the moderators to have it restored). Especially the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T17:55:25+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#","datePublished":"2022-11-01T17:55:25+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/"},"wordCount":543,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","itext","itextsharp","pdf","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/","name":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c# - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T17:55:25+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-search-for-particular-line-contents-in-pdf-and-make-that-line-marked-in-color-using-itext-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to search for Particular Line Contents in PDF and Make that Line Marked In Color using Itext in c#"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18785"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18785\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}