0

I am trying to flatten bitmap images in a PDF document as opposed to keep separatelayers. For example, let's say I have a document with two square images that are partially overlaying each other. I would like to merge them so that the user cannot individually select one of the squares to copy it out of the document. They'll be able to select both, I would think, but I don't want them to be able to isolate one of them. My client has a more complicated reason for wanting this restriction, but this is the simplest explanation. I would like to solve this with iTextSharp, but another product would be fine with me. I have used iTextSharp for form flattening, but I can't figure out how to flatten images. Thank you.

Edit

I realized another solution might just be to prevent selection within the document, which would hopefully prevent copying and pasting. I would guess that all document readers would not have to abide by my command to prevent selection, but as long as Adobe Reader (and maybe Foxit Reader) do abide by it, that should be good enough.

user1325179
  • 1,261
  • 17
  • 27
  • What kind of images are you talking about? Bitmap images? Vector images? Or some annotation appearances? – mkl Apr 07 '15 at 22:35
  • @mkl, bitmaps. I just updated the question. Thanks. – user1325179 Apr 07 '15 at 22:38
  • 1
    Hmm, the general case may be non-trivial, considering different color spaces, interesting blending options, transparency, etc. Are you interested in that general case or only simpler ones? AFAIK this "image flattening" is not a common creature in PDF libraries. – mkl Apr 07 '15 at 22:54
  • @mkl, the actual case involves mixing images of several types along with text, annotations, form fields, and whatever else. I just wanted to start with something simple. – user1325179 Apr 07 '15 at 23:10
  • @mkl, please see my edit. Thanks. – user1325179 Apr 07 '15 at 23:15
  • I'll look into it done more later. – mkl Apr 08 '15 at 09:59
  • *another solution might just be to prevent selection within the document* - you might consider putting the material to make unselectable into a pattern. Cf. [this answer](http://stackoverflow.com/a/20361261/1729265) which uses patterns to make watermarks unselectable. Obviously such a method may break in some future PDF viewer version which makes pattern elements seelctable... – mkl Apr 09 '15 at 21:49
  • Have you tried the Pattern approach? I though some more about the merging of the images, and saw more and more problems arising. Unless you want to solve it like Capitán Cavernícola in his answer, i.e. flattening the whole page into an image, you need to merge entities which may be located in very different places with utterly different states active, and all this without disturbing all other entities... Non-trivial to say the least. – mkl Apr 13 '15 at 14:51

1 Answers1

0

As you say you can use other products, I'll show a way to do merge layers using ABCpdf

    Dim oDoc As New WebSupergoo.ABCpdf7.Doc
    Using oDoc
        oDoc.Read("D:\example.pdf")

        Dim iTotal As Integer = oDoc.PageCount()
        For i As Integer = 1 To iTotal
            oDoc.PageNumber = 1
            oDoc.Rendering.Save("D:\" & i & ".JPG")
            oDoc.Delete(oDoc.Page)
        Next
        For i As Integer = 1 To iTotal
            oDoc.AddPage()
            oDoc.AddImage("D:\" & i & ".JPG")
            oDoc.Flatten()
        Next
        oDoc.Save("D:\example_abc.pdf")
    End Using

Original: https://encodable.com/cgi-bin/filechucker.cgi?action=landing&path=/SOabcpdf/&file=example.pdf

Procesed: https://encodable.com/cgi-bin/filechucker.cgi?action=landing&path=/SOabcpdf/&file=example_abc.pdf

You have to change the quality, reading ABCpdf Help. http://www.websupergoo.com/helppdfnet/source/4-examples/19-rendering.htm

Edit: As you don't want only merge image, but the copy protection, ABcPDF has this:

  Dim oDoc As New WebSupergoo.ABCpdf7.Doc
    Using oDoc
        oDoc.Read("D:\example.pdf")

        oDoc.Encryption.Type = 2
        oDoc.Encryption.CanCopy = False
        oDoc.Encryption.OwnerPassword = "password"

        oDoc.Save("D:\example_abc.pdf")
    End Using
Morcilla de Arroz
  • 1,964
  • 23
  • 28
  • Your PDF links result in `404 - page not found for http://encodable.com/uploaddemo/files/SOabcpdf/example.pdf Referring page: https://encodable.com/cgi-bin/filechucker.cgi?action=landing&path=/SOabcpdf/&file=example.pdf `... – mkl Apr 09 '15 at 21:41
  • That been stated you essentialy seem to draw the whole page as a JPEG image and then replace it by a new page only containing that JPEG. This is not really what the OP asked for. Furthermore the quality either is horrible or the file size is enormous. – mkl Apr 09 '15 at 21:46
  • As I told you, you can improve quality. http://www.websupergoo.com/helppdfnet/source/4-examples/19-rendering.htm Anyway, I have edit with a distinct way – Morcilla de Arroz Apr 10 '15 at 06:43