Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public FileStreamResult Pdf()
        {

            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();
            document.Add(new Paragraph("Hello World"));
            document.Add(new Paragraph(DateTime.Now.ToString()));
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return new FileStreamResult(workStream, "application/pdf");
        }
Posted
Updated 28-Jul-15 14:35pm
v2

1 solution

Try this. Dug it out of previously working code.

BORDER
C#
PdfContentByte content = writer.DirectContent;
Rectangle rect = new Rectangle(doc.PageSize);
rect.Left += doc.LeftMargin;
rect.Right -= doc.RightMargin;
rect.Top -= doc.TopMargin;
rect.Bottom += doc.BottomMargin;
content.SetColorStroke(Color.BLACK);
content.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
content.Stroke();


WATERMARK
Add similar to the onStartPage event so it gets called for each new page.

C#
float fontSize = 48;
float xPos = 300;
float yPos = 400;
float angle = 25;

PdfContentByte wmark = writer.DirectContentUnder;
BaseFont baseFont = BaseFont.CreateFont(BaseFont.ARIAL, BaseFont.WINANSI, BaseFont.EMBEDDED);
wmark.BeginText();
wmark.SetColorFill(BaseColor.LIGHT_GRAY);
wmark.SetFontAndSize(baseFont, fontSize);
wmark.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "CONFIDENTIAL", xPos, yPos, angle);
wmark.EndText();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900