Print Page | Close Window

How to set the background color

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=1622
Printed Date: 25 Apr 25 at 10:05AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to set the background color
Posted By: wjbons
Subject: How to set the background color
Date Posted: 01 Nov 10 at 8:44AM
Hi,
How can I change the background color (normal white) of a loaded pdf document?



Replies:
Posted By: Dimitry
Date Posted: 01 Nov 10 at 6:19PM

The most common way to set page background color is just to DrawBox() of specified color on it.

Here is code snippet that may be useful for you:
 
// QPL: TQuickPDF;
var
  colordialog: TColorDialog;
 
begin
  colordialog := TColorDialog.Create(nil);
  if not(colordialog.Execute) then
    Exit;
 
  with QPL do
  begin
    SetOrigin(1);
    SetFillColor(GetRValue(colordialog.Color)/255,
      GetGValue(colordialog.Color)/255,
      GetBValue(colordialog.Color)/255);
    DrawBox(0, 0, PageWidth, PageHeight, 1);
  end;

  colordialog.Free;
end;


-------------
Regards,
Dmitry


Posted By: wjbons
Date Posted: 02 Nov 10 at 10:41AM
Next code shows what i do (visual studio 2010)
I see that i get a blank area returned, but no color.
Second the box is drawn on top of all content but i do not want to harm the document anyway, i just want to change the background color for all pages...
 
QP = New PDFLibrary("C:\...\QuickPDFDLL0722.dll")

Result = QP.UnlockKey(LicenseKey)

If Result = 1 Then

Dim T As Long = Now.Ticks

QP.LoadFromFile("c:\somefile.pdf")

Dim w As Integer = QP.PageWidth()

Dim h As Integer = QP.PageHeight()

QP.SetOrigin(1)

QP.SetFillColor(225, 115, 115)

QP.DrawBox(0, 0, w, h, 1)

Dim Output() As Byte = QP.RenderPageToString(96, 0, 5) 'png

Dim byteStream As IO.MemoryStream = New IO.MemoryStream(Output)

Dim Img As New BitmapImage

Img.BeginInit()

Img.StreamSource = byteStream

Img.EndInit()

Dim Pic As New Image

Pic.Source = Img

Pic.Stretch = Stretch.None



Posted By: Dimitry
Date Posted: 02 Nov 10 at 1:36PM
While defining colors in QuickPDF Library the values should belong to the range
from 0 to 1, where 0 indicates 0% and 1 indicates 100% of the color.
So the code will look like:
 
QP.SetFillColor(225/255, 115/255, 115/255)


-------------
Regards,
Dmitry


Posted By: samuelms
Date Posted: 15 Jul 12 at 4:02AM
Just be careful about integer division!  :)

Perhaps...

QP.SetFillColor(225/255.0f, 115/255.0f, 115/255.0f)


Posted By: AndrewC
Date Posted: 16 Jul 12 at 5:01AM
Depending on the PDF file this could be quite difficult.  You would need to somehow inject the drawing commands for coloured rectangle into the contentstream.  The are a couple of ways.

Assuming a 1 page document, here is some pseudocode that might work.

  QP.LoadFromFile
  QP.NewPage
  QP.SelectPage(1)
  id = QP.CapturePage()
  QP.SetFillColor(1,0,0)
  QP.DrawBox(0,0,QP.PageWidth(), QP.PageHeight(), 1)
  QP.DrawCapturedPage(id ...
  QP.SaveToFile

Some PDF files may draw their own WHITE rectangle as a background before they start drawing so I will not be possible for all PDF files.

You might find it easier to let QPL perfrorm the RenderPageToString and then use GDI+ and VB.NET to convert all white pixels to your background colour.  Thiis would work reasonably well for most PDF files.

Andrew.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk