How to insert content of another pdf file into pdf generator

Hi,


I am using Aspose.pdf.Generator to create a pdf document. During the creation, I need to insert content of another pdf file. I don’t want to insert the pdf document as an attachment, but need to show the content of the pdf file. Something like convert memory stream image to PDF from the below link.
http://www.aspose.com/docs/display/pdfnet/How+to+Convert+MemoryStream+Image+to+PDF

How can I achieve that?


Regards,
Iris

Hi there,


Thanks for your inquiry. To achieve your requirement, first convert your existing PDF document to image and save to memory stream. Later add that image to new PDF document as described in your shared link. Hopefully it will serve the purpose.

Please feel free to contact us for any further assistance.

Best Regards,

Thanks. It works for me.


However, I am facing another problem, when I add the image to a table cell, how can I fit the image properly into the cell and keep it in one page. For example, I have header and footer in one page and a table with two rows and one column. The first row is the title and I would like to put the converted image from the pdf file (one page) into the second row of the table. I set IsImageFitToRowHeight property to true, but it doesn’t work (the image is shown in second page and the first page is shown only the first row). I try to calculate the maximum height of the cell that can fit in one page, but I cannot figure out the height of the header and footer. Is there a way to achieve this?

Thanks again.

Regards,
Iris

Hi Iris,


Thanks for your feedback. To fit image into cell you need to set Image height/width accordingly. Please check following sample code snippet. Hopefully it will help you to accomplish your requirements.

Pdf pdf1 = new Pdf();<o:p></o:p>

//Create the section in the Pdf object

Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Generator.Table headerTable = new Aspose.Pdf.Generator.Table();

//Add the table in paragraphs collection of the desired section

sec1.Paragraphs.Add(headerTable);

headerTable.ColumnWidths = "100%";

var row1 = headerTable.Rows.Add();

row1.DefaultCellTextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;

row1.DefaultCellTextInfo.FontName = "Helvetica-Bold";

row1.DefaultCellTextInfo.FontSize = 16;

var cell1 = row12.Cells.Add("Title");

cell1.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center;

var row2 = headerTable.Rows.Add();

row2.FixedRowHeight = 600;

Aspose.Pdf.Generator.Cell cell2 = row2.Cells.Add("");

cell2.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center;

Aspose.Pdf.Generator.Image logo = new Aspose.Pdf.Generator.Image();

logo.ImageInfo.File = myDir + "sunset.jpg";

logo.ImageInfo.ImageFileType = ImageFileType.Jpeg;

logo.ImageInfo.FixHeight = row2.FixedRowHeight;

logo.ImageInfo.FixWidth = sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right;

Text imageContainer = new Text();

Segment imageSegment = new Segment();

imageSegment.InlineParagraph = logo;

imageContainer.Segments.Add(imageSegment);

cell2.Paragraphs.Add(imageContainer);

pdf1.Save(myDir+"Table_ImageGen.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

Thanks for your reply.


In your sample code, still I need to know the height of the row (row2.FixedRowHeight = 600). My situation is I don’t know the exact row height remaining in the table.

Regards,
Iris



Hi Iris,


Thanks for your feedback. We’re looking into your query and will get back to you soon.

Best Regards,

Hi Iris,


Thanks for your patience. Please use following code snippet to find remaining space in page to fit image to same page. Hopefully it will serve the purpose.

// to find
remaining space in page for image height deduct page top margin, page bottom margin,
<o:p></o:p>

//table height, table top margin, pagerBorderMaring top and bottom from page height

logo.ImageInfo.FixHeight = sec1.PageInfo.PageHeight - sec1.PageInfo.Margin.Top - sec1.PageInfo.Margin.Bottom

- headerTable.GetHeight(pdf1) - headerTable.Margin.Top- sec1.PageInfo.PageBorderMargin.Top - sec1.PageInfo.PageBorderMargin.Bottom;

logo.ImageInfo.FixWidth = sec1.PageInfo.PageWidth - sec1.PageInfo.Margin.Left - sec1.PageInfo.Margin.Right;

.....

.....


Please feel free to contact us for any further assistance.


Best Regards,