Friday, March 9, 2012

Tough day at work

Man, what a tough day at work. I'm still fairly new to ASP.Net.
No idea on how to accomplish a certain task.

Tough wall to crack but I'll crack it anyhow, with a little time and patience, just not today...

Anyway, what I'm trying to accomplish is:

1) Pull 2 sets of data out from database via SQL (Done)
2) Populate 2 lists with sets of data (Done)
3) Enable user to manipulate list dynamically without postbacks (Stuck here)
4) Save changes

Still stuck at step 3. Can't think of a way to do this via ASP.Net.
If this was PHP, I would have already done it with the help of Javascript.

Sign, no idea of which web control to use. Repeater? ListView? Or some other web control which I have not heard of or used yet. What a bummer, stumped at my inexperience.

I need the ItemTemplate portion to generate possibly a span item with unique id that can be picked out from a div with unique id (list1) to another div (list2). I would also need checkboxes.

If without postbacks, does it mean I have to put scripts into the page itself rather than code behind? I'll look into in page scripts first thing on Monday.

If any of you readers have any suggestions, do feel free to throw it in the comments section, though I believe that due to the low exposure of this blog at the moment, I would have solved this by then. Do check out my next post then. I should have my solution posted :)

P.S. Looked back on some old code. Had a hard time programming it back then but since I'm touching on a higher level of code now, looking back on those old code really makes it look easy. I would probably be touching up on those old code sometime... In case of TL;DR, Programming level up!

Monday, March 5, 2012

Combine image and text in a PdfPCell

Hey all,

Was confused as to how to combine an image and text together in ASP.net C#, while using the iTextSharp PDF library. Googled a bit and found my answer.

I'm not an expert at this so I'll just list out the code that would be understandable for dabblers.

If it's too long for you to read (including my babbling), just scroll down to the code

Starting off, I use a [Document] to contain a [PdfPTable] that holds [PdfPCell]s.
Being new to this I was hard-wired to think, 1 PdfPCell, 1 item, with that item being maybe text, or just graphics alone.

However I got stumped when I was required to c
ombine text and graphics together in a cell due to formatting, like wanting to have an image next to a label. I tried two separate cells with the image aligned to the right, and text aligned to the right (I needed it to the end) but due to the Colspan and width properties, I got this.


Yeah, you see it right? I needed that round image next to the text, however due to each cell being 100 pixels and the label not using up all the space I had this ugly effect.

Now here's what you were for! A cell can have multiple elements in there.
A quick touchdown on the solution. Remember
PdfPCell? It used to hold one item, either an Image, or a Phrase or Paragraph.

But now, [PdfPcell] holds 1 [Paragraph], which holds 1 [Chunk] (image) and 1 [Phrase] (text). So I get this!
Further formatting can be done with the X and Y offset to shift the image, you'll see it in the code as (image, 0, 0).

Now here's the code:

Document doc = new Document();

doc.Open();

PdfPTable table = new PdfPTable(5);
table.TotalWidth = 500f;
table.LockedWidth = true;

...
Paragraph paragraph = new Paragraph();
paragraph .Add(new Chunk(image, 0, 0)); // x y offsets as 0 but you can edit it in your code to align the image

paragraph.Add(new Phrase("Admin CP J", timesSmallBlack));
paragraph.Alignment = 2; // This alignment affects the positioning within the cell, important!

PdfPCell cell = new PdfPCell(paragraph );
cell.Colspan = 3;
cell.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);

...

doc.Add(table);

Now, kudos to the original post which I've found: How to get a gif in a pdfpcell to line up with trailing text

If you enjoyed my post and it has helped you and you would like to return the favour you can click any of the sponsored Google ads around the page, thank you!

First post!

First!

Nah, let's stop with that "FIRST" crap like we always see on a new forum post or Facebook picture post.

First and foremost, this is going to be my programming blog. Why blog about it when you're supposed to be doing your work?! Well, in case this information helps you readers out there, in case I forget (which I'll most likely do) and return here to read up again.

This is like a learning journal and history of the walls I have to break down and have broken down. So, if you are reading this post, I don't think you are in the right place. Google or another spider should have brought you to a relevant post. So have fun, if one of my posts has helped, do click a sponsored Google Ad or link to help out, thank you!