locked
css issue in string builder RRS feed

  • Question

  • i am creating string builder table and apply css on it.i set table background image like this
    sb.Append("<table style = 'background-image:url(C:\\Users\\Archit\\Downloads\\itbp.jpg)>")
    but when i print to this table on pdf, background image not display and some columns height also not set.
     
    any idea how to set table background image which shown on pdf.
    and how to set column height manually? 
    Wednesday, September 12, 2018 6:42 AM

Answers

  • In that case I'll try set the background color of table cells and see if making it transparent works.

    If that is not working, the only ways left are:

    1) to contact project owner to see if their newer version supports it (as I said before, both iText5/7 are not free for commercial usage), or

    2) to find other mean to convert it, or

    3) just learn how to generate PDF directly with iTextSharp package.

    Friday, September 14, 2018 2:47 AM

All replies

  • More context would be helpful.

    But why using inline styles instead of "real" CSS?

    Wednesday, September 12, 2018 7:43 AM
  • i only want to set table background image and i have no idea how to implement real css in stringbuilder.

    when i debug the program image show in html visualizer but not print on pdf.

    Wednesday, September 12, 2018 7:49 AM
  • Do you have data in xml/database or you write whole html directly in code by stringbuilder?

    How do you print html to pdf? There could be problem with conversion html into pdf. Some convertors cannot convert some part of html into pdf. 


    • Edited by Petr B Wednesday, September 12, 2018 7:59 AM
    Wednesday, September 12, 2018 7:58 AM
  • i am trying this.

     private void btn_export(object sender, EventArgs e)
            {

               
              
                iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10F, 10F, 10F, 0F);
             
                StringBuilder sb = new StringBuilder();
             
                sb.Append("<table  width='100%' height= '100%' background = C:\\Users\\Archit\\Downloads\\itbp.jpg   border=1>");
                foreach (DataGridViewRow row in dgv_preview.Rows)
                {
                    bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].Value);
                    if (isSelected)
                    {
                        DateTime dob = DateTime.Parse(row.Cells["dob"].Value.ToString());
                        DateTime date = DateTime.Now;
                        int ab = Convert.ToInt32(row.Cells["ID"].Value);
                        Zen.Barcode.CodeQrBarcodeDraw brcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
                        
                        sb.Append("<tr>" +
                            "<td width= '49%' ><table width='100%' border =1>" +
                            "<tr><td  colspan=3 height='39px'>Header</td></tr>" +
                            "<tr><td width='29%' rowspan=5><img src= C:\\Users\\Archit\\Downloads\\a1.png width ='70' height ='50'></td><td width='23%' height='10' colspan = 2>ssss</td></tr>" +
                            "<tr><td ></td><td><span style='font-family: arial; font-size: 10pt;'>" + row.Cells["REGT NO"].Value.ToString() + "</span></td> </tr>" +
                            "<tr><td></td><td><span style='font-family: arial; font-size: 10pt;'>" + row.Cells["RANK"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height='12'></td><td><span style='font-family: arial; font-size: 10pt;'>" + row.Cells["NAME"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height='10'></td><td><span style='font-family: arial; font-size: 10pt;'>" + row.Cells["Aadharno"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height='44'><img src= D:\\HOLDER_SIGN\\sign.jpg width = '50' height ='30' valign='top'></td><td colspan=2>" +
                            "<table border=1 width='100%'><tr><td width='36%' rowspan=2></td> <td width='21%' height=0><span style='font-family: arial; font-size: 8pt;'>" + row.Cells["CARD NO"].Value.ToString() + "</span></td>" +
                            "<td width='43%' rowspan=2><img src= D:\\HOLDER_SIGN\\sign.jpg width = '50' height ='30' ></td></tr>" +
                            "<tr><td height='10'><span style='font-family: arial; font-size: 8pt;'>" + row.Cells["bloodgroup"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height='10' colspan=3></td></tr>" +
                            "</table></td><td ></td></tr></table></td>" +
                            "<td width='1'></td>" +
                            "<td width='49%' >" +
                            "<table border=1 width='100%' ><tr><td width='34%' height='10'>***</td><td width='40%' height='10'><span style='font: Kruti Dev 010; font-size: 6pt;'>" + row.Cells["location_h"].Value.ToString() + "</span></td><td width=26% rowspan=3>" + brcode.Draw("read 200000 faliure " + (ab * 1.5).ToString(), 16) + "</td></tr>" +
                            "<tr><td height='10'></td><td height='10'><span style='font-family: arial; font-size: 6pt;'>" + row.Cells["idmark"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height=20 colspan=2>" +
                            "<table border=1 width='100%'><tr><td height='13'></td><td height='13'><span style='font-family: arial; font-size: 6pt;'>" + dob.ToString("dd/MM/yyyy") + "</span></td><td height=13></td><td height=13><span style='font-family: arial; font-size: 6pt;'>" + date.ToString("dd/MM/yyyy") + "</span></td></tr>" +
                            "<tr><td height='12'></td><td height='12'><span style='font-family: arial; font-size: 6pt;'>" + row.Cells["eye"].Value.ToString() + "</span></td><td height=12></td><td height=12><span style='font-family: arial; font-size: 6pt;'>" + row.Cells["height"].Value.ToString() + "</span></td></tr>" +
                            "</table>" +
                            "</td></tr>" +
                            "<tr><td height='25'></td><td colspan=2><span style='font-family: arial; font-size: 6pt;'>" + row.Cells["Location"].Value.ToString() + "</span></td></tr>" +
                            "<tr><td height='56' colspan=3>35</td></tr></table></td>");

                        sb.Append("</tr>");
                     
                        
                    }
                }
                sb.Append("</table>");
               

                StringReader sr = new StringReader(sb.ToString());
                string path = "C:\\Users\\Archit\\Desktop\\test.pdf";
                StringWriter sw = new StringWriter();
                var output = new FileStream(path, FileMode.Create);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
                pdfDoc.Open();
               
                htmlparser.Parse(sr);
                pdfDoc.Close();
                        

            }

    Wednesday, September 12, 2018 8:07 AM
  • then any other solution for this problem. actually i want to create id card printing application and i have id card image so i think cardimage set as a background of every table and place data above the image.now this is not working . :( 
    Wednesday, September 12, 2018 8:24 AM
  • AFAIK iTextSharp HTML/XML worker only can process well formed HTML (your style attribute is not closing) and those attributes. Try add quotes to the url path like this:

    sb.Append("<table style=\"background-image:url('C:\\Users\\Archit\\Downloads\\itbp.jpg')\">");


    • Edited by cheong00 Wednesday, September 12, 2018 8:37 AM
    Wednesday, September 12, 2018 8:33 AM
  • this is not working only table is printing
    Wednesday, September 12, 2018 9:34 AM
  • Maybe, instead of external images, try the Data protocol: https://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx. Try the sample image and see if it appears in PDF.


    Wednesday, September 12, 2018 10:12 AM
  • how to implement dataprotocol to my code???
    Wednesday, September 12, 2018 10:17 AM
  • i try something like 

    style=\"background-image:url('data:image/jpg;base64,/9j/4AAQSkZJRgABAgEBLAEsAAD/4QosRXhpZgAATU0AKg=')\"

    but its also not working. 

    Wednesday, September 12, 2018 10:27 AM
  • Here is a suggestion, create a test html page setup as in your code and then read it in to a stream  if iTextSharp permits this. Does it work? If not tweak it until it does. Then (and this is a jump), create a VB.NET class project, create a class such as the one below (has just enough to get the concept down). While writing this code you get intellisense and color syntax highlights as shown below. Call this in your C# project and pass it along to iTextSharp. The nice thing is no string concatenation is needed. This structure is one thing that C# does not offer when dealing with Windows Forms.

    I write everything in C# but when it comes to task such as this VB.NET is brought in.

    Imports System.Windows.Forms
    
    Public Class Creator
        ''' <summary>
        ''' Create HTML from dgv (DataGridView)
        ''' </summary>
        ''' <param name="dgv"></param>
        ''' <returns></returns>
        Public Function Builder(dgv As DataGridView) As String
            Dim filterData = From row In dgv.Rows.
                    OfType(Of DataGridViewRow).
                    Where(Function(x) CBool(x.Cells("checkBoxColumn").Value))
    
            Dim results =
            <table width='100%' height='100%' background='C:\Users\\Archit\Downloads\itbp.jpg' border='1'>
                <%= From row In filterData
                    Select
                    <tr>
                        <td width='49%'>
                            <%= CBool(row.Cells("checkBoxColumn").Value) %>
                        </td>
                    </tr>
                %>
            </table>
    
            Return results.Value
        End Function
    
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, September 12, 2018 11:40 AM
  • <table style = 'background-image:url(C:\\Users\\Archit\\Downloads\\itbp.jpg)>

    -----

    Missing protocol spec in URL. Try

    <table style = "background-image:url('file://C:\\Users\\Archit\\Downloads\\itbp.jpg')">

    But be sure that image is on place on the host where pdf converted to printable format.

    One additional option - try base64 on place image encoding.

    Best regards,

    Andrey


    Wednesday, September 12, 2018 11:59 AM
  • this is also not working.. image is on place when i debug the program and check into html visualizer image showing there but on pdf no image display...

    can u please explain how to use base64 in my code.

    Wednesday, September 12, 2018 12:12 PM
  • Do you have pictures on the place on PC where you process printing?

    Did user from whom running printer software has permission to access pictures?

    Base64 - you encode your picture into string (http://www.utilities-online.info/base64/) and instead of using url use a =base64:"your base64 string".

    Do you consider possibilities that version of your pdf-printing software didn't process properly a background for table? 

    Best regards,

    Andrey

    Correction - example of base64:

    url('data:image/png;base64,iVBORw0KG ... kJggg==')

    Wednesday, September 12, 2018 2:15 PM
  • hello sir,

    yes sir i have a picture on the place actually image is showing when i debug the program.

    but at pdf there is no image display. 

    Thursday, September 13, 2018 4:46 AM
  • Reading the source code of itextsharp (i.e.: the free version), seems background-image CSS style is processed for DIV tag only, not processed for TD tag or elsewhere, and only in XmlWorker.

    Try wrap the table with big DIV and set the background-image there and see if it helps.

    Maybe you can ask them whether iText 5 or 7 supports it on TABLE tag. (Note that these are not free for commercial use).

    Thursday, September 13, 2018 5:40 AM
  • i am also done this with div tag canvas tag but result is same.

    i think pdf does't support css 


    Thursday, September 13, 2018 5:44 AM
  • Just to make sure you've not missed that part, you need to use XmlWorker instead of HtmlWorker for background-image style be processed for DIV tag.
    Thursday, September 13, 2018 5:50 AM
  • sir how to parse html with xmlworker

                StringReader sr = new StringReader(sb.ToString());
                string path = "C:\\Users\\Archit\\Desktop\\test.pdf";
                StringWriter sw = new StringWriter();
                var output = new FileStream(path, FileMode.Create);

       iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10F, 10F, 10F, 0F);            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, output);
                pdfDoc.Open();
               // pdfDoc.Add(jpg);
                htmlparser.Parse(sr);
                pdfDoc.Close();

    Thursday, September 13, 2018 5:58 AM
  • Google can find it for you.

    https://gist.github.com/kad1r/6105088

    Note that the line for creating hw is not needed.

    Thursday, September 13, 2018 6:32 AM
  • sir i am using xmlworker but result is same

       string HTML = "<div style=\"background-image:url('C:\\Users\\Archit\\Downloads\\itbp.jpg');width:100%;height:100%;\ "><table style=\"width:100%;\"><tr><td style=\"width:100px;\">FOO</td><td style=\"width:10px\">:</td><td>BLABLABLA ...</td></tr></table></div>";
                
                try
                {
                  
                    string filepath = "C:\\Users\\Archit\\Desktop\\test2.pdf";
                    Document document = new Document(PageSize.A4);
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create));
                    document.Open();
                    StringReader sr = new StringReader(HTML);
                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
                    document.Close();
                    PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
                }
                catch (Exception ex)
                {
                    string error = ex.Message;
                }

    Thursday, September 13, 2018 7:08 AM
  • Hi,

    Based on your description and related, I create a demo with the two method about image background, all do not work, it seems that it only support bgcolor, I would suggest that you could contact the owner for suitable support. 

    https://www.nuget.org/packages/iTextSharp/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 13, 2018 7:33 AM
  • But image shows in debugger window.

    Thursday, September 13, 2018 7:55 AM
  • In that case I'll try set the background color of table cells and see if making it transparent works.

    If that is not working, the only ways left are:

    1) to contact project owner to see if their newer version supports it (as I said before, both iText5/7 are not free for commercial usage), or

    2) to find other mean to convert it, or

    3) just learn how to generate PDF directly with iTextSharp package.

    Friday, September 14, 2018 2:47 AM