0

我正在使用 ABCpdf7 创建一个 pdf,在某些情况下我想添加一个“类似水印”的文本以显示在文档的所有页面上。每当我在所有页面上使文本唯一时,它都会按预期工作,但如果它在所有页面上都是相同的文本,它会忽略我的 alpha 属性。

我似乎无法使用 objectID 并在它不是图像时引用它,并且由于 pdf 将提供多种不同语言的版本,因此恐怕无法仅使用文本创建图像并添加它.. .

例如,这有效:

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);
}

...但这不起作用:

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");
}

我觉得我在这里遗漏了一些非常明显的东西,但似乎无法弄清楚是什么......

4

3 回答 3

0

在这种情况下,我读取了一个 pfd 文件并创建了另一个带有水印的文件。

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();
于 2016-10-03T16:42:38.147 回答
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.

于 2015-09-11T11:15:45.450 回答
-2

嗨,不是 100% 确定我是否完全理解这个问题,但是如果这是我的想法,那么这可能会有所帮助。

您是否确保为每一页最后添加水印。在做类似的事情时,我遇到了不同的不透明度问题,这取决于我的文本在一页上的一些对象下面,然后在另一页的顶部。我在包装器中创建了一个方法,我可以简单地在每个页面上调用 last 以确保它被放置在页面上其他所有内容的顶部。

    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);
    }
于 2011-09-05T09:42:45.630 回答