Print Page | Close Window

Stamping with a rotated shadowed text + www link

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2081
Printed Date: 22 Nov 24 at 6:25PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Stamping with a rotated shadowed text + www link
Posted By: AndrewC
Subject: Stamping with a rotated shadowed text + www link
Date Posted: 27 Dec 11 at 5:56AM

Here is some C# to add a rotated text object with a shadow and then on top of this we add a web link annotation.

Enjoy,

Andrew.

            QP.LoadFromFile("unstamped.pdf", "");

            int fid1 = QP.AddStandardFont(4);      // Helvetica    
            double angle = 30;

            for (int p = 1; p <= QP.PageCount(); p++) /* Loop through all pages in doc */
            {
                QP.SelectPage(p);

                QP.SetOrigin(1);            // Top left
                QP.SetMeasurementUnits(0);  // Points

                QP.SelectFont(fid1);
                QP.SetTextAlign(1);         // Align text center

                QP.SetTextSize(72);

                QP.SetTextColor(0, 0, 0);
                QP.DrawRotatedText(QP.PageWidth() / 2 + 3, QP.PageHeight() / 2 + 3, angle, "www.debenu.com"); // Draw Watermark at center

                QP.SetTextColor(0, 0, 1);
                QP.DrawRotatedText(QP.PageWidth() / 2, QP.PageHeight() / 2, angle, "www.debenu.com"); // Draw Watermark at center

                double tx = QP.GetTextWidth("www.debenu.com") / 2 * Math.Cos(angle * Math.PI / 180);
                double ty = QP.GetTextWidth("www.debenu.com") / 2 * Math.Sin(angle * Math.PI / 180);

                double x, y, w, h;
                double hx = QP.GetTextAscent() * Math.Sin(angle * Math.PI / 180);
                double hy = QP.GetTextAscent() * Math.Cos(angle * Math.PI / 180);

                x = QP.PageWidth() / 2 - tx - hx;
                y = QP.PageHeight() / 2 - ty - hy;
                w = tx * 2 + hx;
                h = ty * 2 + hy;
                QP.AddLinkToWeb(x, y, w, h, "http://www.debenu.com", 0);

                QP.SetTextSize(132);
                QP.SetTextColor(1, 0, 0);
                QP.DrawRotatedText(QP.PageWidth() / 2, QP.PageHeight() / 2 - 80, angle, "DEMO"); // Draw Watermark at center

            }

            QP.SaveToFile("out.pdf");




Replies:
Posted By: edvoigt
Date Posted: 30 Dec 11 at 9:30PM
Hi,

this is a nice demo. Here is a transcription to Delphi. I want to have the watermark more realistic in the background. But how to do this?

Remember it is possible to capture a page. On a new page with the same size we draw with the code of andrew a watermark and later we draw over it the captured original.

So it looks then:

uses Math;
...
var
  fid, pid, p, n: integer;
  angle, tx, ty, hx, hy,
  x, y, w, h,
  pw, ph: double;
  sin, cos: extended;
  wm: string;
begin
  QP := TQuickPDF.Create;
  QP.UnlockKey(...);
  QP.LoadFromFile('unstamped.pdf', '');
  fid := QP.AddStandardFont(4); // Helvetica
  angle := 30;
  wm := 'www.debenu.com';

  for p := 1 to QP.PageCount    // Loop through all pages in doc
  do begin
    QP.SelectPage(p);           // note the pagesize
    pw := QP.PageWidth;
    ph := QP.PageHeight;
    n := QP.NewPage;            // is the empty new last page
    pid := QP.CapturePage(p);
    QP.SelectPage(n-1);         // -1 because capture deletes the page
    QP.MovePage(p);             // new page on the old place
    QP.SetPageDimensions(pw, ph); // size of the original page
//
// the watermark part
    QP.SetOrigin(1);            // Top left
    QP.SetMeasurementUnits(0);  // Points
    QP.SelectFont(fid);

    QP.SetTextAlign(1);         // Align text center
    QP.SetTextSize(72);
    QP.SetTextColor(0, 0, 0);   // simple shadow
    QP.DrawRotatedText(pw/2 + 3, ph/2 + 3, angle, wm); // Draw Watermark at center
    QP.SetTextColor(0, 0, 1);
    QP.DrawRotatedText(pw/2,     ph/2,     angle, wm); // Draw Watermark at center
    sincos(angle*pi/180, sin, cos);  // calc the anglefunctions only once
    tx := QP.GetTextWidth(wm)/2*cos;
    ty := QP.GetTextWidth(wm)/2*sin;

    hx := QP.GetTextAscent*sin;
    hy := QP.GetTextAscent*cos;

    x := pw/2-tx-hx;
    y := ph/2-ty-hy;
    w := tx * 2 + hx;
    h := ty * 2 + hy;
    QP.AddLinkToWeb(x, y, w, h, 'http://www.debenu.com', 0);

    QP.SetTextSize(132);
    QP.SetTextColor(1, 0, 0);
    QP.DrawRotatedText(pw/2, ph/2-80, angle, 'DEMO'); // Draw Watermark at center

// stamp the captured original content over the watermark
    QP.DrawCapturedPage(pid, 0, 0, pw, ph);
  end;
  QP.SaveToFile('out.pdf');


Happy new year,

Werner



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