itextsharp PdfStamper.AcroFields.SetField giving error - asp.net

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.

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.

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?

Document cannot be converted into iTextSharp.text.Document

So I saw this tutorial in codeproject http://www.codeproject.com/Articles/20640/Creating-PDF-Documents-in-ASP-NET. I used the itextsharp in my web project
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream("C:\Users\PC\Desktop" + _
"\1.pdf", FileMode.Create))
doc.Open()
doc.Add(New Paragraph("Hello World"))
doc.Close()
Response.Redirect("~/1.pdf")
with this syntax I get errors that 'myProjectName.Document' cannot be converted to 'iTextSharp.text.Document'and open, add ,close is not a member of 'myProjectName.Document'
My current framework is 3.5 . I have downloaded the sample project it was framework 2.0 but can still run when I changed it to 3.5,
Any workaround with my problem?
Looks like your Document is coming from some other namespace, try specifying the namespace explicitly:
dim doc as iTextSharp.text.Document = new iTextSharp.text.Document() 'may need parameters to constructor

To convert xml to pdf using itexthandler using 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

What's the easiest way to convert a BMP to a PDF in ASP.net

What's the easiest way to convert a BMP file to a single page PDF using ASP.net? I'm going to generate the 8.5" x 11" BMP in Flash (which means I can manipulate it to make it as easy as possible), then use a POST to upload it to an ASP page, which will convert it to a PDF and redirect the user to the PDF. I don't want to add any margins or anything else, it will be laid out properly in the BMP for full-bleed.
Would it be easier to convert it to PDF in Flash, then upload?
Thanks!
You can use iTextSharp to create a PDF and insert the image into the document. This can be done all in memory with a final PDF produced to client.
The following is an MVC method, stripped for display, but should see how to do this.
[HttpGet]
public FileStreamResult Export(int? ID)
{
MemoryStream stream = new MemoryStream();
Document pdf = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
pdf.Open();
PdfPTable tblImage = new PdfPTable(1);
tblImage.AddCell(Image.GetInstance(LogChart())); //The LogChart method returns image
pdf.Add(Image);
pdf.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Log.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
}

Resources