使用iTextSharp在同一行上显示多个图像和文本

时间:2018-07-22 03:39:44

标签: c# asp.net itext

我正在尝试使用一些后端数据动态创建PDF文件,并且在文件底部我需要包括2张图像和一个文本:左侧为签名图像,中间和日期为公司徽标(在该行的下面加上“日期”字样。

我已经搜索并成功完成了两个操作:左侧为图片,右侧为日期,但现在我一直试图在两者之间获取另一个图片!

这是我目前拥有的(假设我已经从数据库中读取了用户名和测试日期):

using (MemoryStream ms = new MemoryStream())
{
    Document doc = new Document(PageSize.LETTER.Rotate());
    doc.Open();

    // Set path to PDF file and images used in the file
    string sPath = Server.MapPath("PDF");
    string sImagePath = Server.MapPath("assets/Images");

    // Set PDF file name
    string sFileName = "SomeFileNameGenerated" + ".PDF";

    // Open document
    PdfWriter myPDFWriter = PdfWriter.GetInstance(doc, ms);
    doc.Open();

    // Set some font styles
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    Font fntDate = new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.NORMAL | Font.UNDERLINE, BaseColor.BLACK);
    Font fntUserName = new Font(Font.FontFamily.TIMES_ROMAN, 24f, Font.BOLD | Font.UNDERLINE, BaseColor.BLACK);

    // Add header image
    iTextSharp.text.Image imgHeader = iTextSharp.text.Image.GetInstance(sImagePath + "/headerImage.png");
    doc.Add(imgHeader);

    iTextSharp.text.Image imgSignature = iTextSharp.text.Image.GetInstance(sImagePath + "/Signature.png");

    // Add user's name, underlined, in the center
    Paragraph pUserName = new Paragraph(sUserName, fntUserName);
    pUserName.Alignment = Element.ALIGN_CENTER;
    doc.Add(pUserName);

    // Here I am trying to add two images and one date text; 
    // below I have added one image and one text; need to have another image in between
    Paragraph para = new Paragraph();
    Phrase ph1 = new Phrase();
    Chunk glue = new Chunk(new iTextSharp.text.pdf.draw.VerticalPositionMark());
    Paragraph main = new Paragraph();
    ph1.Add(new Chunk(imgSignature, 0, 0, true)); // Here I add signature image as a chunk into Phrase.    
    ph1.Add(glue); // Here I add special chunk to the same phrase.    
    ph1.Add(new Chunk(sTestDate, fntDate)); // Here I add date as a chunk into same phrase.    
    main.Add(ph1);
    para.Add(main);
    doc.Add(para);
    doc.Close();
}

0 个答案:

没有答案