Search code examples
c#nreco

NReco PdfGenerator disable selection


I want to disable selection, just like an image, even if contain text.

I know PDF contain layers, but i dont find where to remove the text layer.

Thanks.

        var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
        htmlToPdf.Size = NReco.PdfGenerator.PageSize.Letter;

        htmlToPdf.Margins = new NReco.PdfGenerator.PageMargins()
        {
            Bottom = 0,
            Top = 0,
            Left = 0,
            Right = 0
        };
        string ID = "Test";
        Response.ContentType = "application/pdf";

        Response.AppendHeader("content-disposition", string.Format("inline;FileName=\"{0}.pdf\"", ID));

        htmlToPdf.GeneratePdfFromFile("Page.aspx", null, Response.OutputStream);

        Response.End();

Solution

  • PdfGenerator internally uses wkhtmltopdf and it renders text content as text blocks and they are selectable by default; this behavior cannot be changed. If you want to guarantee that text cannot be selected it should be rendered as image (= you can render HTML to image first, and then produce PDF that contains this image).

    Another alternative is encryption of produced PDF with iTextSharp, see answer for Lock PDF against editing using iTextSharp - with only PdfWriter.ALLOW_PRINTING option (LGPL version 4.1.6 of iTextSharp can be used for this purpose).