PDF Watermark for printing only, programmatically

前提是你 提交于 2019-12-29 06:33:30

问题


I can watermark any PDF already, and the images inside, everything ok, but now I need the watermark only showing up when the PDF is printed... Is this possible? How?

I need to do this programmatically of course.


回答1:


You should probably make use of the fact that the screen uses RGB and the printer CMYK. You should be able to create two colors in CMYK that map to the same RGB value. This is of course not enough against a determined specialist.




回答2:


For future readers, this is possible to do by wrapping the watermark in a PDF layer (Optional Content Group), then configuring the Usage attribute of this layer as Print-Only. See the PDF Reference Document, Chapter 4-Graphics, part 4.10-Optional Content for more details.




回答3:


Specifically, using itextsharp, I was able to get it working with the following, specifically - pdf version 1.7, and SetPrint("Watermark",true)

        string oldfile = @"c:\temp\oldfile.pdf";
        string newFile = @"c:\temp\newfile.pdf";
        PdfReader pdfReaderS = new PdfReader(oldfile);
        Document document = new Document(pdfReaderS.GetPageSizeWithRotation(1));
        PdfWriter pdfWriterD = PdfWriter.GetInstance(document, new FileStream(newFile, FileMode.Create, FileAccess.Write));
        pdfWriterD.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
        document.Open();
        PdfContentByte pdfContentByteD = pdfWriterD.DirectContent;

        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

        int n = pdfReaderS.NumberOfPages;

        string text = "UNCONTROLLED";

        for (int i = 1; i <= n; i++)
        {
            iTextSharp.text.Rectangle pageSizeS = pdfReaderS.GetPageSizeWithRotation(i);
            float pageWidth = pageSizeS.Width / 2;
            float pageheight = pageSizeS.Height / 2;

            document.SetPageSize(pageSizeS);
            document.NewPage();
            PdfImportedPage pdfImportedPage = pdfWriterD.GetImportedPage(pdfReaderS, i);

            PdfLayer layer1 = new PdfLayer("Watermark", pdfWriterD);
            layer1.SetPrint("Watermark", true);
            layer1.View = false;
            layer1.On = false;
            layer1.OnPanel = false;

            pdfContentByteD.BeginLayer(layer1);
            pdfContentByteD.SetColorFill(BaseColor.RED);
            pdfContentByteD.SetFontAndSize(bf, 30);

            ColumnText.ShowTextAligned(pdfContentByteD, Element.ALIGN_CENTER, new Phrase(text), 300, 700, 0);
            pdfContentByteD.EndLayer();

            pdfContentByteD.AddTemplate(pdfImportedPage, 0, 0);//, 0, 1, 0, 0);

        }
        document.Close();
        pdfReaderS.Close();



回答4:


The bOnScreen parameter determines whether the watermark will be displayed when the PDF is viewed on the computer screen, and bOnPrint determines whether it will be displayed when the PDF is printed.

-- https://acrobatusers.com/tutorials/watermarking-a-pdf-with-javascript



来源:https://stackoverflow.com/questions/2022972/pdf-watermark-for-printing-only-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!