VhsPiceros
VhsPiceros

Reputation: 426

HR tag is not implemented in iTextSharp

I have one problem with tag HR when I use HTMLWorker, my code is:

var document = new Document(PageSize.A4, 50, 50, 25, 25);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
String contents = File.ReadAllText("C://TemplateCotizaciones//Cotizacion.html");
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader (contents), null);

but,when the html contains one tag HR in the html the method ParseToList of HTMLWorker throw an exception "nullReferenceException"

for example: I have error with:

hello<br/>
<hr>
world

and without error

hello<br/>

world

do you know why? I think the HR is not implement in the code of ParseToList. do you know how write one line in html without HR in html(soported byHTMLWorker.ParseToList) ?

thanks for your advices and help

Upvotes: 4

Views: 2459

Answers (1)

Joris Schellekens
Joris Schellekens

Reputation: 9032

HTMLWorker is deprecated. XMLWorker is still supported, but not the latest way of achieving the "html to pdf" functionality. pdfHTML is the latest iText7 add-on for converting HTML to pdf. It supports HTML5 and CSS3.

A small code-sample:

public void createPdf(String src, String dest, String resources) throws IOException {
    try {
        FileOutputStream outputStream = new FileOutputStream(dest);

        WriterProperties writerProperties = new WriterProperties();
        //Add metadata
        writerProperties.addXmpMetadata();

        PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);

        PdfDocument pdfDoc = new PdfDocument(pdfWriter);
        pdfDoc.getCatalog().setLang(new PdfString("en-US"));
        //Set the document to be tagged
        pdfDoc.setTagged();
        pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));

        //Set meta tags
        PdfDocumentInfo pdfMetaData = pdfDoc.getDocumentInfo();
        pdfMetaData.setAuthor("Samuel Huylebroeck");
        pdfMetaData.addCreationDate();
        pdfMetaData.getProducer();
        pdfMetaData.setCreator("iText Software");
        pdfMetaData.setKeywords("example, accessibility");
        pdfMetaData.setSubject("PDF accessibility");
        //Title is derived from html

        // pdf conversion
        ConverterProperties props = new ConverterProperties();
        FontProvider fp = new FontProvider();
        fp.addStandardPdfFonts();
        fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources

        props.setFontProvider(fp);
        props.setBaseUri(resources);
        //Setup custom tagworker factory for better tagging of headers
        DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
        props.setTagWorkerFactory(tagWorkerFactory);

        HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
        pdfDoc.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Find out more at http://itextpdf.com/itext7/pdfHTML

Upvotes: 1

Related Questions