【问题标题】:ItextSharp add image on center of page with the text under itItextSharp 在页面中心添加图像及其下方的文本
【发布时间】:2017-02-01 09:03:00
【问题描述】:

我正在尝试在带有文本的页面 PDF 文件的中心(中间)添加图片,但我做不到。我用于图片SetAbsolutePosition,但图片下方没有文字。

我的 Pdf 文件中需要下一个页面格式:

我使用下一个代码:

                PdfWriter writer = PdfWriter.GetInstance(doc, fs);

                ITextEvents ev = new ITextEvents();
                writer.PageEvent = ev;
                doc.Open();

                var paragraph = new Paragraph();
                var paragraph1 = new Paragraph();
                var chunk = new Chunk("Text under picture", f14nb);
                var chunk1 = new Chunk("Code of picture", f14);

                img = ScaleImg(Image.GetInstance(imgNane_2));
                img.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth) / 2,
                    ((PageSize.A4.Height - img.ScaledHeight) / 2));
                
                paragraph.Add(img);
                paragraph1.Add(chunk);
                paragraph1.Add(chunk1);
                doc.Add(paragraph);
                doc.Add(paragraph1);
                
                doc.Close();

private Image ScaleImg(Image img)
{
    if (img.Height > img.Width)
    {
        //Maximum height is 800 pixels.
        float percentage = 0.0f;
        percentage = 640 / img.Height;
        img.ScalePercent(percentage * 100);
    }
    else
    {
        //Maximum width is 600 pixels.
        float percentage = 0.0f;
        percentage = 500 / img.Width;
        img.ScalePercent(percentage * 100);
    }
    return img;
}

我想,我应该用另一种方法来解决我的问题,但我不知道是哪种。

谢谢。

【问题讨论】:

    标签: c# image pdf text itext


    【解决方案1】:

    我明白该怎么做。

    这是用于带有文字的图像:

            public Image getWatermarkedImage(Document Doc, Image img, String watermarkText)
        {
            float width = img.ScaledWidth;
            float height = img.ScaledHeight;
    
            PdfTemplate template = cb.CreateTemplate(width, height);
    
            template.AddImage(img, width, 0, 0, height, 0, 0);
            ColumnText.ShowTextAligned(template, Element.ALIGN_RIGHT,
                new Phrase(watermarkText, fontBold_14), width - 10, 10, 0);
    
            return Image.GetInstance(template);
        }
    

    这是用于在图像下添加文本(主要代码):

                    var codeOfPicture = "*Code of picture* - *Код картинки*";
    
                    var chunk = new Chunk("Text under picture", font_14);
    
                    img = ScaleImg(Image.GetInstance(imgNane_1));//imgNane_2));
    
                    var tmpImg = getWatermarkedImage(doc, img, codeOfPicture);
                    var textY = ((PageSize.A4.Height - img.ScaledHeight) / 2) - 15;
                    var textX = PageSize.A4.Width / 2;
    
                    tmpImg.SetAbsolutePosition((PageSize.A4.Width - img.ScaledWidth) / 2,
                        ((PageSize.A4.Height - img.ScaledHeight) / 2));
    
                    doc.Add(tmpImg);
    
                    ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(chunk), textX, textY, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      相关资源
      最近更新 更多