Embed image once and use on multiple pages

Hi,


I am using PDF.Generator for pdf. I need to add images to the pdf. This increase the size of the PDF. If i need to add one big image multiple times in my pdf size of the pdf increases many folds. Can i add this image only once to PDF and show it on multiple pages. Let me know if this is possible using Aspose.Pdf.Generator.

Regards,
Sandeep

Hi Sandeep,

Thanks for contacting support.
Please try using the following code snippet based on new DOM approach. Please note that Aspose.Pdf.Generator is a legacy approach and it will be deprecated in near future. Therefore we recommend using the new DOM approach.

[C#]

//open document
Document pdfDocument = new Document();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = "c:pdftestimage001 (1).png";

for (int counter = 1; counter < 10; counter++)
{
    pdfDocument.Pages.Add().Paragraphs.Add(image);
}

//save modified PDF
pdfDocument.Save("c:pdftestMultipleImage_output.pdf");

Hi,


Thanks for your reply. First i can’t move to the DOM approach as of now as it still doesn’t fully supports all features of generator.

Second the example you gave is not what i asked for. In your example you are adding the image on various page and suppose the size of your image is 4 mb then it will be added to pdf 10 times and resultant PDF will be of size 40mb. Correct me if i am wrong.

What i want is that though i am showing images on multiple pages PDF document should contain only one copy of image. So in your example it will be shown on 10 pages but actual size of pdf still remain 4 mb.

Let me know if it is possible in Generator approach.

Regards,
Sandeep

Hi Sandeep,


I am working on preparing the code based on Aspose.Pdf.Generator and will get back to you soon.

Hi Sandeep,

We have a method named CompleteClone(..) in Aspose.Pdf.Generator.Image which creates a clone of a previously created object. However, even when using this approach to place multiple instances of an image on various pages, a separate copy of the image is placed, and hence it produces a PDF file with a size equal to Image Size X number of times it is repeated inside the PDF.

I am afraid currently we cannot accomplish the requirement with the legacy Aspose.Pdf.Generator approach.

C#

//open document
Aspose.Pdf.Generator.Pdf pdfDocument = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Image image = new Aspose.Pdf.Generator.Image();
image.ImageInfo.File = "c:/pdftest/BarCode_Image.png";
image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;

for (int counter = 1; counter < 10; counter++) {
    Aspose.Pdf.Generator.Image newimage = image.CompleteClone() as Aspose.Pdf.Generator.Image;
    pdfDocument.Sections.Add().Paragraphs.Add(newimage);
}

// compress the PDF file contents
pdfDocument.CompressionLevel = 1;

// save modified PDF
pdfDocument.Save("c:/pdftest/MultipleImage_output.pdf");