1

I'm trying to bold certain words i am going to insert into my pdf file. The words can be found after my table.addcell("words"). I bold it in order to identify between the data and the label.

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop\\New folder", filename), FileMode.Create);
        PdfWriter.GetInstance(doc1, output);
        doc1.Open();

        PdfPTable table = new PdfPTable(2);

        ////actual width of table in points
        table.TotalWidth = 585f;
        ////fix the absolute width of the table
        table.LockedWidth = true;

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

        SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid,  pr.prdatetime, pr.policereport, mr.citizenreport, aor.officialreport from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro, AdminOfficialReport aor where mr.memberreportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and pr.policereportid=aor.policereportid", con);
        con.Open();
        SqlDataReader dr;

        dr = cm.ExecuteReader();
        PdfPCell header = new PdfPCell(new Phrase("OFFICIAL REPORT "));

        if (dr.Read())
        {
            table.AddCell("Full Name :\r\r "+ dr[0].ToString());
            table.AddCell("Contact :\r\r "+ dr[1].ToString());
            table.AddCell("Type Of Crime :\r\r "+ dr[2].ToString());
            table.AddCell("Location :\r\r "+ dr[3].ToString());
            table.AddCell("Citizen Report Date Time :\r\r "+ dr[4].ToString());
            table.AddCell("PoliceID :\r\r "+ dr[5].ToString());
            table.AddCell("Police Report Date Time :\r\r "+ dr[6].ToString());
            table.AddCell("Police Report :\r\r "+ dr[7].ToString());
            table.AddCell("Citizen Report :\r\r"+ dr[8].ToString());
            table.AddCell("Admin Official Report :\r\r"+ dr[9].ToString());

        }

        dr.Close();
        doc1.Add(table);
        doc1.Close();

    }

I tried using chunk and it doesn't seems to work either.

Chunk chunk = new Chunk("Full Name", FontFactory.GetFont("dax-black") + table.AddCell(dr[0].ToString()));

UPDATE Finally able to format the word Return true when chunk is used iTextSharp

Community
  • 1
  • 1
Bryan
  • 8,488
  • 14
  • 52
  • 78
  • you probably want to add some `Chunk`s tot he cells; there's some examples on working with Chunks at http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs – Rowland Shaw Jun 04 '13 at 08:10
  • I think `` and `` will work. – शेखर Jun 04 '13 at 08:12
  • http://stackoverflow.com/questions/5628473/different-format-into-one-single-line-interop-word/10076425#10076425 – Freelancer Jun 04 '13 at 08:14
  • http://stackoverflow.com/questions/11564073/how-do-i-write-bold-text-to-a-word-document-programmatically-without-bolding-the – Freelancer Jun 04 '13 at 08:15
  • @Freelancer the link you gave me are bolding a string of label as i have read them before posting. However if you see my codes above, my label are placed inside the addcell of the pdf. Hence, i tried the method from the links and it didnt work. – Bryan Jun 04 '13 at 08:21
  • @RowlandShaw I tried using chunk on within my datareader method on my datareader and it doesnt work. Chunk chunk = new Chunk("Full Name", FontFactory.GetFont("dax-black") + table.AddCell(dr[0].ToString())); – Bryan Jun 04 '13 at 08:22
  • @Shekhar those are html tag to bold words and i'm pretty sure it doesn't work for my label. – Bryan Jun 04 '13 at 08:24
  • Has "dax-black" been loaded using an absolute path? http://stackoverflow.com/questions/3497124/itextsharp-textfield-setting-the-font-to-bold – Chris Haas Jun 04 '13 at 13:52
  • @ChrisHaas I'm not very sure. I get the code from some tutorial site. It just wanted the label to be bold though. And i think it is something different from others as the label is within a addcell method. – Bryan Jun 05 '13 at 01:21
  • See the link I posted for how to explicitly create fonts from an actual file path. iTextSharp doesn't automatically look in your fonts folders for fonts (although you can tell it to do that). – Chris Haas Jun 05 '13 at 15:36
  • I get what you meant by explicitly creating font from an actual file path. But as you can see from my codes, I dont really see a way for me to link this explicitly created font into my label within my datareader method. – Bryan Jun 06 '13 at 01:48
  • I have tried using chunk as well http://stackoverflow.com/questions/16952771/return-true-when-chunk-is-used-itextsharp – Bryan Jun 06 '13 at 02:28

0 Answers0