attached are my results:
Instead of the contents of the pdf file (which I want), I get either an icon (inserted as an icon, I expect that) or an aspose picture. (assuming because the last parameter is null)
what would I set that last parameter to (using insertOleObject asOle.pdf (28.6 KB) AsIcon.pdf (27.0 KB)
When a PDF is embedded in a document, Microsoft Word converts the first page of that PDF to an EMF and then inserts this EMF. You can achieve the same with the following code:
Document pdf = new Aspose.Words.Document(@"embedded.pdf");
MemoryStream pdfPreviewEmfStream = new MemoryStream();
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Emf);
opts.Scale = 0.2f; // Scale image down, so it fits in the page
opts.PageSet = new PageSet(0); // Select the first page
pdf.Save(pdfPreviewEmfStream, opts);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
Cell cell = builder.InsertCell();
builder.MoveTo(cell.FirstParagraph);
builder.InsertOleObject(@"embedded.pdf", false, false, pdfPreviewEmfStream);
builder.EndRow();
builder.EndTable();
doc.Save(@"pdfembeddedindocument.docx");
better.
next question : best way of resizing, based on the width of the cell (which will be nearly width of page)
I don’t want to force it to fit on a page (because some of the real life pdf files wouldn’t fit on a page), but I do want the width to match. scaled.pdf (113.7 KB) notScaled.pdf (115.1 KB)
With the known cell width and the width of PDF’s page you can calculate the scale factor like it is shown in the code given below:
Document doc = new Document(@"bSpecs.dot");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move inside of the first paragraph in the first cell of the first table.
builder.MoveToCell(0, 0, 0, 0);
Cell cell = (Cell)builder.CurrentNode.ParentNode.ParentNode;
double cellWidth = cell.CellFormat.Width;
// Optional: limit the height of the row. The PDF preview image will be truncated in this case.
const double MaxRowHeight = 500.0;
RowFormat rowFormat = cell.ParentRow.RowFormat;
rowFormat.HeightRule = HeightRule.Exactly;
rowFormat.Height = MaxRowHeight;
// Generate PDF preview by converting its first page to an EMF image.
Document pdf = new Document(dataDir + "embedded.pdf");
Aspose.Words.Rendering.PageInfo pageInfo = pdf.GetPageInfo(0);
// Scale the PDF preview, so it fits in the cell
float scaleFactor = (float)(cellWidth / pageInfo.WidthInPoints);
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Emf);
opts.Scale = scaleFactor;
opts.PageSet = new PageSet(0); // Select the first page
MemoryStream pdfPreviewEmfStream = new MemoryStream();
pdf.Save(pdfPreviewEmfStream, opts);
// Embed the PDF
builder.InsertOleObject(@"embedded.pdf", false, false, pdfPreviewEmfStream);
doc.Save(@"pdfembeddedindocument.scaled.docx");
so, after running tests, this is working fine, if the pdf file has only one page.
my code:
Aspose.Words.Document pdf = new Aspose.Words.Document(fileName);
MemoryStream pdfPreviewEmfStream = new MemoryStream();
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Emf);
Aspose.Words.Rendering.PageInfo pageInfo = pdf.GetPageInfo(0);
float scaleFactor = (float)(m_Cell.CellFormat.Width/ pageInfo.WidthInPoints);
opts.Scale = scaleFactor;
opts.PageSet = new PageSet(0); // Select the first page
// Calculate the ratio to fit the page size based on which side is longer.
pdf.Save(pdfPreviewEmfStream, opts);
builder.MoveTo(m_Cell.FirstParagraph);
builder.InsertOleObject(fileName, false, false, pdfPreviewEmfStream);
attached is the pdf file, and there result. note the pdf file has 2 pages, but only the first page is showing on the result.
from your initial response:
When a PDF is embedded in a document, Microsoft Word converts the first page of that PDF to an EMF and then inserts this EMF. You can achieve the same with the following code:
@conniem Could you please create an expected output document in MS Word and provide it here? We will check it and provide you more information.
As an option you can save each page to thumbnail, for example see Document.RenderToScale method.
I worked out how to deal with this, (my delphi codes saves to a tif file, then splits the tiff into separate pages and embeds those). I was just wondering if there was a way to do it automatically, without all the processing I have to do to embed tiff files, or pdf files.
@conniem It is perfect that you managed to achieve what you need. And no, unfortunately, there is no way to automatically to this, you have to render each page of your PDF and then put them together.