简体   繁体   English

在razorpdf mvc上添加水印

[英]Adding watermark to razorpdf mvc

I am using the following code to generate pdf in mvc using itext with razorpdf producer 我正在使用以下代码使用带有razorpdf生成器的itext在mvc中生成pdf

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

This code works fine. 这段代码工作正常。 I want to add a watermark to the generated pdf. 我想为生成的pdf添加水印。 I know how to add the image. 我知道如何添加图像。 I need a watermark which shows in the background. 我需要一个在后台显示的水印。

this is pretty much the exact same question 这几乎是完全相同的问题

ItextSharp - RazorPdf put image on Pdf ItextSharp - RazorPdf将图像放在Pdf上

so using the answer there should work for you: 所以使用答案应该适合你:

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

Edit: To overlay text on the image, you need to modify your CSS and HTML. 编辑:要覆盖图像上的文本,您需要修改CSS和HTML。

How to position text over an image in css 如何在css中的图像上定位文本

Add "Watermark" effect with CSS? 用CSS添加“水印”效果?

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

You might need to put the CSS inline. 可能需要将CSS内联。

Here is what I have done. 这就是我所做的。 The code goes in the controller. 代码进入控制器。

[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;
}

I think it is not possible via markup. 我认为通过标记是不可能的。

If you have a look at PdfView.cs in RazorPDF source code, it uses XmlParser or HtmlParser in iTextsharpt to render pdf. 如果你看一下RazorPDF源代码中的PdfView.cs,它会在iTextsharpt中使用XmlParser或HtmlParser来渲染pdf。

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

The support of markups in these two classes are limited. 这两个类中的标记支持是有限的。 You can only do what they have implemented. 你只能做他们已经实施的事情。

The alternative way is to use iTextsharp to create pdf via code. 另一种方法是使用iTextsharp通过代码创建pdf。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM