PDF Watermark for printing only, programmatically

前端 未结 4 694
鱼传尺愫
鱼传尺愫 2020-12-17 05:49

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?

<
相关标签:
4条回答
  • 2020-12-17 06:33

    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.

    0 讨论(0)
  • 2020-12-17 06:37

    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

    0 讨论(0)
  • 2020-12-17 06:41

    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.

    0 讨论(0)
  • 2020-12-17 06:48

    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();
    
    0 讨论(0)
提交回复
热议问题