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.

Tue Mar 16, 2021 3:05 pm

hello
I'm trying to add a watermark to my pdf and i'm using code provided on docs (i can't post urls...):
Code: Select all
using Spire.Pdf;
using System.Drawing;


namespace ImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Sample.pdf");

            PdfPageBase page = pdf.Pages[0];

            Image img = Image.FromFile("logo.png");
            page.BackgroundImage = img;

            page.BackgroundRegion = new RectangleF(300, 200, 80, 81);

            pdf.SaveToFile("result.pdf");
        }
    }
}

compiler say that System.Drawing.Image connot be converted to System.IO.Stream
I'm using Spire.PDF v 7.1.10 in a wpf project

I've tried to use
Code: Select all
Image ebLogo = Image.FromFile(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources/logo.jpg"));
MemoryStream stream = new MemoryStream();
ebLogo.Save(stream, ImageFormat.Jpeg);
page.BackgroundImage = ebLogo;
page.BackgroundRegion = new RectangleF(300, 200, 80, 81);

but at live 4 spirepdf throw a System.NullReferenceException
file path is correct and using breakpoints i can see that also stream is not null

any help is appreciated

gabrielandrei79
 
Posts: 2
Joined: Sun Oct 18, 2020 9:30 am

Wed Mar 17, 2021 7:25 am

Hello,

Thanks for your inquiry.
Did you create a WPF (.NetCore) project and install our Spire.PDF via NuGet? If so, please refer to the following code.
Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("Sample.pdf");
            PdfPageBase page = pdf.Pages[0];
            FileStream stream = new FileStream("logo.png", FileMode.Open);
            page.BackgroundImage = stream;
            page.BackgroundRegion = new RectangleF(300, 200, 80, 81);
            pdf.SaveToFile("result.pdf");

If there are still any issues, please provide us with your input file. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Thu Mar 18, 2021 6:54 am

Hello, thank you for your answer
Brian.Li wrote:Did you create a WPF (.NetCore) project and install our Spire.PDF via NuGet?

Yes, I did, it is a NetCore project, now Spire.Pdf is updated at version 7.3.1.
attached the input files
here the code:
Code: Select all
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;

namespace re.Service
{
    class WatermarkTest
    {
        public static void test()
        {
            string pdfIn = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file.pdf");
            string logoPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "logo.png");
            string pdfOut = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file_out.pdf");

            FileStream stream = new FileStream(logoPath, FileMode.Open);
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(pdfIn);
            PdfPageBase page = pdf.Pages[0];
            page.BackgroundImage = stream;
            page.BackgroundRegion = new RectangleF(300, 200, 80, 81);
            pdf.SaveToFile(pdfOut);
        }
    }
}

first problem seems to be that I was using a jpg, at line "page.BackgroundImage = stream;" throw a System.NullReferenceException.
second problem, I've converted to a png and now it throw a StackOverflowException. file_out.pdf seem to be locked, Adobe Reader can't open it

gabrielandrei79
 
Posts: 2
Joined: Sun Oct 18, 2020 9:30 am

Thu Mar 18, 2021 10:57 am

Hello,

Thanks for your feedback.
I have reproduced your issue and logged it in our bug tracking system with the ticket SPIREPDF-4114. If there are any updates, we will let you know. Sorry for the inconvenience caused.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Apr 14, 2021 10:16 am

Hello,

Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:7.4.5 which fixes your issue, please download it from the following links to test on your side. Looking forward to your testing result.
Website link: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget link: https://www.nuget.org/packages/Spire.PDF/7.4.5

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Apr 19, 2021 10:40 am

Hello,

Does this hotfix solve your issue? Could you please give us some feedback at your convenience?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF