如何在我的Migradoc文档中使用PDF Sharp对象

问题描述 投票:1回答:1

我有一个使用MigraDoc框架生成PDF的应用程序,但是我需要添加文本驱动的水印。我发现了一些使用PDF Sharp here完成此操作的示例,但我似乎无法弄清楚这将如何与我正在渲染的Migradoc Document()对象集成。

我有以下代码:

    public byte[] render()
    {
        PdfDocument document = new PdfDocument();
        CreateWaterMarks(document);

        // *****************************

        PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
        renderer.Document = this.document;
        renderer.RenderDocument();

        byte[] pdfContents = null;
        using (MemoryStream stream = new MemoryStream())
        {
            renderer.PdfDocument.Save(stream, true);
            pdfContents = stream.ToArray();
        }

        return pdfContents;
    }

这个方法被称为渲染MigraDoc文档并将其作为字节数组传递出去。这里的第二行代码调用以下方法,而不是我正在寻找的东西:

    void CreateWaterMarks(PdfDocument document)
    {
        PdfPage page = document.AddPage();
        Document doc = this.document;

        MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
        docRenderer.PrepareDocument();

        XRect A4Rect = new XRect(0, 0, pageActiveWidth, pageActiveHeight);

        int pageCount = docRenderer.FormattedDocument.PageCount;
        for (int idx = 0; idx < pageCount; idx++)
        {
            XFont font = new XFont("Verdana", 13, XFontStyle.Bold);
            XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

            XSize size = gfx.MeasureString("Watermark", font);

            gfx.TranslateTransform(pageActiveWidth / 2, pageActiveHeight / 2);
            gfx.RotateTransform(-Math.Atan(pageActiveHeight / pageActiveWidth) * 180 / Math.PI);
            gfx.TranslateTransform(-pageActiveWidth / 2, -pageActiveHeight / 2);

            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Near;
            format.LineAlignment = XLineAlignment.Near;

            XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

            gfx.DrawString("Watermark", font, brush, new XPoint((pageActiveWidth - size.Width) / 2, (pageActiveHeight - size.Height) / 2), format);

            docRenderer.RenderPage(gfx, idx + 1);
        }
    }

我希望这会神奇地使这些PDFSharp水印出现,但唉,我什么都没得到!

watermark pdfsharp migradoc
1个回答
0
投票

我使用以下代码工作:

    public byte[] render()
    {
        PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
        renderer.Document = this.document;

        renderer.RenderDocument();
        renderer.PrepareRenderPages();

        CreateWatermarks(renderer);

        byte[] pdfContents = null;
        using (MemoryStream stream = new MemoryStream())
        {
            renderer.PdfDocument.Save(stream, true);
            pdfContents = stream.ToArray();
        }

        return pdfContents;
    }

    private void CreateWatermarks(PdfDocumentRenderer renderer)
    {
        int pages = renderer.DocumentRenderer.FormattedDocument.PageCount;
        for (int i = 0; i < pages; ++i)
        {
            var page = renderer.PdfDocument.Pages[i];

            XFont font = new XFont("Verdana", 27, XFontStyle.Bold);
            XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

            XSize size = gfx.MeasureString("Watermark", font);

            gfx.TranslateTransform(pageActiveWidth / 2, pageActiveHeight / 2);
            gfx.RotateTransform(-Math.Atan(pageActiveHeight / pageActiveWidth) * 180 / Math.PI);
            gfx.TranslateTransform(-pageActiveWidth / 2, -pageActiveHeight / 2);

            XStringFormat format = new XStringFormat();
            format.Alignment = XStringAlignment.Near;
            format.LineAlignment = XLineAlignment.Near;

            XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

            gfx.DrawString("Watermark", font, brush, new XPoint((pageActiveWidth - size.Width) / 2, (pageActiveHeight - size.Height) / 2), format);

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.