Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Sep 13, 2023 12:40 pm

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");

bstojanovic
 
Posts: 45
Joined: Mon Sep 28, 2020 2:54 pm

Thu Sep 14, 2023 6:52 am

Hi,

Thank you for your more inquiry.
I used the latest version of Spire.Office for .Net 8.9.2 to test the pdf file and code you provided, due to I don’t find the code of the “Position” method,  I replaced  one line of your code with this one:
Code: Select all
PointF p = new PointF(stampWidth, stampHeight);

as shown in the screenshot:
lQLPJyAYORrWK7DNAXTNAl-w7eIkbgbS5rUE9jlHZEDlAA_607_372.png

and successfully generated the stamp as shown in the following figure. Please carefully check if your customized Position method is correct. And if you don’t use the latest version of Spire.Office for .Net 8.9.2, please update to it and test again.
Here is a screenshot of my test screen:
1.png

If the issue still exists, please offer the following message to help us do further investigation. Thank you in advance.
1)The complete code of Position method.
2)Application type, such as Console App, .NET Framework 4.8.
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit).

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Tue Oct 24, 2023 9:34 am

Hi,

I would like to know if you have resolved the issue you have encountered, and our team hope to have more communication with you. If the problem exsists,you can attach it here or send it to us via email( support@e-iceblue.com ). Thank you in advance.

Sincerely
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Wed Oct 25, 2023 12:55 pm

Hi,
Yes we were able to fix this issue.
Thank you.

bstojanovic
 
Posts: 45
Joined: Mon Sep 28, 2020 2:54 pm

Thu Oct 26, 2023 6:11 am

Hi,

Thank you for your feedback.
If you have any question, just feel free to write back. I hope you have a wonderful day.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.PDF