Different coloured text on same line and image scale problem

View: New views
3 Messages — Rating Filter:   Alert me  

Different coloured text on same line and image scale problem

by Elrinth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I recently found this pretty neat lib. There's actually two things I've wondered about and really can't find much documentation about.

Problem 1: How do I create for example something like this?
COMPANY Category, Product Name

I'd like COMPANY to be blue text and "Category, Product Name" to be black text.

I tried achieving it by splitting a it in a table like such:
PdfPTable productNameTable = new PdfPTable(2);
productNameTable.AddCell(new Phrase("COMPANY", blueFont));
productNameTable.AddCell(new Phrase("Category, Product Name", blackFont));

However, it becomes a pretty hugh space inbetween. If I could specify the width of the first cell somehow then it wouldn't be a problem. So I tried making the first cell like PdfPCell and specifying width to it, but seems u can't change that value.


Problem 2: Scaling images to their right size.

Seems my images are upscaled when I insert them into a Table Cell.. How to I prevent this from happening?
I either don't want them to scale at all or that I can specify a max height for each image and the width is scaled to constrain proportions.

Problem 3: Putting images in PdfPCell and adding them as simple AddCell(image) are different?

look:

Here's my code:
PdfPTable colourImgTable = new PdfPTable(2);
colourImgTable.DefaultCell.Border = 0;
colourImgTable.DefaultCell.PaddingTop = 10;
for (int i = 1; i < colourImages.Count; i++)
{
                    PdfPCell smallCell = new PdfPCell(colourImages[i]);
                    if (i + 1 % 2 == 0)
                        smallCell.PaddingRight = 10;
                    else
                        smallCell.PaddingLeft = 10;
                    colourImgTable.AddCell(smallCell);
}
But then the images became like 2. you can see in the image above.

What I wish to achieve is that Padding should only be up and inbetween the two images.

If I do DefaultCell.Padding = 10 I get padding in all directions but the images are atleast the correct size (fit to the table row-col size). But if I add them by PdfPCell they became much larger than the table row-col can handle. Why is this and how do I achieve my goal? :)

Re: Different coloured text on same line and image scale problem

by Elrinth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok so I solved it myself. I type the Company logo + Product name via:
PdfContentByte cb = writer.DirectContent;
        cb.BeginText();
        cb.SetFontAndSize(bf, 12);
        cb.SetColorFill(new Color(0x33, 0x33, 0xff));
        cb.SetTextMatrix(45, 740);
        cb.ShowText("Company Name"); // set position in document
        cb.EndText();

        cb.BeginText();
        cb.SetFontAndSize(bf, 12);
        cb.SetColorFill(new Color(0, 0, 0));
        cb.SetTextMatrix(115, 740); // set position in document
        cb.ShowText("Product Name");
        cb.EndText();


I still haven't figured out how to align images properly tho. The PaddingLeft seems to set PaddingRight automagically and for all other cells also. Very weird.
But I successfully resized an image via:
curImg.ScaleAbsoluteWidth(100);

I'll keep ya updated whenever I fix the other shiz.

Re: Different coloured text on same line and image scale problem

by Elrinth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I solved image alignment aswell. I used the PdfContentByte for that aswell.

I thought I would share my function for anyone else who might be needing the same thing:

/// <summary>
    /// Add spread to pdf
    /// </summary>
    /// a hidden field containing ids for
    /// the contentbyte object grabbed from the writer
    /// a textbox containing some text we want to write above our picture
    /// the font we want to use
    /// is this the pic in the top, second or third row? (0, 1, 2)
private void AddSpreadsToPDF(HiddenField i_hf, PdfContentByte i_cb, TextBox i_textbox, BaseFont i_font, int i_nr)
    {
        // skriv ut texten ovanför
        i_cb.BeginText();
        i_cb.SetFontAndSize(i_font, 10);
        i_cb.SetColorFill(Color.BLACK);
        //i_cb.SetTextMatrix(85, 580 - (i_nr*220));
        //i_cb.ShowText(i_textbox.Text);
        i_cb.SetTextMatrix(83, 710 - (i_nr*230));
        i_cb.ShowText(i_textbox.Text);
        i_cb.EndText();
        string[] splitVals = i_hf.Value.Split(',');
        int cnt = 0;
        foreach (string val in splitVals)
        {
            if (val == "")
                continue;
            if (Request.Form[i_hf.ClientID.Substring(6) + val] != "")
            {
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath(Request.Form[i_hf.ClientID + val]));
                img.SetAbsolutePosition(83 + (cnt*246), 510 - (i_nr*230));
                img.ScaleAbsolute(190, 190);
                i_cb.AddImage(img);
                cnt++;
            }
        }
    }

Ofcourse this code will have to be modified to work for your own need. But you guys hopefully get the picture.