【问题标题】:Adding watermark to razorpdf mvc将水印添加到razorpdf mvc
【发布时间】:2014-11-03 04:05:06
【问题描述】:

我正在使用以下代码在 mvc 中使用 itext 和 razorpdf producer 生成 pdf

@model myModel.Models.Examform
@{
    Layout = null;
}
<itext creationdate="@DateTime.Now.ToString()" producer="RazorPDF">
    Hello
</text>

此代码运行良好。我想在生成的 pdf 中添加水印。我知道如何添加图像。我需要一个在后台显示的水印。

【问题讨论】:

  • 我用 iTextSharp 解决了这个问题,和 itext 一样吗?

标签: c# asp.net-mvc razor itextsharp razorpdf


【解决方案1】:

这几乎是同一个问题

ItextSharp - RazorPdf put image on Pdf

所以使用那里的答案应该对你有用:

<image url="@Context.Server.MapPath("~/Images/sampleImage.png")" />

编辑:要在图像上叠加文本,您需要修改 CSS 和 HTML。

How to position text over an image in css

Add "Watermark" effect with CSS?

http://www.the-art-of-web.com/css/textoverimage/

可能需要将 CSS 内联。

【讨论】:

  • 我知道如何添加图片。我想知道如何添加水印或将在背景中显示的图像。
  • @MaryPhelps - 我在答案中添加了更多内容。
  • 您提到的所有选项都不起作用。它们都可以在普通的 html 页面中工作。它们在 razorpdf 的情况下不起作用。
  • 好吧,我们的做法是使用 rotativa 库nuget.org/packages/Rotativa
【解决方案2】:

这就是我所做的。代码进入控制器。

[HttpPost]
public FileStreamResult Certificate(MyModel model)
{
    Stream fileStream = GeneratePDF(model);
    HttpContext.Response.AddHeader("content-disposition", "inline; filename=Certificate.pdf");

    var fileStreamResult = new FileStreamResult(fileStream, "application/pdf");
    return fileStreamResult;
}

public Stream GeneratePDF(HomeViewModel model)
{ 
    var rect = new Rectangle(288f, 144f);
    var doc = new Document(rect, 0, 0, 0, 0);

    BaseFont bfArialNarrow = BaseFont.CreateFont(Server.MapPath("../Content/fonts/ARIALN.ttf"), BaseFont.CP1252, BaseFont.EMBEDDED);

    //Full Background Image (Includes watermark)
    Image fullBackground = null;
    fullBackground = Image.GetInstance(Server.MapPath("../Content/images/Certificate/Cert1.jpg"));

    doc.SetPageSize(PageSize.LETTER.Rotate());

    MemoryStream memoryStream = new MemoryStream();
    PdfWriter pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
    doc.Open();

    //Full Background Image
    fullBackground.Alignment = Image.UNDERLYING | Image.ALIGN_CENTER | Image.ALIGN_MIDDLE;
    doc.Add(fullBackground);

    Font myFont = new Font(bfArialNarrow, 57);
    var myParagraph = new Paragraph("Some text here.", myFont);
    doc.Add(myParagraph);

    pdfWriter.CloseStream = false;
    doc.Close();

    memoryStream.Position = 0;

    return memoryStream;
}

【讨论】:

    【解决方案3】:

    我认为通过标记是不可能的。

    如果您查看 RazorPDF 源代码中的 PdfView.cs,它使用 iTextsharpt 中的 XmlParser 或 HtmlParser 来呈现 pdf。

    https://github.com/RazorAnt/RazorPDF/blob/master/RazorPDF/PdfView.cs

    这两个类中对标记的支持是有限的。你只能做他们已经实施的事情。

    另一种方法是使用 iTextsharp 通过代码创建 pdf。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多