【问题标题】:Adding text to a pdf after an image在图像之后将文本添加到 pdf
【发布时间】:2012-01-31 20:34:04
【问题描述】:

我创建了一个带有图像的 PDF 文档,我正在尝试在每个图像下添加文本。请记住,页面的模板会根据用户在页面上想要的图像数量而有所不同。我的问题是我在添加和定位文本时遇到问题。

添加图片的代码:

int count = 0;

        imageStartX = (docSize.Width / 100) * marginSizeProcent;

        float imageMaxHeight = 0;
        float imageMaxWidth = 0;

        iTextSharp.text.Image image = null;

        switch (pageLayout)
        {
            case PageLayoutEnum.SingleImage:

                imageMaxWidth = docSize.Width - ((docSize.Width/100) * (2 * (float)marginSizeProcent));
                imageMaxHeight = imageStartY - ((docSize.Width/100) * (float)marginSizeProcent);


                foreach (PDFObject o in pdfObjects)
                {
                    if (count > 0)
                        AddPageWithHeader(false);

                    image = iTextSharp.text.Image.GetInstance(o.File);
                    image.ScaleToFit(imageMaxWidth, imageMaxHeight);
                    image.SetAbsolutePosition(imageStartX + (imageMaxWidth - image.ScaledWidth) / 2, imageStartY - image.ScaledHeight - (imageMaxHeight - image.ScaledHeight) / 2);

                    image.Border = Rectangle.BOX;
                    image.BorderWidth = 2f;
                    image.BorderColor = BaseColor.DARK_GRAY;

                    document.Add(image);

                    count++;
                }

                break;

            case PageLayoutEnum.TwoImages:

添加文字的代码:

MemoryStream memoryStream = new MemoryStream();

        PdfReader pdfReader = new PdfReader(documentStream.ToArray());
        PdfStamper stamper = new PdfStamper(pdfReader, memoryStream);

        PdfContentByte contentbyte = stamper.GetUnderContent(1);
        ColumnText dispalyIdText = new ColumnText(contentbyte);
        Paragraph idText;

        int counter = 0;

        switch (pageLayout)
        {
            case PageLayoutEnum.SingleImage:
                foreach (PDFObject item in pdfObjects)
                {
                    dispalyIdText.SetSimpleColumn(200, 200, 200, 200, 200, Element.ALIGN_LEFT);
                    idText = new Paragraph(new Chunk(item.DisplayId, FontFactory.GetFont("Arial", 20, Font.BOLD, BaseColor.RED)));
                    dispalyIdText.AddElement(idText);
                }
                break;

            case PageLayoutEnum.TwoImages:

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    你没有说你的实际问题是什么,只是说你遇到了这些问题。

    如果我猜的话,您的问题之一是文本实际上并未显示在您的 PDF 中。这有三个不同的原因。首先是这一行:

    dispalyIdText.SetSimpleColumn(200, 200, 200, 200, 200, Element.ALIGN_LEFT);
    

    此方法的前四个参数是您要将绘图约束到的矩形的坐标。第一个参数是左下x,第二个是左下y,第三个是右上x,第四个是右上y。在您的代码中,您说要将文本绑定到左下坐标为200,200 和右上坐标为200,200 的矩形。这意味着您的矩形的宽度和高度为零。要解决此问题,您需要提供一个实际有效的矩形。在 PDF 中,左下角是 0,0,因此要在左下角的 20 像素(实际上不是像素,但这是另一回事)高和 200 宽的矩形中绘制文本做:

    dispalyIdText.SetSimpleColumn(0, 0, 200, 20, 200, Element.ALIGN_LEFT);
    

    您的第二个问题是您将leading(行高)设置为200。根据您创建的对象,这可能会或可能不会将文本吹出。您应该将其设置为更合理的值,可能是字体的高度。这不会影响AddElement,但会影响SetText

    dispalyIdText.SetSimpleColumn(0, 0, 200, 20, 12, Element.ALIGN_LEFT);
    

    最后一个问题是,当使用ColumnText 时,您现在处于“文本”模式,并且必须在准备好开始处理时通知系统。您可以通过发出Go() 命令来做到这一点:

    dispalyIdText.Go();
    

    【讨论】:

    • 感谢 Chris,我找到了另一种使用 PDFContentByte 显示和定位文本的解决方案。通常你很适合解决问题,所以我猜你也适合这个!如果我必须重新访问这部分代码,那么我会尝试您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多