PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Watermark and scanned documents
https://forum.pdfsharp.net/viewtopic.php?f=3&t=337
Page 1 of 1

Author:  opa [ Wed Mar 12, 2008 1:47 pm ]
Post subject:  Watermark and scanned documents

Hello.

I'm trying to put watermark in a pdf document. After many tests, I have a problem with certain pdf documents. These documents are composed of pages with text, images and "scanned documents". In the pages that include "scanned documents" I recieve the "Unknown token 'QQ'" or "Unknown token 'DoQ'".

Can you help me?

Thanks for advance

Author:  Thomas Hoevel [ Wed Mar 12, 2008 2:50 pm ]
Post subject: 

Hi!
To replicate this problem we need a PDF file that causes this error.

I'll send you a PM with an e-mail address for file submission.

Author:  Thomas Hoevel [ Thu Mar 13, 2008 8:06 am ]
Post subject: 

Hello!

I can replicate the error, but other team members will have to fix it ...

I'll keep you informed.

Author:  opa [ Thu Mar 13, 2008 9:57 am ]
Post subject:  Watermark and scanned documents

Hi!

Thank you very much.

I'm trying to resolve it too.

Regards

Author:  Thomas Hoevel [ Mon Mar 17, 2008 9:56 am ]
Post subject: 

Hello!

We found a solution for your problem.

In PdfContent.cs (around line 124) change method
"internal void PreserveGraphicsState()"
like this:
Code:
internal void PreserveGraphicsState()
{
  // If a content stream is touched by PDFsharp it is typically because graphical operations are
  // prepended or appended. Some nasty PDF tools does not preserve the graphical state correctly.
  // Therefore we try to relieve the problem by surrounding the content stream with push/restore
  // graphic state operation.
  if (this.Stream != null)
  {
    byte[] value = Stream.Value;
    int length = value.Length;
    if (length != 0 && ((value[0] != (byte)'q' || value[1] != (byte)'\n')))
    {
      byte[] newValue = new byte[length + 2 + 3];
      newValue[0] = (byte)'q';
      newValue[1] = (byte)'\n';
      Array.Copy(value, 0, newValue, 2, length);
      newValue[length + 2] = (byte)' ';
      newValue[length + 3] = (byte)'Q';
      newValue[length + 4] = (byte)'\n';
      Stream.Value = newValue;
      Elements.SetInteger("/Length", Stream.Length);
    }
  }
}


In file PdfContents.cs (around line 142) change method "void SetModified()" like this:
Code:
else if (count > 1)
{
  // Surround content streams with q/Q operations
  byte[] value;
  int length;
  PdfContent content = (PdfContent)((PdfReference)Elements[0]).Value;
  if (content != null && content.Stream != null)
  {
    length = content.Stream.Length;
    value = new byte[length + 2];
    value[0] = (byte)'q';
    value[1] = (byte)'\n';
    Array.Copy(content.Stream.Value, 0, value, 2, length);
    content.Stream.Value = value;
    content.Elements.SetInteger("/Length", length + 2);
  }
  content = (PdfContent)((PdfReference)Elements[count - 1]).Value;
  if (content != null && content.Stream != null)
  {
    length = content.Stream.Length;
    value = new byte[length + 3];
    Array.Copy(content.Stream.Value, 0, value, 0, length);
    value[length] = (byte)' ';
    value[length + 1] = (byte)'Q';
    value[length + 2] = (byte)'\n';
    content.Stream.Value = value;
    content.Elements.SetInteger("/Length", length + 3);
  }
}

Author:  opa [ Tue Mar 18, 2008 4:11 pm ]
Post subject: 

Hello!

I have updated the code and works correctly. Have solved all my problems.

Thank you very much.

Regards.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/