Hi,
I was trying to add stamp to pdf document that has multiple input fields and after changing document with code below it is not possible to save file. I get message on saving to file "Object reference not set to an instance of an object."
I'm sending code example and file example.
Regards
This is the code example:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\BiljanaStojanovic\Documents\Willow\PDF Test2.pdf");
string text = @"test";
PdfDocument newPdf = new PdfDocument();
MemoryStream memoryStream = new MemoryStream();
string[] rows = text.Split("\n");
int r = rows.Length;
float fontSize = 16.0f;
string str = rows[0];
foreach (string row in rows)
{
if (row.Length > str.Length) str = row;
}
SizeF size = new SizeF();
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Segoe UI", fontSize), true);
PdfStringFormat format = new PdfStringFormat();
format.LineAlignment = PdfVerticalAlignment.Middle;
format.Alignment = PdfTextAlignment.Center;
format.MeasureTrailingSpaces = true;
size = font2.MeasureString(str, format);
float stampHeight = (r * size.Height + 1f);
float stampWidth = (size.Width * 1.05f + 1f);
float possitionH = 0;
float possitionW = 0;
int i = 0;
foreach (PdfPageBase page1 in doc.Pages)
{
PdfPageBase newPage;
PointF p = Position(0, page1, 0, 0, stampWidth, stampHeight);
possitionW = p.X;
possitionH = p.Y;
if (possitionW < 0) possitionW = 0;
if (possitionH < 0) possitionH = 0;
PdfTemplate template = new PdfTemplate(stampWidth, stampHeight);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
RectangleF rectangle = new RectangleF(new PointF(possitionW, possitionH), template.Size);
template.Graphics.DrawString(text, font2, brush, new PointF(1, 1));
PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle);
PdfAppearance apprearance = new PdfAppearance(stamp);
apprearance.Normal = template;
stamp.Appearance = apprearance;
newPage = newPdf.Pages.Add(page1.Size, new PdfMargins(0));
page1.CreateTemplate().Draw(newPage, new PointF(0, 0));
newPage.AnnotationsWidget.Add(stamp);
}
newPdf.SaveToFile(@"C:\Users\BiljanaStojanovic\Documents\Willow\PDF Test22.pdf");