i am not getting you properly, what exactly your need is. Is you want to parse complete html form into the pdf??
Regards Inderjeet Singh On 4/8/12, Parminder Kaur <[email protected]> wrote: > I am trying to create a new TagProcessor to process custom input tags and > render a PdfFormField. I tried searching and reading about it but neither > the book nor the site have much information on how to do so. > > This is the code to add the new TextBoxTagProcessor to the TagFactory > > ByteArrayOutputStream os = new ByteArrayOutputStream(); >> Document pdfDocument = new Document(PageSize.A4); >> try{ >> PdfWriter writer = PdfWriter.getInstance(pdfDocument, os); >> InputStream in = new ByteArrayInputStream(pdfXHTML.getBytes("UTF-8")); >> //pdfXHTML is a HTML page >> HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); >> pdfDocument.open(); >> TagProcessorFactory tagProcessorFactory = (DefaultTagProcessorFactory) >> Tags.getHtmlTagProcessorFactory(); >> tagProcessorFactory.addProcessor(new TextBoxTagProcessor(), >> "textbox"); >> htmlContext.setTagFactory(tagProcessorFactory); >> htmlContext.setImageProvider(new AbstractImageProvider() { >> public String getImageRootPath() { >> return ""; >> } >> }); >> htmlContext.setLinkProvider(new LinkProvider() { >> public String getLinkRoot() { >> return "http://tutorial.itextpdf.com/src/main/resources/html/ >> "; >> } >> }); >> >> CSSResolver cssResolver = >> XMLWorkerHelper.getInstance().getDefaultCssResolver(true); >> Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, new >> HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDocument, writer))); >> XMLWorker worker = new XMLWorker(pipeline, true); >> XMLParser p = new XMLParser(worker, Charset.defaultCharset()); >> p.parse(in); >> //XMLWorkerHelper.getInstance().parseXHtml(writer, document, in, >> Charset.defaultCharset() ); >> pdfDocument.close(); >> }catch (Exception e){} >> >> return os.toByteArray(); > > > > And this is the TextBoxTagProcessor class > > import com.itextpdf.text.*; >> import com.itextpdf.text.pdf.*; >> import com.itextpdf.text.pdf.TextField; >> import com.itextpdf.tool.xml.NoCustomContextException; >> import com.itextpdf.tool.xml.Tag; >> import com.itextpdf.tool.xml.WorkerContext; >> import com.itextpdf.tool.xml.exceptions.LocaleMessages; >> import com.itextpdf.tool.xml.html.AbstractTagProcessor; >> import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext; >> >> import java.util.ArrayList; >> import java.util.List; >> >> public class TextBoxTagProcessor extends AbstractTagProcessor { >> private String name; >> private boolean required; >> >> @Override >> public List<Element> start(final WorkerContext ctx, final Tag tag){ >> List<Element> l = new ArrayList<Element>(1); >> return l; >> } >> >> @Override >> public List<Element> end(final WorkerContext ctx, final Tag tag, final >> List<Element> currentContent) { >> List<Element> l = new ArrayList<Element>(1); >> final HtmlPipelineContext context; >> try { >> context = getHtmlPipelineContext(ctx); >> PdfPTable table = new PdfPTable(1); >> PdfPCell cell = new PdfPCell(); >> String name = (tag.getAttributes().get("name") != null) ? >> tag.getAttributes().get("name") : ""; >> String required = (tag.getAttributes().get("required") != >> null) ? tag.getAttributes().get("required") : "false"; >> cell.setCellEvent(new TextboxEvent(name, required)); >> table.addCell(cell); >> l.add(table); >> } catch (NoCustomContextException e) {} >> return l; >> } >> >> public static class TextboxEvent implements PdfPCellEvent { >> private String name; >> private boolean required; >> >> public TextboxEvent(String name, String required) { >> this.name = name; >> this.required = Boolean.parseBoolean(required); >> } >> >> public void cellLayout(PdfPCell cell, Rectangle position, >> PdfContentByte[] canvases) { >> try { >> PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS]; >> PdfWriter writer = canvases[0].getPdfWriter(); >> cb.saveState(); >> TextField text = new TextField(writer, new >> Rectangle(position.getLeft(), position.getBottom()-4, >> position.getRight()-30, position.getTop()+4),name); >> text.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); >> text.setText(""); >> text.setFontSize(9.0f); >> text.setAlignment(Element.ALIGN_LEFT); >> if(required){ >> text.setOptions(TextField.REQUIRED); >> } >> PdfFormField field = text.getTextField(); >> writer.addAnnotation(field); >> //cb.restoreState(); >> } >> catch (Exception e) { >> throw new ExceptionConverter(e); >> } >> } >> } >> } >> > > > When I run this code, I get "java.lang.NullPointerException" but I don't > know where this is being thrown or why. Is the issue with the Pdfwriter > being null in the cellLayout method? My old code works fine for these > formfields but now I am trying to migrate over to XMLWorker as it seems > much easily modifiable but I am stuck on this. I tried looking at the > Header.java and Anchor.java but didn't get through. Any pointers would be > helpful. > > Thanks! > > Regards, > Parminder Kaur > ------------------------------------------------------------------------------ For Developers, A Lot Can Happen In A Second. Boundary is the first to Know...and Tell You. Monitor Your Applications in Ultra-Fine Resolution. Try it FREE! http://p.sf.net/sfu/Boundary-d2dvs2 _______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
