iText标题:在左侧添加徽标img,在右侧添加文本

时间:2017-03-01 12:21:58

标签: image header itext

我是iText的新手。 我想在每个页面上创建一个相同的标题。 页眉如下所示:

logo.jpg                                                                       some text

我该怎么做? 我看过这个链接:

http://developers.itextpdf.com/question/how-generate-report-dynamic-header-pdf-using-itextsharp

我有一个问题,因为我无法将图像添加到pharse中。

Image image = Image.GetInstance(Server.MapPath(mclLogo));
phrase.Add(image);

引发错误:

  

插入非法元素:32

编辑:我尝试过段落和胶水:

Paragraph paragraph = new Paragraph();
paragraph.Add(image);
paragraph.Add(new Chunk(glue));
paragraph.Add("text on the right");

,输出为2行。 somthing链接:

logo.jpg
                                                                                some text

编辑2: 我读过这个: http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks 特别是部分:

Image fox = new Image(ImageDataFactory.create(FOX));
Image dog = new Image(ImageDataFactory.create(DOG));
Paragraph p = new Paragraph("The quick brown ")
        .add(fox)
        .add(" jumps over the lazy ")
        .add(dog);
document.add(p);

但是我使用iText5而且我找不到任何方法如何在同一行中制作图片和文字。

1 个答案:

答案 0 :(得分:0)

<块引用>

试试下面的代码,它适用于两个不同的标题图,在右端用图像绘制,在左端用文本绘制。

public override void OnStartPage(PdfWriter writer, iTextSharp.text.Document document)
{
        PdfPTable HeaderPlot = new PdfPTable(new float[] { 10F });//Header plot 1
        PdfPTable HeaderPlot2 = new PdfPTable(new float[] { 10F });//Header plot 2
        PdfPCell cell;//cell 1
        PdfPCell cell2;// cell 2
        HeaderPlot.TotalWidth = 570F; //width for Header plot 1
        HeaderPlot2.TotalWidth = 570F;//width for Header plot 2
        cell = new PdfPCell();
        cell2 = new PdfPCell();
        string path = headerpath;
        FileInfo f2 = new FileInfo(path);
        FileStream fs = new FileStream(f2.FullName,
        FileMode.Open, FileAccess.Read);
        BinaryReader rdr = new BinaryReader(fs);
        byte[] fileData = rdr.ReadBytes((int)fs.Length);
        Image image = Image.GetInstance(fileData);
        image.ScaleAbsolute(80, 40); //adjusting image size
        image.Alignment = Element.ALIGN_CENTER;
        cell = new PdfPCell(image)
        {
            Border = 0,
            HorizontalAlignment = Element.ALIGN_TOP,
            VerticalAlignment = Element.ALIGN_TOP
        };//Header image position
        Font font = FontFactory.GetFont("Calibri Light", 8f, Font.NORMAL, 
        iTextSharp.text.BaseColor.BLACK);//Initializing font
        cell2 = new PdfPCell(new Phrase("Text", font))
        {
            Border = 0,
            HorizontalAlignment = Element.ALIGN_LEFT,
            VerticalAlignment = Element.ALIGN_TOP
        };//Header Text position
        HeaderPlot.AddCell(cell);//adding cell 1 to Headerplot 1
        HeaderPlot.WriteSelectedRows(0, -1, 480, 835, 
        writer.DirectContent);//Position of the header on right end, coordinates on right end(480,835)
        HeaderPlot2.AddCell(cell2);//adding cell 2 to Headerplot 2
        HeaderPlot2.WriteSelectedRows(0, -1, 40, 835, 
        writer.DirectContent);//Position of the header on left end, coordinates on left end(40,835)
    }