Free Support Forum - aspose.com

Add dynamically generated images to PDF using Aspose.PDF for .NET

Hi!

If it is possible, could you please provide an example of adding dynamically generated image to a PDF file which can be used on the webserver (i.e. without saving to a temp file).

For example, how can an object generated by Aspose.Chart be added to a pdf file(or a Word file).

Thanks!
Igor

Dear Igor,

Thanks for your consideration.

Please view the example of fix 1.5.27.


Thanks!

Is it possible to add Aspose.Chart to a PDF document?

Dear IgorS,

Absolutely yes.

Would you please post a generic specification of the chart you want to create and then add to a Pdf document?

Then Tommy can write a demo for you to illustrate how to integrate Aspose.Chart with Aspose.Pdf.

Tommy, IgorS’s request is really interesting. Would you please consider to write a section in the Advanced Topics of the Programmer’s Guide to explain how to integrate Aspose.Chart with Aspose.Pdf?

Dear Igor,

Thanks for your consideration.

Adding a Chart object from Aspose.Chart to Aspose.Pdf’s document object model directly is not supported. But you can save the Chart object to a stream as image and then add the dynamic image into Aspose.Pdf. Here is an example:

[C#]
Chart chart = new Chart();
chart.SmoothingMode = SmoothingMode.HighQuality;

ASeries s = new ASeries();
s.ChartType = ChartType.Bar;
s.DataPoints.Add( 1 );
s.DataPoints.Add( 2 );
s.DataPoints.Add( 3 );
chart.NSeries.Add( s );

Bitmap bm = chart.Save();
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
bm.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);
System.IO.BinaryReader reader = new System.IO.BinaryReader(mstream);

Pdf pdf1 = new Pdf();

Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
sec1.Paragraphs.Add(image1);

image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp;
image1.ImageInfo.OpenType = ImageOpenType.Memory;
image1.ImageScale = 0.5F;
mstream.Position = 0;
image1.ImageInfo.MemoryData = reader.ReadBytes((int)mstream.Length);

bm.Dispose();

pdf1.Save(“Chart.pdf”);


[VisualBasic]
Dim chart As Chart = New Chart()
chart.SmoothingMode = SmoothingMode.HighQuality

Dim s As ASeries = New ASeries()
s.ChartType = ChartType.Bar
s.DataPoints.Add(1)
s.DataPoints.Add(2)
s.DataPoints.Add(3)
chart.NSeries.Add(s)

Dim bm As Bitmap = chart.Save()
Dim mstream As System.IO.MemoryStream = New System.IO.MemoryStream()
bm.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp)
Dim reader As System.IO.BinaryReader = New System.IO.BinaryReader(mstream)

Dim pdf1 As Pdf = New Pdf()

Dim sec1 As Section = pdf1.Sections.Add()

Dim image1 As Aspose.Pdf.Image = New Aspose.Pdf.Image(sec1)
sec1.Paragraphs.Add(image1)

image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp
image1.ImageInfo.OpenType = ImageOpenType.Memory
image1.ImageScale = 0.5F
mstream.Position = 0
image1.ImageInfo.MemoryData = reader.ReadBytes(CType(mstream.Length, Integer))

bm.Dispose()

pdf1.Save(“Chart.pdf”)


I am currently creating my PDF using the XML method rather than C# is it possible to dynamically add the image in XML as well or will I need to save it to disk first?

Thanks!

Dear AStorm,

Thank you for considering Aspose.

Since XML editting is supported in .NET, you can modify the XML ducoment dynamically to add the image. This is pure XML operation which has nothing to do with Aspose.Pdf so I have no examples. You can refer to documents about how to edit XML in .NET.