|
-
May 26th, 2011, 09:50 AM
#1
Thread Starter
Frenzied Member
Anyone experienced in iTextSharp?
I am using the code below to parse my HTML into a PDF Document.
However I have found that it works initially at overwriting documents as required unless I try to overwrite one with less content than the original, in these cases i suspect that redundant pages are not being deleted and that also ithe actual PDF will not open (as if it is corrupt)
Code:
public void GeneratePDFDoc(strText)
{
Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(ConfigurationManager.AppSettings["PDFPath"].Trim()) +"/" + CourseCode + ".pdf", FileMode.OpenOrCreate));
doc.AddDocListener(writer);
doc.Open();
String htmlText = strText;
List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
doc.Add((IElement)htmlarraylist[k]);
}
doc.Close();
}
In addition to this I actually want to create the PDF from a template so rather than opening and modifying the PDF I actually just want to overwrite it completely.
I understand I need to use the stamper methods for this but dont see how I can fit in my HTML parser text.
In addition to this I also want to insert the images refered to in the HTML image tags.
Any help on what methods to use appreciated.
-
Jun 1st, 2011, 09:57 AM
#2
Thread Starter
Frenzied Member
Re: Anyone experienced in iTextSharp?
Update
The images problem was solved in thread http://www.vbforums.com/showthread.php?t=651424
The code below opens a template copies is and creates a new document from it and adds the html to the document.
My outstanding issue is that the template actually just holds an image like a header, when I add the additional html generated text it is added at the same place as the header rather than after.
I need to figure out where the template content ends and only add the additional content after.
Code:
public void GeneratePDFDoc(string htmlText)
{
int pageNumber = 1;
PdfReader reader = new PdfReader(Server.MapPath(ConfigurationManager.AppSettings["PDFPath"].Trim()) + "/" +"Template.pdf");
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(pageNumber);
Document doc = new Document(size);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(ConfigurationManager.AppSettings["PDFPath"].Trim()) + "/" + Code + ".pdf", FileMode.Create, FileAccess.Write));
doc.Open();
PdfContentByte cb = writer.DirectContent;
doc.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
cb.AddTemplate(page, 0, 0);
String htmlText = ReturnHTMLString(htmlText);
htmlText = RemoveHyperLinks(htmlText);
htmlText = LocalizeImages(htmlText);
List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText),null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
doc.Add((IElement)htmlarraylist[k]);
}
doc.Close();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|