Add an Image as Background in PDF document using Aspose.PDF for .NET

Hi,


The Aspose example code showing how to add a background image to a PDF doesn’t appear to work

I’ve tried the main web page example and the code from the GitHub repositories.


Both produce a blank pdf (except for the eval message).

The code from git hub example
// The path to the documents directory.
            string dataDir = Path.GetFullPath(“…/…/…/Data/”);

        <span style="color: green;">// create a new Document object</span>
        <span style="color: rgb(43, 145, 175);">Document</span> doc = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">Document</span>();

        <span style="color: green;">// add a new page to document object</span>
        <span style="color: rgb(43, 145, 175);">Page</span> page = doc.Pages.Add();

        <span style="color: green;">// create BackgroundArtifact object</span>
        <span style="color: rgb(43, 145, 175);">BackgroundArtifact</span> background = <span style="color: blue;">new</span> <span style="color: rgb(43, 145, 175);">BackgroundArtifact</span>();

        <span style="color: green;">// specify the image for backgroundartifact object</span>
        background.BackgroundImage = <span style="color: rgb(43, 145, 175);">File</span>.OpenRead(dataDir + <span style="color: rgb(163, 21, 21);">"aspose-total-for-net.jpg"</span>);

        <span style="color: green;">// add backgroundartifact to artifacts collection of page</span>
        page.Artifacts.Add(background);

        <span style="color: green;">// save the document</span>
        doc.Save(dataDir + <span style="color: rgb(163, 21, 21);">"output.pdf"</span>);</pre></div></div><div><br></div><div>I'm currently using the eval version of Aspose.pdf , but this is a key requirement for us and is stopping my company from purchasing. Please can you advise how to get this working.</div><div><br></div><div>Many Thanks.</div><div><br></div><div><br></div><div><br></div>

Hi Craig,

Thanks for your considering Aspose. Aspose.Pdf evaluation version has two limitations, evaluation watermark and at most four elements of any collection can be viewed. Please make a request for 30 days temporary license to evaluate our product without any limitation. It will resolve the issue and help you to accomplish the task.

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for your help, this is working perfectly with the trial license. However, the above code doesn’t appear to be using more than 4 items in a collection, so I can only assume it’s the inners of the Aspose dll that flagged the eval limitation.


Thanks again for your quick and helpful response!

Hi Craig,


Thanks for your feedback. It is good to know that your issue has been resolved. Moreover, in reference to your findings, we will investigate it internally and will fix with proper exception implemenation.


Please feel free to contact us for any further assistance.


Best Regards,

That’s great.


One more thing, I now have a background image working, it’s A4 in size, so fits the full page, however, it is positioned slightly to the right of the page.

Is there a margin or positioning that I need to set so that it appears centrally?

Many Thanks,
Craig

Hi Craig,


Sorry for the delayed response.

Please try using following code lines.

background.ArtifactHorizontalAlignment = ASPDF.HorizontalAlignment.Center;<o:p></o:p>

background.ArtifactVerticalAlignment = VerticalAlignment.Center;



In case you face any issue, please share the resource Image and PDF file so that we can test the scenario at our end.