c# - 如何在 C# 中使用 iTextSharp 将添加的图像带到最前沿

标签 c# .net pdf pdf-generation itextsharp

好的,我必须制作一个具有某些样式的 PDF。是为了标签。我有以下代码:

Document doc = new Document(PageSize.A4);

    int labelHeight = 394;
    int labelWidth = 556;

    float labelTop = doc.PageSize.Height - 2;
    float labelBottom = doc.PageSize.Height - labelHeight;

    MemoryStream ms = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(doc, ms);
    doc.Open();

    PdfContentByte cb = writer.DirectContent;

    // Generate Boxes For Content

    cb.SetColorStroke(new CMYKColor(207, 115, 255, 133));
    cb.SetLineWidth(4);
    cb.SetColorFill(new CMYKColor(207, 71, 255, 38));

    cb.MoveTo(2, labelBottom);
    cb.LineTo(labelWidth - 2, labelBottom);
    cb.LineTo(labelWidth - 2, labelTop);
    cb.LineTo(2, labelTop);

    cb.ClosePathFillStroke(); // Draw Main Green Box

    cb.SetColorStroke(new CMYKColor(0, 0, 0, 0));
    cb.SetLineWidth(1);
    cb.SetColorFill(new CMYKColor(0, 0, 0, 0));

    int whiteBoxTop = 100;
    int whiteBoxHeight = 260;

    cb.MoveTo(5, labelTop - (whiteBoxTop + whiteBoxHeight));
    cb.LineTo(labelWidth - 5, labelTop - (whiteBoxTop + whiteBoxHeight));
    cb.LineTo(labelWidth - 5, labelTop - whiteBoxTop);
    cb.LineTo(5, labelTop - whiteBoxTop);

    cb.ClosePathFillStroke(); // Draw Inner White Content Box

    cb.SetColorStroke(new CMYKColor(20, 15, 41, 0));
    cb.SetLineWidth(0);
    cb.SetColorFill(new CMYKColor(20, 15, 41, 0));

    cb.MoveTo(labelWidth / 2, labelTop - ((whiteBoxTop + whiteBoxHeight) - 4));
    cb.LineTo(labelWidth - 9, labelTop - ((whiteBoxTop + whiteBoxHeight) - 4));
    cb.LineTo(labelWidth - 9, labelTop - (whiteBoxTop + 4));
    cb.LineTo(labelWidth / 2, labelTop - (whiteBoxTop + 4));

    cb.ClosePathFillStroke();

    string logoPath = Server.MapPath(".") + "/pdf_logo.jpg";

    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(logoPath);

    logo.SetAbsolutePosition(labelWidth / 4, labelTop - (whiteBoxTop + whiteBoxHeight) + 25);
    logo.ScaleToFit(150, 30);


    doc.Add(logo);    

    doc.Close();

问题在于添加的 Logo 。如果我添加它并将它显示在我正在创建的框下方,它就会显示出来,但是当我将绝对位置放在框所在的位置时,它会消失在绘制的框后面。我需要做什么才能将图像放在我绘制的框上方?对于我尝试添加到框顶部的任何文本,也会发生同样的问题。

最佳答案

尝试将 Logo 添加到您的 PdfContentByte 对象而不是您的 Document

替换这个:

doc.Add(logo);

用这个:

cb.AddImage(logo);

关于c# - 如何在 C# 中使用 iTextSharp 将添加的图像带到最前沿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158274/

相关文章:

c# - 语言间的方法差异(Python->C#)

C# System.Diagnostics.Process 动词问题

.net - 将敏感信息放入 Web.config 文件中是一种不好的做法吗?

java - 如何获取特定 pdf 表单字段的页码和位置,并在 iText7 中的相应位置插入表格?

c# - 如何在 C# 中计算用户输入的字符串?

C# 提取对象上的局部变量名称。不是通用类名

.net - UPS Worldship 集成和 .net

iphone - 如何合并带有注释的 PDF 文件

python - 有没有办法确定 PDF 是 "real"PDF 还是 PNG 的包装器

c# - 使用数组进行非托管导出