Adding an image to a pdf

Hi,


I’m finding it hard to figure out how to add an image to a pdf using Aspose.pdf

I want to add a jpg to a table that sits within a paragraph which is part of a header.

I see the documentation mentions using a stream and operators such as the “ConcatenationMatrix” but I can’f find anything on what this is and can I use this in my case.

Any help much appreciated.

Hi Steve,


Thanks for contacting support.

The Image and Table are paragraph level objects and they are placed inside the paragraphs collection of PDF file. However a table consists of one or more Row instances and a particular Row object contains one or more Cells objects, where a cell has paragraphs collection and any paragraph level object (i.e. Image, TextFragment, Table etc) can be placed inside the paragraphs collection of Cell object.

Please visit the following links for required information on

Thanks.

What I am trying to do is thus:

1. Replicate the same pdf using pdf.document rather than pdf.generator (as I understand this is now obsolete)

2. Save the pdf as a blob to a sql database, then read and display the pdf at a later time.

For point 1, I have now placed the image in the pdf, however the files size of the finished pdf is 91k. When I created the same pdf in pdf.generator the file size was a much better 16k. (with image)

For point 2, I need the image to be saved with the pdf data when it is stored as a blob in SQL, so that the image is displayed when the blob is read at a later date.

Thanks

How can I do this?

steve.wackett1:
For point 1, I have now placed the image in the pdf, however the files size of the finished pdf is 91k. When I created the same pdf in pdf.generator the file size was a much better 16k. (with image)
Hi Steve,

Can you please share the source Image so that we can test the scenario in our environment.
steve.wackett1:
For point 2, I need the image to be saved with the pdf data when it is stored as a blob in SQL, so that the image is displayed when the blob is read at a later date.
Once the image is placed inside PDF file, you can save the output in Stream instance and when saving/retrieving PDF file Blog/database, the image should appear when viewing the document. Please note that Image is placed inside PDF document, instead of adding a simple relative reference to image path.

Thanks for your reply.

The image would not save using pdf.generator, however it is now saving fine with pdf.

The only issue I have is how do I compress the file? The pdf is 89k vs 16k from pdf.generator.

Also using pdf.document, with the image the pdf is 89kb, without the image the pdf is 48kb, the image is only 11kb in size so where is the extra 30kb coming from?

Image attached.

I'm using the following code to add the image to a paragraph of a table cell.

var quartixLogo = new Image
{
File = @"C:\quartixlogostrapline_80mm.png",
HorizontalAlignment = HorizontalAlignment.Left,
ImageScale = 0.7f
};

Hi Steve,

Thanks for sharing the resource files.

I have tested the scenario where I have simply placed the source image into paragraphs collection of PDF document and as per my observations, the resultant file being generated with Aspose.Pdf for .NET 10.2.0 is 22.0 KB. It appears that you are also adding some other contents to Table element inside PDF file.

Can you please share the code snippet so that we can again test the scenario in our environment. For your reference, I have also attached the resultant PDF generated over my end.

[C#]

```csharp
var document = new Aspose.Pdf.Document();
var quartixLogo = new Aspose.Pdf.Image
{
    File = @"C:\pdftest\QuartixLogostrapline_80mm.png",
    HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left,
    ImageScale = 0.7f
};
document.Pages.Add().Paragraphs.Add(quartixLogo);
document.Save(@"c:/pdftest/QuartixLogostrapline_output.pdf");
```

If I just comment out the line where I add the image to the cell paragraph the pdf is 48kb vs 89k with the line uncommented.

var mainPage = pdf.Pages.Add();

var headerTable = new Table
{
ColumnWidths = "200 50 200",
HorizontalAlignment = HorizontalAlignment.Left
};

var headerRow1 = headerTable.Rows.Add();
headerRow1.FixedRowHeight = 90;

var headerRow1Cell1 = headerRow1.Cells.Add();
headerRow1Cell1.VerticalAlignment = VerticalAlignment.Top;
headerRow1Cell1.Alignment = HorizontalAlignment.Left;
headerRow1Cell1.Paragraphs.Add(quartixLogo);

headerRow1.Cells.Add();

var headerRow1Cell3 = headerRow1.Cells.Add();
headerRow1Cell3.VerticalAlignment = VerticalAlignment.Top;
headerRow1Cell3.Alignment = HorizontalAlignment.Right;
headerRow1Cell3.Paragraphs.Add(addressText);

var header = new HeaderFooter { Margin = new MarginInfo { Left = 57, Right = 57, Top = 30, Bottom = 0 } };
header.Paragraphs.Add(headerTable);
mainPage.Header = header;

I should also say that I am saving the pdf to a SQL server database using a varbinary(max) column. To do this I use the following code

var memoryStream = new MemoryStream();
pdf.Save(memoryStream);

Then to open I use (where invoice.invoiceaspdf is the varbinary column)

var byteArray = invoice.InvoiceAsPdf;
var fileStream = new FileStream(string.Format("{0}invoiceFromDB.pdf", dataDir), FileMode.Create);
fileStream.Write(byteArray, 0, byteArray.Length);

So to test, at the end of the pdf creation code I save it twice, once to file and once to memorystream(db).

Now when I run this the first save (straight save to file system) is 87kb and the second save (to DB) is now 130kb.

code snippet of this bit

const string dataDir = @"c:/pdftest/";
pdf.Save(string.Format("{0}invoiceFromPdfB.pdf", dataDir));

var memoryStream = new MemoryStream();
pdf.Save(memoryStream);

steve.wackett1:

If I just comment out the line where I add the image to the cell paragraph the pdf is 48kb vs 89k with the line uncommented.

var mainPage = pdf.Pages.Add();

var headerTable = new Table
{
ColumnWidths = “200 50 200”,
HorizontalAlignment = HorizontalAlignment.Left
};

var headerRow1 = headerTable.Rows.Add();
headerRow1.FixedRowHeight = 90;

var headerRow1Cell1 = headerRow1.Cells.Add();
headerRow1Cell1.VerticalAlignment = VerticalAlignment.Top;
headerRow1Cell1.Alignment = HorizontalAlignment.Left;
headerRow1Cell1.Paragraphs.Add(quartixLogo);

headerRow1.Cells.Add();

var headerRow1Cell3 = headerRow1.Cells.Add();
headerRow1Cell3.VerticalAlignment = VerticalAlignment.Top;
headerRow1Cell3.Alignment = HorizontalAlignment.Right;
headerRow1Cell3.Paragraphs.Add(addressText);

var header = new HeaderFooter { Margin = new MarginInfo { Left = 57, Right = 57, Top = 30, Bottom = 0 } };
header.Paragraphs.Add(headerTable);
mainPage.Header = header;

Hi Steve,

Thanks for sharing the details.

I have tested the scenario where I have place image file (QuartixLogostrapline_80mm.png) into paragraphs collection of table cell and have placed Text in other table cell and as per my observations, a resultant PDF of 42KB is being generated. For your reference, I have also attached the resultant file generated over my end.

In an other attempt, when I have commented out the code line for adding image to table cell, the resultant file of 1.97 KB is being generated.

steve.wackett1:

I should also say that I am saving the pdf to a SQL server database using a varbinary(max) column. To do this I use the following code

var memoryStream = new MemoryStream();
pdf.Save(memoryStream);

Then to open I use (where invoice.invoiceaspdf is the varbinary column)

var byteArray = invoice.InvoiceAsPdf;
var fileStream = new FileStream(string.Format("{0}invoiceFromDB.pdf", dataDir), FileMode.Create);
fileStream.Write(byteArray, 0, byteArray.Length);

So to test, at the end of the pdf creation code I save it twice, once to file and once to memorystream(db).

Now when I run this the first save (straight save to file system) is 87kb and the second save (to DB) is now 130kb.

code snippet of this bit

const string dataDir = @"c:/pdftest/";
pdf.Save(string.Format("{0}invoiceFromPdfB.pdf", dataDir));

var memoryStream = new MemoryStream();
pdf.Save(memoryStream);

Hi Steve,

During my testing with Aspose.Pdf for .NET 10.2.0, the size of file being saved to MemoryStream is also 43.5KB. Can you please share which version of API you are using.

Ah, so 42KB with image and 1.97KB without. Is this normal that an 11KB image generates a PDF 4 times larger than the image itself?

Why is the PDF with the image not 12.97KB (1.97KB + 11KB) ?

Just checked and I am using 10.1.

I just upgraded to 10.2.0, the problem still persists with the this version.

Hi Steve,

Thanks for sharing the details.

For the sake of correction, I have logged the size increase problem in our issue tracking system as PDFNEWNET-38456. We will investigate this issue in detail and will keep you updated on the status of a correction.

We apologize for your inconvenience.