PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Feb 28, 2020 2:53 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Mar 12, 2008 1:47 pm 
Offline

Joined: Mon Dec 03, 2007 5:33 pm
Posts: 8
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 12, 2008 2:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3027
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 13, 2008 8:06 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3027
Location: Cologne, Germany
Hello!

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

I'll keep you informed.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 13, 2008 9:57 am 
Offline

Joined: Mon Dec 03, 2007 5:33 pm
Posts: 8
Hi!

Thank you very much.

I'm trying to resolve it too.

Regards


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Mar 17, 2008 9:56 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3027
Location: Cologne, Germany
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);
  }
}

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 18, 2008 4:11 pm 
Offline

Joined: Mon Dec 03, 2007 5:33 pm
Posts: 8
Hello!

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

Thank you very much.

Regards.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group