【问题标题】:Error when adding watermark on whole pdf page using itextsharp使用 itextsharp 在整个 pdf 页面上添加水印时出错
【发布时间】:2012-11-01 18:55:49
【问题描述】:

我正在使用以下代码在页面上添加水印,但输出 MemoryStream 始终为空,我想知道我做错了什么。

public static MemoryStream testwatermark(MemoryStream pdf)
{
    PdfReader reader = new PdfReader(pdf);

    using (MemoryStream output = new MemoryStream())
    {
        PdfStamper pdfStamper = new PdfStamper(reader, output);

        for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++)
        {
            iTextSharp.text.Rectangle pageRectangle = reader.GetPageSizeWithRotation(pageIndex);
            PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
            pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);
            PdfGState graphicsState = new PdfGState(); graphicsState.FillOpacity = 0.4F;
            pdfData.SetGState(graphicsState);     
            //set color of watermark     
            pdfData.SetColorFill(BaseColor.BLUE);
            pdfData.BeginText();     
            //show text as per position and rotation     
            pdfData.ShowTextAligned(Element.ALIGN_CENTER, "BlueLemonCode", pageRectangle.Width / 2, pageRectangle.Height / 2, 45);     
            //call endText to invalid font set     
            pdfData.EndText();
        }
        pdfStamper.Close();
        return output;
    }
}

进一步查看输出流时显示以下错误。

CanRead = false
CanSeek = false
CanWrite = false
Capacity = 'output.Capacity' threw an exception of type 'System.ObjectDisposedException'
Length = 'output.Length' threw an exception of type 'System.ObjectDisposedException'
Position = 'output.Position' threw an exception of type 'System.ObjectDisposedException'

【问题讨论】:

标签: c# asp.net watermark


【解决方案1】:

上述代码通过返回字节而不是内存流来解决问题。

.
.
.
.
pdfStamper.Close();
return output;

【讨论】:

  • 我有同样的问题,但我通过这个答案摆脱了这个问题。竖起大拇指 Amir
猜你喜欢
  • 2011-01-23
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 2018-07-12
  • 1970-01-01
  • 2015-04-02
相关资源
最近更新 更多