Paragraph CommentsofExaminerData = new Paragraph();
if (DTO.QuestionComment != null)
{
CommentsofExaminerData = new Paragraph(DTO.QuestionComment, fontH2);
}
cell1 = new PdfPCell(new Paragraph(CommentsofExaminerData));
I have html richtextbox value in DTO.QuestionComment and i want to convert to html Format.
How can I parse the data?
Related
I want to apply some styles that are predefined in word. My code is as follows, but the style "Heading1" doesn't work.
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true);
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Paragraph p = new Paragraph();
ParagraphProperties ppr = new ParagraphProperties();
ParagraphStyleId stid = new ParagraphStyleId() { Val = "Heading1" };
ppr.Append(stid);
p.Append(ppr);
I'm trying to export a DataGridView with custom currency formatting to PDF using iTextSharp, the formatting shows up perfectly on the DataGrid when running the program but doesn't appear on the PDF once I save it.
Here is the DataGridView code (showing the currency formatting):
DataTable t = new DataTable();
if (t != null)
{
a.Fill(t);
//Custom currency formatting
var format = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
format.CurrencySymbol = "R";
dgvMiscEntries.AutoGenerateColumns = false;
dgvMiscEntries.ColumnCount = 2;
//Assign column headers manually
dgvMiscEntries.Columns[0].Name = "Description";
dgvMiscEntries.Columns[0].HeaderText = "Description";
dgvMiscEntries.Columns[0].DataPropertyName = "Description";
dgvMiscEntries.Columns[1].Name = "Rate";
dgvMiscEntries.Columns[1].HeaderText = "Rate";
dgvMiscEntries.Columns[1].DataPropertyName = "Rate";
dgvMiscEntries.Columns[1].DefaultCellStyle.FormatProvider = format;
dgvMiscEntries.Columns[1].DefaultCellStyle.Format = "c";
// Render data onto grid
dgvMiscEntries.DataSource = t;
}
And here is the iTextSharp code:
//Creating iTextSharp Misc Entries Table from the DataTable data
PdfPTable miscTable = new PdfPTable(dgvMiscEntries.ColumnCount);
miscTable.DefaultCell.Padding = 3;
float[] miscWidthPosit = new float[] { 1000f, 200f };
miscTable.WidthPercentage = 80;
miscTable.SetWidths(miscWidthPosit);
miscTable.HorizontalAlignment = Element.ALIGN_CENTER;
miscTable.DefaultCell.BorderWidth = 1;
miscTable.SpacingBefore = 10;
miscTable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (DataGridViewRow row in dgvMiscEntries.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
miscTable.AddCell(cell.Value.ToString());
}
}
}
I have searched for similar questions here and on other sites but I haven't found any addressing this particular problem.
Thats because the Value property takes the actual bound value which is used to provide the formatted text.
So instead of
miscTable.AddCell(cell.Value.ToString());
use FormattedValue
miscTable.AddCell(cell.FormattedValue.ToString());
I'm using C#, ASP.NET, iTextSharp for my application.
I have formatting issues when I print my datatable to PDF.
I have two datatables: dt and dt_child
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
stringWrite.Write(dr[dc]);
}
indentno=dr["REF"].ToString();
dt_child = getDataTablebyProcedure("ChildProcedure",ref);
stringWrite = getStringWriterbyDataTable(dt_child, stringWrite);
}
Code is working fine. I have an issue with formatting.
The picture shows how it is currently displayed.
I want
1: The header row of dt to be displayed. It is currently not displayed.
2: The row of dt to be displayed in table form. Currently I'm iterating rows in each column.
You will need to set UI for PDF.
Try this if it helps
private void ExportDataToPDFTable()
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
doc.Open();//Open Document to write
Font font8 = FontFactory.GetFont("ARIAL", 7);
//Write some content
Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");
DataTable dt = GetDataTable();
if (dt != null)
{
//Craete instance of the pdf table and set the number of column in that table
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;
//Add Header of the pdf table
PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
PdfTable.AddCell(PdfPCell);
PdfPCell = new PdfPCell(new Phrase(new Chunk("Name", font8)));
PdfTable.AddCell(PdfPCell);
//How add the data from datatable to pdf table
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
PdfTable.AddCell(PdfPCell);
}
}
PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
doc.Add(paragraph);// add paragraph to the document
doc.Add(PdfTable); // add pdf table to the document
}
}
catch (DocumentException docEx)
{
//handle pdf document exception if any
}
catch (IOException ioEx)
{
// handle IO exception
}
catch (Exception ex)
{
// ahndle other exception if occurs
}
finally
{
//Close document and writer
doc.Close();
}
}
Courtesy
Add table into existing PDF using iTExtsharp
For reference
http://www.codeproject.com/Tips/573907/Generating-PDF-using-ItextSharp-with-Footer-in-Csh
I am trying to create reports in my asp.net MVC3 application after a lot of search I found many blog posts talks about ITextSharp to convert my Html/Razor to Pdf I am trying to parse razor view to get PDf as follows
public void Render(ViewContext viewContext, TextWriter writer)
{
var doc = new Document();
// associate output with response stream
var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream);
pdfWriter.CloseStream = false;
viewContext.HttpContext.Response.ContentType = "application/pdf";
viewContext.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
// generate view into string
var sb = new System.Text.StringBuilder();
TextWriter tw = new System.IO.StringWriter(sb);
_result.View.Render(viewContext, tw);
var resultCache = sb.ToString();
//Path to our font
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
doc.Open();
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
doc.Add(element);
}
doc.Close();
pdfWriter.Close();
}
the result of that code is
which is not correct, the arabic word should be "محمد". so what I need is to set document direction to be from right to left
EDIT
Thanks to #Romulus
I made a little changes to his code i just replaced adding element to PdfPCell to looping on my Html and set some attributes
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
//Create a cell and add text to it
//PdfPCell text = new PdfPCell(new Phrase(element.ToString(), f));
//Ensure that wrapping is on, otherwise Right to Left text will not display
//text.NoWrap = false;
//Add the cell to the table
//table.AddCell(text);
if (element is iTextSharp.text.pdf.PdfPTable)
{
table = (iTextSharp.text.pdf.PdfPTable)element;
table.DefaultCell.NoWrap = false;
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
foreach (PdfPRow row in table.Rows)
{
foreach (PdfPCell cell in row.GetCells())
{
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
cell.NoWrap = false;
}
}
}
}
That's working for me now well Thanks :)
You need to use container elements which support RunDirection, such as ColumnText or PdfPCell and then set their element.RunDirection = PdfWriter.RUN_DIRECTION_RTL
List<IElement> list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
doc.Open();
//Use a table so that we can set the text direction
PdfPTable table = new PdfPTable(1);
//Ensure that wrapping is on, otherwise Right to Left text will not display
table.DefaultCell.NoWrap = false;
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
//Loop through each element, don't bother wrapping in P tags
foreach (var element in list)
{
//Create a cell and add text to it
PdfPCell text = new PdfPCell(new Phrase(element, font));
//Ensure that wrapping is on, otherwise Right to Left text will not display
text.NoWrap = false;
//Add the cell to the table
table.AddCell(text);
}
//Add the table to the document
document.Add(table);
doc.Close();
pdfWriter.Close();
For addition reference, have a look at this sample.
I want to make table using OpenXML WordProcessing. I want to format the font inside the cell. This is my code
MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Body body = mainDocumentPart.Document.AppendChild(new Body());
RunProperties runHeader = new RunProperties();
RunFonts runFont = new RunFonts();
runFont.Ascii = "Lucida Sans";
runHeader.Append(runFont);
runHeader.Append(new Bold());
runHeader.Append(new FontSize() { Val = "16" });
//// Create a new table
Table tbl = new Table();
tr = new TableRow();
tc = new TableCell();
Paragraph paraHeader = new Paragraph();
Text heading_text = new Text("Company Name");
runHeader.Append(heading_text);
paraHeader.Append(runHeader);
tc.Append(paraHeader);
tr.Append(tc);
tbl.Append(tr);
body.AppendChild(tbl);
But when I open up on Microsoft Word, I got error. Its said that the file has problem with the contents
You are appending your text to your Run Properties, it needs to be appended to a Run.
try:
Text heading_text = new Text("Company Name");
////create the run
Run runHeaderRun = new Run();
////append the run properties and text to the run
runHeaderRun.Append(runHeader);
runHeaderRun.Append(heading_text);
////append the run to the paragraph
paraHeader.Append(runHeaderRun);
tc.Append(paraHeader);
RunProperties rp = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Calibri Light" };
rp.Append(runFont);
rp.Append(new Color() { Val = "#2E74B5" });
body.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(rp, new Text("3. Risk Assessment"))));
It's a really simple to use, try this.