CSS is not applying on creating pdf from ABC pdf - c#

Problem:
I am passing HTML and creating pdf through ABC pdf.
But the CSS are not applied on the content and pdf created is not as expected.
Here is my code can u please suggest what is the problem or how we can apply CSS...
public static String CreateHtmlFile(String strHtmlCode)
{
String Modifiedhtml = #"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html class="" _Telerik_IE9"" xmlns=""http://www.w3.org/1999/xhtml"">" + strHtmlCode;
Modifiedhtml = Modifiedhtml.Remove(Modifiedhtml.IndexOf(#"//<![CDATA["), (Modifiedhtml.IndexOf("//]]>") - Modifiedhtml.IndexOf(#"//<![CDATA[")));
string[] stringSeparators = new string[] { "PdfCreator" };
var baseUrl = HttpContext.Current.Request.Url.AbsoluteUri.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries).First();
Modifiedhtml = Modifiedhtml.Replace(#"href=""../", (#"href=""" + baseUrl));
Modifiedhtml = Modifiedhtml.Replace(#"href=""/", (#"href=""" + baseUrl));
Doc theDoc = new Doc();
theDoc.HtmlOptions.UseScript = false;
//theDoc.Width = 1125;
String s = string.Empty;
//s = File.ReadAllText(#"D:\test.html");
theDoc.Page = theDoc.AddPage();
int theID;
theID = theDoc.AddHtml(strHtmlCode);
//theID = theDoc.AddHtml(s);
while (true)
{
theDoc.FrameRect(); // add a black border
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}
theDoc.Save(#"D:\two\pagedhtml4.pdf");
theDoc.Clear();
return String.Empty;
}
strHtmlCode is the HTML of the page which we have to convert in PDF.
Thanks in advance

From the WebSupergoo doc page on the AddHtml Function:
Adds a block of HTML styled text to the current page.
HTML styled text does not support CSS. For full featured, standard CSS, you want AddImageHtml.

You are passing strHtmlCode into the AddHtml function. It looks like you really want to pass in Modifiedhtml instead.

Related

Read PDF Line By Line using iText7 and Fill on Textboxes Winforms

I am working on a WinForms application. I use the pdf file to reset the password and the values on pdf are stored as key-value pairs(email: xxxx#mail.com, pass: 11111).
What I want to do:
Read the PDF file line by line and fill the appropriate textboxes.
What I Have done:
public bool CreatePDF(string location, string email, string key)
{
if(location != "" && email != "" && key != "")
{
PdfWriter pdfwriter = new PdfWriter(location);
PdfDocument pdf = new PdfDocument(pdfwriter);
Document document = new Document(pdf);
Paragraph fields = new Paragraph("Email: "+email + "\n" + "Secret Key: "+key);
document.Add(fields);
document.Close();
return true;
}
else
{
return false;
}
}
public string ReadPDF(string location)
{
var pdfDocument = new PdfDocument(new PdfReader(location));
StringBuilder processed = new StringBuilder();
var strategy = new LocationTextExtractionStrategy();
string text = "";
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
{
var page = pdfDocument.GetPage(i);
text += PdfTextExtractor.GetTextFromPage(page, strategy);
processed.Append(text);
}
return text;
}
}
Thank you in advance Guys!. Any suggestions on CreatePDF are also welcome.
This is what I came up with,
var pdfDocument = new PdfDocument(new PdfReader("G:\\Encryption_File.pdf"));
StringBuilder processed = new StringBuilder();
var strategy = new LocationTextExtractionStrategy();
string text = "";
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
{
var page = pdfDocument.GetPage(i);
text += PdfTextExtractor.GetTextFromPage(page, strategy);
processed.Append(text);
}
text.Split('\n');
string line = "";
line = text + "&";
string[] newLines = line.Split('&');
textBox1.Text = newLines[0].Split(':')[1].ToString();
textBox2.Text = newLines[0].Split(':')[2].ToString();

How to remove header and footer in pdf

generate html content to pdf using IronPdf, but in pdf generate 3 pages header content and footer in separate pages , how to display in single page hole content.
var Renderer = new IronPdf.HtmlToPdf();
//Renderer.PrintOptions.Header.DrawDividerLine = false;
//Renderer.PrintOptions.Footer.DrawDividerLine = false;
Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Portrait;
// Renderer.PrintOptions.MarginTop = 10; //millimeters
// Renderer.PrintOptions.MarginBottom = 10;
Renderer.PrintOptions.Zoom = 125;
//Renderer.PrintOptions.FirstPageNumber = 1;
Renderer.PrintOptions.CreatePdfFormsFromHtml = true;
Renderer.PrintOptions.FitToPaperWidth = true;
Renderer.PrintOptions.InputEncoding = Encoding.UTF8;
Renderer.PrintOptions.FitToPaperWidth = true;
pdf will be generate like this: enter image description here
Add the header and footer as follows:
var PDF = Renderer.RenderHTMLFileAsPdf("HTML Body");
PDF.AddHTMLHeaders(new HtmlHeaderFooter() {
HtmlFragment = "HTMLHeader"
});
PDF.AddHTMLFooters(new HtmlHeaderFooter()
{
Height=20,
HtmlFragment = "HTML Footer"
});
https://ironpdf.com/object-reference/api/IronPdf.PdfDocument.html?q=AddHTMLHeaders#IronPdf_PdfDocument_AddHTMLHeaders_IronPdf_HtmlHeaderFooter_System_Double_System_Double_System_Double_System_Boolean_System_Collections_Generic_IEnumerable_System_Int32__

parsing an element in a div with html agility pack [C#]

I'm using Html Agility Pack on a website to extract some data. Parsing some of the HTML I need is easy but I am having trouble with this (slightly complex?) piece of HTML.
<tr>
<td>
<div onmouseover="toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br /><br /><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')" onmouseout="toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')" onclick="togglestick('clue_J_1_1_stuck')">
...
I need to get the value from the em class "correct_response" in the onmouseover div based on the clue_J_X_Y value. I really don't know how to go beyond this..
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//tr//td/div[#onmouseover]");
Some help would be appreciated.
I don't know what you're supposed to get out from the em. But I will give you all the data you say you need to figure it out.
First we load the HTML.
string html = "<tr>" +
"<td>" +
"<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br/><br/><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>";
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Console.WriteLine(doc.DocumentNode.OuterHtml);
Then we get the value of the attribute, onmouseover.
string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[#onmouseover]").GetAttributeValue("onmouseover", "FAILED");
It will return FAILED if it failed to find an attribute named "onmouseover". Now we get the parameters of the toggle method where each are enclosed by two '(apostrophe).
//Get Variables from toggle()
List<string> toggleVariables = new List<string>();
bool flag = false; string temp = "";
for(int i=0; i<toggle.Length; i++)
{
if (toggle[i] == '\'' && flag== true)
{
toggleVariables.Add(temp);
temp = "";
flag = false;
}
else if (flag)
{
temp += toggle[i];
}
else if (toggle[i] == '\'')
{
flag = true;
}
}
After that we have a list with 3 entities. In this case it will contain the following.
clue_J_1_1
clue_J_1_1_stuck
<em class="correct_response">Obama</em><br/><br/><table width="100%"><tr><td class="right">Kailyn</td></tr></table>;
Now we can create a new HtmlDocument with the HTML code from the third parameter. But first we have to convert it into workable HTML since the third parameter contains escape characters from HTML.
//Make it into workable HTML
toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]);
//New HtmlDocument
HtmlDocument htmlInsideToggle = new HtmlDocument();
htmlInsideToggle.LoadHtml(toggleVariables[2]);
Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml);
And done. The code in it's entirety is below from here.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using HtmlAgilityPack;
using System.Web;
namespace test
{
class Program
{
public static void Main(string[] args)
{
string html = "<tr>" +
"<td>" +
"<div onmouseover = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', '<em class="correct_response">Obama</em><br/><br/><table width="100%"><tr><td class="right">Kailyn</td></tr></table>')\" onmouseout = \"toggle('clue_J_1_1', 'clue_J_1_1_stuck', 'Michelle LaVaughn Robinson')\" onclick = \"togglestick('clue_J_1_1_stuck')\"></div></td></tr>";
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Console.WriteLine(doc.DocumentNode.OuterHtml);
string toggle = doc.DocumentNode.SelectSingleNode("//tr//td/div[#onmouseover]").GetAttributeValue("onmouseover", "FAILED");
//Clean up string
//Console.WriteLine(toggle);
//Get Variables from toggle()
List<string> toggleVariables = new List<string>();
bool flag = false; string temp = "";
for(int i=0; i<toggle.Length; i++)
{
if (toggle[i] == '\'' && flag== true)
{
toggleVariables.Add(temp);
temp = "";
flag = false;
}
else if (flag)
{
temp += toggle[i];
}
else if (toggle[i] == '\'')
{
flag = true;
}
}
//Make it into workable HTML
toggleVariables[2] = HttpUtility.HtmlDecode(toggleVariables[2]);
//New HtmlDocument
HtmlDocument htmlInsideToggle = new HtmlDocument();
htmlInsideToggle.LoadHtml(toggleVariables[2]);
Console.WriteLine(htmlInsideToggle.DocumentNode.OuterHtml);
//You're on your own from here
Console.ReadKey();
}
}

ITextSharp build phrase from Html with list tags

I have a report that I'm trying to generate using iTextSharp that includes html text entered by the user using tinymce on my web page. I then have a report and I want to insert a phrase that uses their markup.
While basic markup such as bold and underline work, lists, indents, alignment do not. Any suggestions short of writing my own little html to pdf parser?
My code:
internal static Phrase GetPhraseFromHtml(string html, string fontName, int fontSize)
{
var returnPhrase = new Phrase();
html.Replace(Environment.NewLine, String.Empty);
//the string has to be well formated html in order to work and has to specify the font since
//specifying the font in the phrase overrides the formatting of the html tags.
string pTag = string.Format("<p style='font-size: {0}; font-family:{1}'>", fontSize, fontName);
if (html.StartsWith("<p>"))
{
html = html.Replace("<p>", pTag);
}
else
{
html = pTag + html + "</p>";
}
html
= "<html><body>"
+ html
+ "</body></html>";
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
{
var xmlWorkerHandler = new XmlWorkerHandler();
//Bind a reader to our text
using (TextReader textReader = new StringReader(html))
{
//Parse
XMLWorkerHelper.GetInstance().ParseXHtml(xmlWorkerHandler, textReader);
}
var addPhrase = new Phrase();
var elementText = new StringBuilder();
bool firstElement = true;
//Loop through each element
foreach (var element in xmlWorkerHandler.elements)
{
if (firstElement)
{
firstElement = false;
}
else
{
addPhrase.Add(new Chunk("\n"));
}
//Loop through each chunk in each element
foreach (var chunk in element.Chunks)
{
addPhrase.Add(chunk);
}
returnPhrase.Add(addPhrase);
addPhrase = new Phrase();
}
return returnPhrase;
}
}
}

RTF to HTML, change font-family

I have some RTF text from my C# application which I convert to HTML and then send to my PHP file.
Problem is; all my text in PHP is in Arial, The output of my RTF is Tahoma. Any ideas how I can change the font-family?
This is my code so far:
string memoValue = inboundSet.Fields["MEMO"].Value.ToString();
if (RtfTags.IsRtfContent(memoValue))
{
using (RichEditDocumentServer richServer = new RichEditDocumentServer())
{
string htmlText = string.Empty;
richServer.RtfText = memoValue;
htmlText = richServer.HtmlText;
callDetail.Memo = htmlText;
}
}
else
{
callDetail.Memo = memoValue;
}
In my PHP file I get the value in this way:
echo "<td>Memo:</td><td>".$value->Memo."</td>";
I also tried it in this way:
echo "<td>Memo:</td><td class='fonttest'>".$value->Memo."</td>";
And in my CSS:
.fonttest
{
font-size:12px;
font-family:Arial;
}
My text keeps looking like this:
This is what my RTF text looks like:
I solved my issue by this way:
string memoValue = inboundSet.Fields["MEMO"].Value.ToString();
if (RtfTags.IsRtfContent(memoValue))
{
using (RichEditDocumentServer richServer = new RichEditDocumentServer())
{
string htmlText = string.Empty;
richServer.RtfText = memoValue;
CharacterProperties cp = richServer.Document.BeginUpdateCharacters(richServer.Document.Range);
cp.FontName = "Arial";
cp.FontSize = 12;
richServer.Document.EndUpdateCharacters(cp);
htmlText = richServer.HtmlText;
callDetail.Memo = htmlText;
}
}
You have to set font-family for your generated HTML. You can do so by applying CSS style to the header of geneted page.
Embed this style to the header:
"<style>body {font-family:Arial;}</style>"

Categories

Resources