[英]Error when adding watermark on whole pdf page using itextsharp
我正在使用以下代碼在頁面上添加水印,但輸出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'
通過返回上述代碼中的字節而不是內存流來解決問題。
.
.
.
.
pdfStamper.Close();
return output;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.