Free Support Forum - aspose.com

Pdf - Add image


#1

Hi,

I have a requirement to add an image as a watermark on every page and later be able to delete this image if not needed anymore.

The documents have different sizes (A4, A3,A0…) so the image should adapt to the document width (for example half of the pdf file width) while keeping its original ratio.
I need to add the resized image at the bottom right corner

Below is the code i wrote:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(sourceFile);

        for (int i = 1; i <= pdfDocument.Pages.Count; i++)
        {                
            ImageStamp imageStamp = new ImageStamp(image_WM_Path);
            
            double ratio = imageStamp.Width / (pdfDocument.Pages[i].Rect.Width / 2);

            imageStamp.Width = imageStamp.Width / ratio;
            imageStamp.Height = imageStamp.Height / ratio;

            imageStamp.XIndent = pdfDocument.Pages[i].Rect.Width - imageStamp.Width;

            imageStamp.Background = false;
            imageStamp.setStampId(999);
            // Add stamp to particular page
            pdfDocument.Pages[i].AddStamp(imageStamp);               
        }

        pdfDocument.Save(destinationPath);

To delete the stamp i use :

 PdfContentEditor contentEditor = new PdfContentEditor();
 contentEditor.BindPdf(fileName);
 StampInfo[] stampInfo = contentEditor.GetStamps(1);
 for (int i = 0; i < stampInfo.Length; i++)
 {
     if (stampInfo[i].StampId == 999)
     {
           contentEditor.DeleteStampById(stampInfo[i].StampId);
     }
 }
 contentEditor.Save(fileName); 

Is it the right way to

  • add images and delete them ?

  • , to resize them ?

Is it the more efficient regarding the increase in the file size ?
I notice an increase in the files size, not the same on each file. Sometime just hundreds of Kb, sometimes multiple Mb.

On some documents the image is not placed in the bottom right corner. I dont know if pdfDocument.Pages[i].Rect.Width is the way to get the xindent.

Whats the difference with pdfDocument.Pages[i].MediaBox, ArtBox , …

Thanks for your advice


#2

@lisi

Thank you for contacting support.

We would like to update you that you are using right approaches to achieve your requirements. StampId property is used to differentiate between stamps. Moreover, we are afraid you may not manipulate the images with Aspose.PDF for .NET API as the image will be resized automatically, depending on height and width of stamp.

About adding the image in center of the page, you may specify XIndent and YIndent properties based on page dimensions. Below is a code snippet for your kind reference.

        Document pdfDocument = new Document();
        pdfDocument.Pages.Add();
        ImageStamp imageStamp = new ImageStamp(dataDir + "Source.jpg");
        imageStamp.Background = false;
        imageStamp.Height = 300;
        imageStamp.Width = 300;
        imageStamp.XIndent = (pdfDocument.PageInfo.Width / 2) - (imageStamp.Width / 2);
        imageStamp.YIndent = (pdfDocument.PageInfo.Height / 2) - (imageStamp.Height / 2);
        for (int i = 1; i <= pdfDocument.Pages.Count; i++)
        {
            pdfDocument.Pages[i].AddStamp(imageStamp);
        }
        pdfDocument.Save(dataDir + "ImageStamp_18.9.1.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

Hi Farhan,

While you were writing a repsosne at the same time I updated my post to match the progress i made.

Can you please look at it and answer my questions ?

The pdfDocument.PageInfo property does not give me the correct page size.

Thanks


#4

@lisi

You may use any property of the page to calculate the dimensions for image stamp. For the documents where image is not set on extreme bottom left, probably there are some margins set which you may override with below line of code before adding the ImageStamp.

pdfDocument.Pages[i].PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

Moreover, you may visit Get Page Properties to understand ArtBox and other related properties with pictorial representation.

Furthermore, file size may vary based on the images. In case you notice irregular size of files then please create a separate topic while mentioning all the details so that we will be able to assist you accordingly.


#5

On some documents the pdfDocument.PageInfo.Widh value i get is wrong.

It is equal to 66% of the document width. Then my image is not properly scaled and is not in the right bottom corner


#6

@lisi

Would you please share source PDF file and sample image along with narrowed down code snippet so that we may try to reproduce and investigate it in our environment.


#7

example.zip (20.7 KB)

Please find attached a zip file containing the png image, link to pdf file and code.

Is anyone able to download the zip file i attached ?


#8

@lisi

Thank you for sharing requested data.

We have worked with the data shared by you and have noticed that the difference probably occurred because of Landscape orientation of the page. So you may simply avoid calculating XIndent value and specify the alignments for image stamp to position it in Bottom Right corner of the page. Please try below code snippet in your environment and then share your kind feedback with us.

        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "HST10 - Sheet1.pdf");
        for (int i = 1; i <= pdfDocument.Pages.Count; i++)
        {
            ImageStamp imageStamp = new ImageStamp(dataDir + "watermark.png");
            double ratio = imageStamp.Width / (pdfDocument.Pages[i].Rect.Width / 2);
            imageStamp.Width = imageStamp.Width / ratio;
            imageStamp.Height = imageStamp.Height / ratio;
            pdfDocument.Pages[i].PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
            imageStamp.HorizontalAlignment = HorizontalAlignment.Right;
            imageStamp.VerticalAlignment = VerticalAlignment.Bottom;
            imageStamp.Background = false;
            imageStamp.setStampId(999);
            pdfDocument.Pages[i].AddStamp(imageStamp);
        }
        pdfDocument.Save(dataDir + "Alignment_18.9.1.pdf");

Generated PDF document has been attached for your kind reference. Alignment_18.9.1.pdf

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#9

Thank you for your answer it worked.regarding the position in the Bottom Right corner.

I have a last question. I want the image to be 2/3 of the document width.

With this line of code it works for most document i have :
double ratio = imageStamp.Width / (pdfDocument.Pages[i].Rect.Width / 1.5);

But for the document i shared with you i cant make it working, maybe because it is in landscape.
Can you please provide me a way to resize the stamp depending on the page orientation ?

Thanks


#10

@lisi

We are glad to know that suggested approach satisfied your requirements. For resizing the page stamp, we have noticed that your PDF file contains rotated pages so you need to assign page height value to stamp width and the width value to stamp height. This way added stamp will have 2/3 of page height and 2/3 of page width. Please try below code snippet in your environment and then share your kind feedback with us.

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "HST10 - Sheet1.pdf");
for (int i = 1; i <= pdfDocument.Pages.Count; i++)
{
    ImageStamp imageStamp = new ImageStamp(dataDir + "watermark.png");
    if (pdfDocument.Pages[i].Rotate == Rotation.on90)
    {
        imageStamp.Width = (pdfDocument.Pages[i].Rect.Height / 3) * 2;
        imageStamp.Height = (pdfDocument.Pages[i].Rect.Width / 3) * 2;
    }
    else
    {
        imageStamp.Width = (pdfDocument.Pages[i].Rect.Width / 3) * 2;
        imageStamp.Height = (pdfDocument.Pages[i].Rect.Height / 3) * 2;
    }
    pdfDocument.Pages[i].PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
    imageStamp.HorizontalAlignment = HorizontalAlignment.Right;
    imageStamp.VerticalAlignment = VerticalAlignment.Bottom;
    imageStamp.Background = false;
    imageStamp.setStampId(999);
    pdfDocument.Pages[i].AddStamp(imageStamp);
}

pdfDocument.Save(dataDir + "TwoThirdStamp_18.9.1.pdf");

Generated PDF file has been attached for your kind reference. TwoThirdStamp_18.9.1.pdf

If you still face any problem then please share a screenshot of expected output so that we may assist you accordingly.


#11

I adapted the code a little to fit new requirements and it works great.
The page orientation was the issue.

Thank you


#12

@lisi

Thank you for your kind feedback.

We are glad to know that suggested approach worked in your environment. Please feel free to contact us if you need any further assistance.


#13

Hi,
I need to add an image to pdf at bottom of the page, or at a bookmark in a pdf page.Please share teh code. I am using imageStamp, which works, however, assist me in placing the image at the bottom of the page.
Regards,
Mamtha.A.C.D


#14

@HAREEM_HCL_COM

Thank you for contacting support.

You may specify the location of image stamp using XIndent, YIndent or VerticalAlignment properties as explained in Adding Image Stamp in PDF File.

We hope this will be helpful. Please feel free to let us know if you need any further assistance.


#15

Hi Team,
I had already used ImageStamp, it over laps the existing content. I need to place the image at bookmark location or at bottom of the page. It should not overlap the existing content.
Please assist on this.

I can place it at bottom, but, it is overlapping the existing content.
The image has to be inserted, and it should not over lap the content in PDF.

Bookmark is not working for me.
Do need to have any special edition of Adobe Acrobat. I have Acrobat Reader, do I need to have professional.

Regards,
Mamtha.A.C.D


#16

@HAREEM_HCL_COM

Would you please share source and generated files to elaborate the problem you are facing, while sharing a document as your expected output so that we may investigate and assist you accordingly. Moreover, you do not need to install any PDF manipulation application like Adobe Acrobat because Aspose.PDF for .NET is not dependent on any other application.