I am trying to programmatically create a number of PDF documents with a watermark on each page using itextsharp (a C# port of Java's itext).
I am able to do this after the document has been created using a PdfStamper. However this seems to involve re-opening the document reading it and then creating a new document with the watermark on each page.
Is there a way of doing this during document creation?
After digging into it I found the best way was to add the watermark to each page as it was created. To do this I created a new class and implemented the IPdfPageEvent interface as follows:
class PdfWriterEvents : IPdfPageEvent
{
string watermarkText = string.Empty;
public PdfWriterEvents(string watermark)
{
watermarkText = watermark;
}
public void OnOpenDocument(PdfWriter writer, Document document) { }
public void OnCloseDocument(PdfWriter writer, Document document) { }
public void OnStartPage(PdfWriter writer, Document document) {
float fontSize = 80;
float xPosition = 300;
float yPosition = 400;
float angle = 45;
try
{
PdfContentByte under = writer.DirectContentUnder;
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
under.BeginText();
under.SetColorFill(BaseColor.LIGHT_GRAY);
under.SetFontAndSize(baseFont, fontSize);
under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
under.EndText();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
public void OnEndPage(PdfWriter writer, Document document) { }
public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition) { }
public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { }
public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { }
public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition) { }
public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { }
public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition) { }
public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { }
}
}
This object is registered to handle the events as follows:
PdfWriter docWriter = PdfWriter.GetInstance(document, new FileStream(outputLocation, FileMode.Create));
PdfWriterEvents writerEvent = new PdfWriterEvents(watermark);
docWriter.PageEvent = writerEvent;
Although Tim's solution seems very nice, I have managed to do the same thing (I believe) using the following code (perhaps iTextSharp was improved a bit since then...):
private byte[] AddWatermark(byte[] bytes, BaseFont bf)
{
using(var ms = new MemoryStream(10 * 1024))
{
using(var reader = new PdfReader(bytes))
using(var stamper = new PdfStamper(reader, ms))
{
int times = reader.NumberOfPages;
for (int i = 1; i <= times; i++)
{
var dc = stamper.GetOverContent(i);
PdfHelper.AddWaterMark(dc, AppName, bf, 48, 35, new BaseColor(70, 70, 255), reader.GetPageSizeWithRotation(i));
}
stamper.Close();
}
return ms.ToArray();
}
}
public static void AddWaterMark(PdfContentByte dc, string text, BaseFont font, float fontSize, float angle, BaseColor color, Rectangle realPageSize, Rectangle rect = null)
{
var gstate = new PdfGState { FillOpacity = 0.1f, StrokeOpacity = 0.3f };
dc.SaveState();
dc.SetGState(gstate);
dc.SetColorFill(color);
dc.BeginText();
dc.SetFontAndSize(font, fontSize);
var ps = rect ?? realPageSize; /*dc.PdfDocument.PageSize is not always correct*/
var x = (ps.Right + ps.Left) / 2;
var y = (ps.Bottom + ps.Top) / 2;
dc.ShowTextAligned(Element.ALIGN_CENTER, text, x, y, angle);
dc.EndText();
dc.RestoreState();
}
This will add a watermark on all pages of a PDF document that is provided as a byte array.
(You don't need to do it while creating the PDF.)
It seems working for both landscape and portrait and it probably works for documents with mixed orientations.
Cheers! :)
string WatermarkLocation = "D:\\Images\\superseded.png";
Document document = new Document();
PdfReader pdfReader = new PdfReader(FileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileMode.Create));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(125, 300); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)
PdfContentByte waterMark;
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
waterMark = stamp.GetOverContent(page);
waterMark.AddImage(img);
}
stamp.FormFlattening = true;
stamp.Close();
// now delete the original file and rename the temp file to the original file
File.Delete(FileLocation);
File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);
I used the first solution. I was having trouble getting it to work at first. I getting green underlines under all of my public voids saying that it was going to hide some inherit member.
Basically I realized that I already had added a PagePageEventHelper and I basically just cut out the code for the OnStartPage. ALSO! For some reason I had to make all of my public void's public override void.
public override void OnStartPage(PdfWriter writer, Document document)
{
if (condition)
{
string watermarkText = "-whatever you want your watermark to say-";
float fontSize = 80;
float xPosition = 300;
float yPosition = 400;
float angle = 45;
try
{
PdfContentByte under = writer.DirectContentUnder;
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
under.BeginText();
under.SetColorFill(iTextSharp.text.pdf.CMYKColor.LIGHT_GRAY);
under.SetFontAndSize(baseFont, fontSize);
under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
under.EndText();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
}
}
Can't you just lay down the watermark on each page after you've made it?
Yes, the Watermark class seems to be no more - odd. However in the process of converting to iTextSharp 5.3, I found a simple way to add a watermark to a new document.
MemoryStream mem = new MemoryStream();
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, mem);
PdfContentByte cb = writer.DirectContent;
document.Open();
document.NewPage();
Image watermark = Image.GetInstance(WATERMARK_URI);
watermark.SetAbsolutePosition(80, 200);
document.Add(watermark);
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.BeginText();
...
cb.EndText();
document.Close();
In iTextSharp you should be able to programmatically add a watermark e.g.
Watermark watermark = new Watermark(Image.getInstance("watermark.jpg"), 200, 420);
document.Add(watermark);
Related
I am using iTextSharp for pdf generating purpose . I want to get page no with total page show on footer.
my code is-
public class footer : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = doc.PageSize.Width;
Chunk myFooter = new Chunk("Page " + (doc.PageNumber) + " of " + doc.PageCount, FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 8, grey))
PdfPCell footer = new PdfPCell(new Phrase(myFooter));
footer.Border = Rectangle.NO_BORDER;
footer.HorizontalAlignment = Element.ALIGN_RIGHT;
footerTbl.AddCell(footer);
footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 10), writer.DirectContent);
}
doc.PageCount is give an error
you need to extent ITextEvents class that extends PdfPageEventHelper to add footer. in following way.
public class footer : PdfPageEventHelper
{
// This is the contentbyte object of the writer
PdfContentByte cb;
// we will put the final number of pages in a template
PdfTemplate footerTemplate;
// this is the BaseFont we are going to use for the header / footer
BaseFont bf = null;
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
footerTemplate = cb.CreateTemplate(50, 50);
}
catch (DocumentException de)
{
//handle exception here
}
catch (System.IO.IOException ioe)
{
//handle exception here
}
}
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{
base.OnEndPage(writer, document);
iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
Phrase p1Header = new Phrase("Sample Header Here", baseFontNormal);
//Create PdfTable object
PdfPTable pdfTab = new PdfPTable(3);
//We will have to create separate cells to include image logo and 2 separate strings
//Row 1
PdfPCell pdfCell1 = new PdfPCell();
PdfPCell pdfCell2 = new PdfPCell(p1Header);
PdfPCell pdfCell3 = new PdfPCell();
String text = "Page " + writer.PageNumber + " of ";
//Add paging to footer
{
cb.BeginText();
cb.SetFontAndSize(bf, 12);
cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
float len = bf.GetWidthPoint(text, 12);
cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
}
pdfTab.TotalWidth = document.PageSize.Width - 80f;
pdfTab.WidthPercentage = 70;
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
footerTemplate.BeginText();
footerTemplate.SetFontAndSize(bf, 12);
footerTemplate.SetTextMatrix(0, 0);
footerTemplate.ShowText((writer.PageNumber - 1).ToString());
footerTemplate.EndText();
}
}
I'm trying to draw a horizontal/Vertical line in pdf on a specific page number(let say on page number 3 out of 8 pages). But, I'm only able to draw a line on the first and last page.
Here's my code:
Step 1:
public ActionResult CreatePDF(string id)
{
// Create the iTextSharp document.
Document doc = new Document();
// Set the document to write to memory.
MemoryStream memStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
writer.CloseStream = false;
doc.Open();
DrawThickLine(writer, 36f, 519f, 806f, 519f);//Horizontal Line
DrawThickLine(writer, 36f, 280f, 36f, 521f);//Vertical Line
// Close and get the resulted binary data.
doc.Close();
// Send the binary data to the browser.
return BinaryContentData(memStream, "", "", d1.formnumber.Value);//External Binary Content method
}
Step 2:
private static void DrawThickLine(PdfWriter writer, float x1, float y1, float x2, float y2)
{
PdfContentByte contentByte = writer.DirectContent;
contentByte.SetLineWidth(4.0f); // Make a bit thicker than 1.0 default
contentByte.SetColorStroke(Color.BLACK);
contentByte.MoveTo(x1, y1);
contentByte.LineTo(x2, y2);
contentByte.Stroke();
}
Any help would be appreciated!!
After working an hour, here's my solution.
Step 1:
public ActionResult CreatePDF(string id)
{
// Create the iTextSharp document.
Document doc = new Document();
// Set the document to write to memory.
MemoryStream memStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
writer.CloseStream = false;
doc.Open();
// DrawThickLine(writer, 36f, 519f, 806f, 519f);//Horizontal Line
// DrawThickLine(writer, 36f, 280f, 36f, 521f);//Vertical Line
// Close and get the resulted binary data.
doc.Close();
// Send the binary data to the browser.
return BinaryContentData(memStream, "", "", id);//External Binary Content method
}
Step 2:
protected ActionResult BinaryContentData(MemoryStream pdf, string UserPwd, string OwnerPwd, string uid)
{
PdfReader reader = new PdfReader(pdf);
MemoryStream output = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(reader, output);
pdfStamper.Writer.CloseStream = false;
for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++)
{
if (pageIndex == 3)//Page 3
{
DrawThickLine(pdfStamper, pageIndex, 36f, 519f, 806f, 519f);//Horizontal Line
DrawThickLine(pdfStamper, pageIndex, 36f, 280f, 36f, 521f);//Vertical Line
}
}
............//some code goes here
return new BinaryContentResult(buffer, "application/pdf", uid);
}
Step 3:
private static void DrawThickLine(PdfStamper pdfStamper, int pageIndex, float x1, float y1, float x2, float y2)
{
PdfContentByte cb = pdfStamper.GetOverContent(pageIndex);
cb.SetLineWidth(4.0f); // Make a bit thicker than 1.0 default
cb.SetColorStroke(Color.BLACK);
cb.MoveTo(x1, y1);
cb.LineTo(x2, y2);
cb.Stroke();
}
I have an existing PDF file, I want to insert a text at the bottom of PDF file in Red color, but the existing pdf file color must remain the same.
Thank You #mkl below code, I used which is specific for Stamp.
public static void ManipulatePdf(string src, string dest)
{
src = #"C:\CCPR Temp\TempFiles\PayStub_000106488_12282019_20200117112403.pdf";
dest = #"C:\CCPR Temp\TempFiles\PayStub_WithStamper.pdf";
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); // create?
int numberOfPages = reader.NumberOfPages;
Rectangle pagesize;
for (int i = 1; i <= numberOfPages; i++)
{
PdfContentByte under = stamper.GetUnderContent(i);
pagesize = reader.GetPageSize(i);
float x =40;// (pagesize.Left + pagesize.Right) / 2;
float y = pagesize.Top/4;// (pagesize.Bottom + pagesize.Top) / 2;
PdfGState gs = new PdfGState();
gs.FillOpacity = 1.0f;
under.SaveState();
under.SetGState(gs);
under.SetRGBColorFill(255,0,0);
ColumnText.ShowTextAligned(under, Element.ALIGN_BOTTOM,
new Phrase("Watermark", new Font(Font.FontFamily.HELVETICA, 20)),
x, y, 1);
under.RestoreState();
}
stamper.Close();
reader.Close();
}
Adding content to an existing PDF document without changing the existing content, is sometimes referred to as stamping. Examples are adding page numbers, adding a watermark, and adding a running head.
For your specific case:
Create a PdfDocument instance from a PdfReader (to read the input PDF) and a PdfWriter (to write the output PDF). The PdfDocument instance will be in stamping mode.
Set the red text color on a Paragraph with the footer text.
Position the text with the ShowTextAligned convenience method, based on the page size.
You may also want to take into account page rotation.
PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"), new PdfWriter("output.pdf"));
Document doc = new Document(pdfDoc);
Paragraph footer = new Paragraph("This is a footer").SetFontColor(ColorConstants.RED);
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdfDoc.GetPage(i).GetPageSize();
float x = pageSize.GetLeft() + pageSize.GetWidth() / 2;
float y = pageSize.GetBottom() + 20;
doc.ShowTextAligned(footer, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM, 0);
}
doc.Close();
This will set the Font of your Paragraph. I am not sure about the Position.
BaseFont btnRedFooter = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font fntRedFooter = FontFactory.GetFont(iTextSharp.text.Font.FontFamily.TIMES_ROMAN.ToString(), 16,
iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED);
pProtocoll.Add(new Paragraph("Text in Footer", fntRedFooter));
I used the below steps and finally, got the required output. I was struggling for the font color, to apply to the newly added text.
public static void StampPdfFile(string oldFile, string newFile)
{
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetFontAndSize(bf, 8);
// write the text in the pdf content
cb.BeginText();
string text = $"Voided On - {DateTime.Now.Date.ToString("MM/dd/yyyy")}";
// put the alignment and coordinates here
cb.SetColorFill(BaseColor.RED); //Give Red color to the newly added Text only
cb.ShowTextAligned(2, text, 120, 250, 0);
cb.SetColorFill(BaseColor.BLACK); //Give Red color to the exisitng file content only
cb.EndText();
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.Close();
fs.Close();
writer.Close();
reader.Close();
}
From a pdf, I want to make a new pdf that I 'll add a watermark and then make each page an image page.
Is this possible with itext?
I don't know how to convert it to images, but for the watermark, as said by Usama Kiyani in comments, you should consider using itextsharp which can be installed through nugget packages manager. I already used it to add a water mark to an existing pdf file.
Here's the code I used, it add a diagonal red watermark (which text is argument watermarkText) in the center of each page of an existing pdf file (sourceFile), then save this modified version at the given location (outputFile) :
public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText)
{
BaseFont tWatermarkFont = null;
float tWatermarkFontSize = 48F;
iTextSharp.text.BaseColor tWatermarkFontColor = null;
float tWatermarkFontOpacity = 0.3F;
float tWatermarkRotation = 45.0F;
tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
tWatermarkFontColor = iTextSharp.text.BaseColor.RED;
AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);
}
public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.BaseColor watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)
{
iTextSharp.text.pdf.PdfReader reader = null;
iTextSharp.text.pdf.PdfStamper stamper = null;
iTextSharp.text.pdf.PdfGState gstate = null;
iTextSharp.text.pdf.PdfContentByte underContent = null;
iTextSharp.text.Rectangle rect = null;
float currentY = 0.0F;
float offset = 0.0F;
int pageCount = 0;
try
{
reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
rect = reader.GetPageSizeWithRotation(1);
FileStream stream = new System.IO.FileStream(outputFile, System.IO.FileMode.Create);
stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream);
if (watermarkFont == null)
{
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
}
if (watermarkFontColor == null)
{
watermarkFontColor = iTextSharp.text.BaseColor.RED;
}
gstate = new iTextSharp.text.pdf.PdfGState();
gstate.FillOpacity = watermarkFontOpacity;
gstate.StrokeOpacity = watermarkFontOpacity;
pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
underContent = stamper.GetOverContent(i);
underContent.SaveState();
underContent.SetGState(gstate);
underContent.SetColorFill(watermarkFontColor);
underContent.BeginText();
underContent.SetFontAndSize(watermarkFont, watermarkFontSize);
underContent.SetTextMatrix(30, 30);
currentY = (rect.Height / 2);
underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, currentY - offset, watermarkRotation);
underContent.EndText();
underContent.RestoreState();
}
stamper.Close();
reader.Close();
stream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
I guess it's not really hard to change it to fit your need, but if there's is anything you need me to explain just ask for it.
I am creating pdf using itextsharp. Now I want to add watermark on each pages I tried method on
public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)
Now watermark is coming but if pdf page containing image then its hiding watermark
public class itsevent : PdfPageEventHelper
{
string watermarkText = string.Empty;
public itsevent(string watermark)
{
watermarkText = watermark;
}
public override void OnStartPage(PdfWriter wr, iTextSharp.text.Document doc)
{
float fontSize = 80;
float xPosition = 300;
float yPosition = 300;
float angle = 45;
PdfContentByte u = wr.DirectContentUnder;
PdfGState gs = new PdfGState();
u.SaveState();
u.SetGState(gs);
BaseFont baseFont =BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
u.BeginText();
u.SetColorFill(iTextSharp.text.pdf.CMYKColor.LIGHT_GRAY);
u.SetFontAndSize(baseFont, fontSize);
u.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
u.EndText();
under.RestoreState();
}
}
itsevent ev = new itsevent("watermark");
pdfW.PageEvent = ev;
doc.Open();
Which PdfContentByte to use
You use wr.DirectContentUnder which is for adding content under the normally added content:
PdfContentByte u = wr.DirectContentUnder;
Thus, obviously an image in the normal content will cover the watersign. Use wr.DirectContent instead.
Which page event to use
You are adding the watermark in OnStartPage; iText developers recommend against doing so. Instead do that in OnEndPage.
How to improve the looks
You might want to apply transparency by setting the fill opacity of PdfGState gs.
Try using GetOverContext() to get the image
https://simpledotnetsolutions.wordpress.com/2012/04/08/itextsharp-few-c-examples/