0

I'm using ABCpdf7 to create a pdf where I in some instances want to add a "watermark like" text to be shown on all pages in the document. Whenever I make the text unique on all pages, it works as expected, but if it's the same text on all pages, it ignores my alpha property.

I can't seem to use the objectID and reference that when it's not an image, and since the pdf is to be available in many different languages I'm afraid it's not possible to just create an image with the text and add that...

For instance, this works:

theDoc.HPos = 0;
theDoc.VPos = 0;
theDoc.Rect.SetRect(250, 265, 500, 80);
theDoc.Transform.Rotate(55, 250, 265);
theDoc.FontSize = 72;
theDoc.Color.String = "0 0 0 a70";
Page[] pages = theDoc.ObjectSoup.Catalog.Pages.GetPageArray();
foreach (Page p in pages)
{
    theDoc.Page = p.ID;
    var dummy = theDoc.PageNumber.ToString();
    theDoc.AddText("Unpublished" + dummy);
}

...but this doesn't work:

theDoc.HPos = 0;
theDoc.VPos = 0;
theDoc.Rect.SetRect(250, 265, 500, 80);
theDoc.Transform.Rotate(55, 250, 265);
theDoc.FontSize = 72;
theDoc.Color.String = "0 0 0 a70";
Page[] pages = theDoc.ObjectSoup.Catalog.Pages.GetPageArray();
foreach (Page p in pages)
{
    theDoc.Page = p.ID;
    theDoc.AddText("Unpublished");
}

I feel like I'm missing something very obvious here, but can't seem to figure out what...

petstran
  • 101
  • 1
  • 7

3 Answers3

0
theDoc.Transform.Rotate(55, 250, 265);

this line should be called once for the first page, otherwise it will keep rotating for each page.

0

In this case I read a pfd file and create another with the watermark.

BinaryWriter Writer = null;       
                string Name = Server.MapPath(@"~\ventas\" + id_uuid + ".pdf");

                try
                {
                    // Create a new stream to write to the file
                    Writer = new BinaryWriter(File.OpenWrite(Name));

                    // Writer raw data                
                    Writer.Write(ArchivoDownload);
                    Writer.Flush();
                    Writer.Close();
                }
                catch { }

                Doc theDoc = new Doc();
                theDoc.Read(Name);
                int theCount = theDoc.PageCount;

                for (int i = 1; i <= theCount; i++)
                {

                    theDoc.PageNumber = i;
                    theDoc.Pos.String = "23 456";
                    theDoc.FontSize = 90;

                    theDoc.HtmlOptions.HideBackground = true;
                    theDoc.TextStyle.CharSpacing = 5;
                    theDoc.Font = theDoc.AddFont("Helvetica");
                    theDoc.Color.Alpha = 255 / 10;
                    theDoc.Transform.Reset();
                    theDoc.Transform.Rotate(396, 302, 315);
                    theDoc.AddText("WaterMark");
                }

                theDoc.Save(Server.MapPath(@"~\ventas\" + id_uuid + "_c.pdf"));
                theDoc.Clear();
A. Senna
  • 131
  • 1
  • 6
-2

Hi not 100% sure if I understand the issue fully however if it is what I am thinking it is then this may help.

Have you made sure that you are adding the watermark last for everypage. I was having issues with different opacitys when doing a similar thing and it was down to my text being inderneath some objects on one page and then on the top on another. I created a method in my wrapper that I could simply call last on each page to ensure it was placed on top of everything else on the page.

    public void DrawWaterMark(double positionX = -55, double positionY = 130, string text = "APPROVED", double width = 260, double height = 90, TextAlign textAlign = TextAlign.Center, int colourR = 197, int colourG = 197, int colourB = 197, int fontSize = 95)
    {
        // Set text alignment:
        switch (textAlign)
        {
            case TextAlign.Left:
                PdfDocument.HPos = 0;
                break;
            case TextAlign.Center:
                PdfDocument.HPos = 0.5;
                break;
            case TextAlign.Right:
                PdfDocument.HPos = 1;
                break;
        }

        // Set the text colour:
        PdfDocument.Color.String = colourR + " " + colourG + " " + colourB;

        SetFontSize(fontSize);

        // Draw text:
        PdfDocument.Transform.Rotate(45, (PdfDocument.MediaBox.Width / 2), (PdfDocument.MediaBox.Height / 2));

        DrawHtmlString(positionX, positionY, width, height, "<b>" + text + "</b>", TextAlign.Center, colourR, colourG, colourB, 50);

        PdfDocument.Transform.Rotate(-45, (PdfDocument.MediaBox.Width / 2), (PdfDocument.MediaBox.Height / 2));

        SetFontSize(11);
    }
CSharpened
  • 11,674
  • 14
  • 52
  • 86