0

I have got HTML working with itextsharp to create a PDF with the following code, however I can't add emtpy rows or changing width of td by using width. Here is the code that I use

string customer = "客";
        string address = "地";
        int page = 0;
        DateTime today = DateTime.Now;
        double lastMonthLeft = 0.0;
        double twoMonthsLeft = 0.0;
        double threeMonthsLeft = 0.0;
        double thisMonthLeft = 0.0;
        double fourAboveLeft = 0.0;
        string handleMan = "";
        DateTime startPick = StartTimePicker.SelectionRange.Start;
        DateTime endPick = EndTimePicker.SelectionRange.Start;
        DateTime beginning = new DateTime(startPick.Year, startPick.Month, startPick.Day, 00, 00, 01);
        DateTime ending = new DateTime(endPick.Year, endPick.Month, endPick.Day, 23, 59, 59);
        string path = "C:\\Users\\Benjiw\\Desktop\\test.pdf";
        StringBuilder sb = new StringBuilder();
        sb.Append("<html><body><table border=\"1\" width=\"100%\" cellspacing=\"0\"> <tr><td></td><td colspan=\"3\" align=\"center\" font size=\"5\">ccc</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >bbb</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >aaa</td><td></td></tr>");
        sb.Append("<tr><td></td><td colspan=\"3\" align=\"center\" >Rddd</td><td></td></tr>");
        sb.Append("<tr><td></td><td>TEL: 123123</td><td></td><td>FAX: 32321</td></tr><tr><td></td><td></td><td></td>  <td>   </td><td></td></tr>");
        sb.Append("</table>");

        sb.Append("<table border=\"1\" width=\"100%\" cellspacing=\"0\">");
        sb.Append("<tr><td>致   :</td><td>" + customer + "</td><td></td><td>日期         :</td><td>" + today.ToString("MMMM dd yyyy") + "</td></tr>");
        sb.Append("<tr><td>地址   :</td><td colspan=\"2\">" + address + "</td><td>發票號碼         :</td><td>" + today.ToString("MMMM dd yyyy") + "</td></tr>");
        sb.Append("<tr><td></td><td align=\"center\" colspan=\"3\">INVOICE</td><td></td></tr>");
        sb.Append("<tr><td></td><td align=\"center\" colspan=\"3\">發票</td><td></td></tr>");


        sb.Append("</table>");
        sb.Append("<table border=\"1\" width=\"100%\" cellspacing=\"0\">");
        sb.Append("<tr><td colspan=\"7\">======================================================================================</td></tr>");
        sb.Append("<tr><td style=\"width:5px;\">單號</td><td width=\"5%\">日期</td><td width=\"5%\">貨品</td><td width=\"5%\" colspan=\"2\">數量</td><td width=\"5%\">單價(HK$)</td><td width=\"75%\">金額(HK$)</td></tr>");
        sb.Append("<tr><td colspan=\"7\">======================================================================================</td></tr>");



        sb.Append("<tr><td colspan=\"7\"><br><br><br>1<\td><\tr>");  <---------- this is where i wanna insert some white space
        sb.Append("</table>");

        sb.Append("</body></html>");
        iTextSharp.text.FontFactory.Register(Environment.GetEnvironmentVariable("windir") + "\\Fonts\\KAIU.TTF", "KAIU");
        iTextSharp.text.html.simpleparser.StyleSheet style = new iTextSharp.text.html.simpleparser.StyleSheet();

        style.LoadTagStyle("body", "face", "KAIU");
        style.LoadTagStyle("body", "encoding", "Identity-H");
        style.LoadTagStyle("body", "leading", "12,0");


        style.LoadTagStyle("td", "face", "KAIU");
        style.LoadTagStyle("td", "encoding", "Identity-H");
        style.LoadTagStyle("td", "leading", "18,0");

        iTextSharp.text.Document document = new iTextSharp.text.Document();
        document.SetPageSize(iTextSharp.text.PageSize.A4);

        iTextSharp.text.pdf.PdfWriter.GetInstance(document, (new FileStream(path, FileMode.Create)));
        document.Open();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document, null, style);
        hw.Parse(new StringReader(sb.ToString()));

        //document.NewPage();
        //hw.Parse(new StringReader(htmlText));

        document.Close();

I tried to add
but it doesn't have the same height as one data row, please let me know how I can get that done and also setting the width of the column thanks

  • Kindly Edit your question – Rajesh Mar 25 '15 at 04:35
  • @Rajesh is it better now ? First time posting sorry – user4710372 Mar 25 '15 at 04:38
  • Questions about `HTMLWorker` will no longer be answered because `HTMLWorker` has been abandoned in favor of XML Worker. You can find several XML Worker examples (including examples that produce Chinese glyphs) here: http://itextpdf.com/sandbox/xmlworker – Bruno Lowagie Mar 25 '15 at 09:45
  • @BrunoLowagie Thanks for the reply, the reason why I am using html is because I couldn't get Chinese character showing by using itextsharp itself, I have set the font like above and instead of using HTML code I used pdfptable to create a table and add the table to the document, however, Chinese characters are not showing, so u know how that can be fixed? – user4710372 Mar 25 '15 at 09:49
  • Did you take a look at the following examples? [pdf1](http://itextpdf.com/sites/default/files/hero.pdf) [code1](http://itextpdf.com/sandbox/xmlworker/D07_ParseHtmlAsian) [pdf2](http://itextpdf.com/sites/default/files/asian.pdf) [code2](http://itextpdf.com/sandbox/xmlworker/D07bis_ParseHtmlAsian) [pdf3](http://itextpdf.com/sites/default/files/asian2.pdf) [code3](http://itextpdf.com/sandbox/xmlworker/D07tris_ParseHtmlAsian). All of these examples convert HTML with Chinese characters to PDF, don't they? – Bruno Lowagie Mar 25 '15 at 10:06
  • @BrunoLowagie guess I didn't say it clearly. I am able to show Chinese character with HTML to itextsharp. However, I have problems trying to get the width and such working for . About the Chinese characters, I was not able to create a pdf using itextsharp itself without html working properly. Does it make any sense to u? – user4710372 Mar 25 '15 at 10:07
  • Currently, I only see `HTMLWorker` code and it doesn't surprise me that the width for `` doesn't work. However, I don't see any XML Worker code that proves that the width for `` doesn't work. – Bruno Lowagie Mar 25 '15 at 10:16

1 Answers1

0

Please take a look at the ParseHtmlTable8 example. For this example, I have tweaked your HTML a little bit (because it wasn't entirely correct): table5_css.html (Note that it's still an awkward document).

When I convert this to PDF using the examples I referred you to in my comments, I get this PDF: html_table_8.pdf

enter image description here

As you can see, widths and height and fonts look OK.

This is my code:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    // CSS
    CSSResolver cssResolver = new StyleAttrCSSResolver();

    // HTML
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontProvider.register("resources/fonts/NotoSansCJKsc-Regular.otf");
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    // Pipelines
    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));;
    // step 5
    document.close();
}

Note that I used NotoSansCJKsc-Regular.otf as font. This is a font distributed by Google under the Apache software license 2.0.

Obviously, I indicated that I wanted to use this font in my HTML file:

<body style="font-family: NotoSansCJKsc-Regular">
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks alot, it looks really need, for the .html file, its basically the same as a string right? I am asking this because I don't really want to create a html file – user4710372 Mar 25 '15 at 12:05
  • You can pass the HTML any way you want, but I didn't like your code (it's looks as if it's written by a very lazy programmer), and that's why I converted the string into an HTML file. – Bruno Lowagie Mar 25 '15 at 13:08
  • yea, I was just trying to see if it works out, since I am gonna access my database to get data too, I can basically just write everything into a. Html file and save it somewhere, then read it like the way u did it? If I wanna do it the clean way. I am still a beginner for programming, I am trying to learn more and become better – user4710372 Mar 25 '15 at 14:00
  • The main problem I have with your code is the fact that you use hardcode Chinese glyphs in your code. These glyphs risk to get lost if you save the file with the wrong encoding, if your versioning system uses a different encoding,... I would make sure that the content is stored in a database as UTF-8 and that the data is retrieved as UTF-8. If your data is stored in a database, I wouldn't create a file on disk. I would create the HTML in memory and serve it to XML Worker as an input stream from memory. – Bruno Lowagie Mar 25 '15 at 14:17
  • awww so the idea is kinda the same ? one is being harcode the other is saving it to a html with the corrent encoding? I am saving and getting data using utf8, I have already tested those – user4710372 Mar 26 '15 at 00:03
  • hi again. I just got a chance to try it out, I am wondering which library are u using, I can't seems to import itextpdf.tool, it says there's no name space as. Tool. I am usinf c# btw – user4710372 Mar 26 '15 at 04:22
  • Use [iTextSharp](http://sourceforge.net/projects/itextsharp/files/itextsharp/iTextSharp-5.5.5/) and [XML Worker](http://sourceforge.net/projects/itextsharp/files/xmlworker/xmlworker-5.5.5/) – Bruno Lowagie Mar 26 '15 at 07:49
  • I got it working just now, however it takes about like 8 sec to create the PDF while htmlworker is basically instant, is there any ways to fix that? – user4710372 Mar 26 '15 at 07:51
  • (1) On my machine, it doesn't take 8 seconds. Only very complex HTMLs with plenty of nested elements would take that long. (2) XML Worker supports CSS and many other things that aren't supported in `HTMLWorker`, hence XML Worker needs more time than `HTMLWorker` (but not 8 seconds). (3) The really slow method is `registerDirectories()` but that method can be avoided: http://stackoverflow.com/questions/21275800/itextsharp-xmlworker-parsing-really-slow – Bruno Lowagie Mar 26 '15 at 07:59
  • hi, since I am working with a database, there will be lots rows inserting into the table, and it is now taking like 20-30 sec to create the PDF. Let says if I dont wanna work with xmlworker nor htmlworker anymore, just by using the standard pdfptable to create these tables, how would I set the width of the columns and the height of the rows? – user4710372 Mar 27 '15 at 02:58
  • When I read comments on my phone (like I am doing now), the "notification" of unread comments is changed into "read". I can't answer questions that involve code samples or an elaborate explanation. I hope that this explains why using a comment to ask an extra question usually isn't a good idea. The question usually remains unanswered because comments are forgotten whereas real questions (posted as a question, not as a comment) remain. – Bruno Lowagie Mar 27 '15 at 11:18