To convert xml to pdf using itexthandler using Asp.Net? - asp.net

I am using iTextHandler to convert xml to pdf it creates pdf but does not open it and gives error pdf cannot be opened because it is already opened
Here is my code
string pdfFile = "PeerReview5" + ".pdf";
string strPath = Server.MapPath(Request.ApplicationPath.ToString() + "\12\") + pdfFile;
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(strPath, FileMode.Create));
document.Open();
ITextHandler xmlHandler = new ITextHandler(document);
xmlHandler.Parse(Server.MapPath("12\Peer_Review_Referral_Form_Template.xml"));
document.Close();

From what I read in this post:
iTextSharp How include GenericTag using XML Parsing
You should get the latest version of iTextSharp:
http://sourceforge.net/projects/itextsharp/
Here is a post on of how to open the pdf in the browser:
opening PDF document from memory

Related

C# iTextSharp PdfCopy to MemoryStream copies entire document and single page

I am trying to make a one page PDF from a 3 page PDF document using memory stream. But when the code below executes, all 3 pages get added and not just the first page.
What am I missing here? Please help.
Note: I am using iTextSharp v5.5.13
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
MemoryStream ms = new MemoryStream();
byte[] fileToBeEncrypted = null;
string sSourcePDF = "C:\\my3pageFile.pdf;
PdfReader pdfReader = new PdfReader(sSourcePDF);
Document document = new Document();
PdfCopy copy = new PdfCopy(document, ms) {CloseStream = false};
document.Open();
copy.AddPage(copy.GetImportedPage(pdfReader, iPage));
document.Close();
fileToBeEncrypted = ms.ToArray(); //returns the ENTIRE DOCUMENT AND NOT JUST PAGE 1
Can anyone help?
Thanks
Tom
The above code pdfCopy.GetImportedPages does works and I found an error further down in my own code.

Facing issue with exporting CSV using filehelper.dll

I am exporting CSV from my asp.net web application using filehelper dll.
My code is as follows
this.Response.Clear();
this.Response.ClearContent();
this.Response.ClearHeaders();
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment;
filename=" + "Unicode_CSV_Demo.csv");
DemoCSVcls DemoCSVcls = new DemoCSVcls();
List<DemoCSVcls> CSVTextList = new List<DemoCSVcls>();
DemoCSVcls.Text1 = "‘tipping off’";
DemoCSVcls.Text2 = "ʼ";
CSVTextList.Add(DemoCSVcls);
var engine = new FileHelperEngine<DemoCSVcls>(Encoding.UTF8);
string finalString = engine.WriteString(CSVTextList);
Response.BufferOutput = true;
Response.Write(finalString);
Response.Flush();
Response.End();
In above code I am passing text "‘tipping off’" but when my csv opens in default csv viewer Excel2016 then it appear like follow ‘tipping off’
but same file I am opening in notepad++ or in notepad then it appears as it is like "‘tipping off’". even its encoding is showing in Notepad++ is "UTF-8"
’ these text render for some specific characters only in excel.
How can I stop rendering ’ this chracters in excel? , is there any solution to avoid this?

Generate a PDF document from a webpage with style intact

I am using the iTextSharp library to convert a web page to a PDF document.
Although the document is successfully generated, the CSS applied on the webpage is not being applied in the PDF.
I take the HTML Markup and save it as a text file.
After that, I access that file and put the content into a string.
That markup gets converted in webpage and then into PDF.
But I am not able to apply any css to it.
Example Code:
using (MemoryStream stream = new MemoryStream())
{
Gridhtml = Gridhtml.Replace("\n", "").Replace("\r", "");
System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~/Content/HtmlTable/HtmlTableMarkup.txt"),Gridhtml);
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Content/HtmlTable/HtmlTableMarkup.txt"));
Document pdfdoc = new Document(PageSize.A4,10f,10f,100f,0f);
PdfWriter pdfw = PdfWriter.GetInstance(pdfdoc,stream);
pdfdoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(pdfw,pdfdoc,sr);
pdfdoc.Close();
return File(stream.ToArray(), "applicationpdf", "Grid.pdf");
}
How can I obtain the PDF with the same styling as the webpage?

itextsharp PdfStamper.AcroFields.SetField giving error

I have a PDF containing Chinese, Japanese language. In that PDF I have some input fields. I want to fill this PDF dynamically in C#.net
I am using iTextSharp dll to read pad and successfully read the PDF fields but when I am going to set value with PdfStamper.AcroFields.SetField, it gives me this error
Font 'KozMinPro-Regular' with 'UniJIS-UCS2-H' is not recognized.
To read PDF and getting fields I am using the following code
string pdfTemplate = #"C:\Users\admin\Desktop\test.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
StringBuilder sb = new StringBuilder();
foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
And I am getting all fields successfully.
To fill data in PDF I am using this code
string pdfTemplate = #"C:\Users\admin\Desktop\test.pdf";
string newFile = #"C:\Users\admin\Desktop\newdata_test.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
pdfFormFields.SetField("fill_17", "test");
I am facing error on pdfFormFields.SetField method as
Font 'KozMinPro-Regular' with 'UniJIS-UCS2-H' is not recognized.
Please advice what I have to change or how I can resolve this issue.
There are some files missing in your project. In order to use the font 'KozMinPro-Regular' with 'UniJIS-UCS2-H', you need to give iTextSharp access to the metrics files that contain information about that font. These metrics files can be downloaded separately from SourceForge. More specifically, you need the file iTextAsian-dll-2.1.zip that can be found in iTextAsian-all-2.1.zip
Note that this will only work with a recent version of iTextSharp (5.3.0.0 or higher). This iTextAsian DLL won't work with older versions of iTextSharp.

iTextSharp generate PDF and show it on the browser directly

How to open the PDF file after created using iTextSharp on ASP.Net? I don't want to save it on the server, but directly when the new PDF is generated, it shows on the browser. Is it possible to do that?
Here is the example for what I mean: Click here . But on this example, the file directly downloaded.
How can I do that?
Dim doc1 = New Document()
'use a variable to let my code fit across the page...
Dim path As String = Server.MapPath("PDFs")
PdfWriter.GetInstance(doc1, New FileStream(path & "/Doc1.pdf", FileMode.Create))
doc1.Open()
doc1.Add(New Paragraph("My first PDF"))
doc1.Close()
The code above do save the PDF to the server.
Thank you very much in advance! :)
You need to set Content Type of the Response object and add the binary form of the pdf in the header
private void ReadPdfFile()
{
string path = #"C:\Somefile.pdf";
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
(or) you can use System.IO.MemoryStream to read and display:
Here you can find that way of doing it
Open Generated pdf file through code directly without saving it onto the disk
The problem solved with the code below:
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim pdfDoc As New Document()
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream)
pdfDoc.Open()
'WRITE PDF <<<<<<
pdfDoc.Add(New Paragraph("My first PDF"))
'END WRITE PDF >>>>>
pdfDoc.Close()
HttpContext.Current.Response.Write(pdfDoc)
HttpContext.Current.Response.End()
Hope help! :)

Resources