Big Size Image is Rendered outside the page in output PDF using .NET

When I embed a pdf as an ole object into a document and then save as PDF text selectability is sometimes lost. Pdfs generated form pupeteer appear to lose their text selectability. Pdfs generated form word do not. Note the text is selectable in all pdfs before they are embedded using aspose.words .net 20.10

Sample code:

    static void WordEmbedPDF(string pathToPDF, string pathToImagePart)
    {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        Stream soueceStream = File.OpenRead(pathToImagePart);
        MemoryStream dataStream = new System.IO.MemoryStream();
        soueceStream.CopyTo(dataStream);
        soueceStream.Close();

        builder.InsertOleObject(pathToImagePart, false, false, dataStream);

        PdfSaveOptions options = new PdfSaveOptions();
        options.EmbedFullFonts = true;
        options.Compliance = PdfCompliance.PdfA1a;
        doc.Save(pathToPDF + "-ole-output.pdf", options);

        doc.Save(pathToPDF + "-ole-output.docx");
    }

I have attached the input and output files. Note that the e753385c-2a84-4cdf-8fcc-4de6e5dbee72.pdf-ole-output.pdf has lost it’s text selectability.

insert ole - text selectability lost.zip (717.1 KB)

@kurtosys

Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Stream soueceStream = File.OpenRead(MyDir + "e753385c-2a84-4cdf-8fcc-4de6e5dbee72.emf");
MemoryStream dataStream = new System.IO.MemoryStream();
soueceStream.CopyTo(dataStream);
soueceStream.Close();
               
Shape shape = builder.InsertOleObject(MyDir + "e753385c-2a84-4cdf-8fcc-4de6e5dbee72.emf", false, false, dataStream);

ResizeLargeImage(shape);

PdfSaveOptions options = new PdfSaveOptions();
options.EmbedFullFonts = true;
options.Compliance = PdfCompliance.PdfA1a;
doc.WarningCallback = new HandleDocumentWarnings();
doc.Save(MyDir + "-ole-output.pdf", options);
doc.Save(MyDir + "-ole-output.docx"); 

public static void ResizeLargeImage(Shape image)
{
    // Return if this shape is not an image.
    if (!image.HasImage)
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.ParentParagraph.ParentSection.PageSetup;
    double freePageWidth = image.IsInline ? ps.PageWidth - ps.LeftMargin - ps.RightMargin : ps.PageWidth;
    double freePageHeight = image.IsInline ? ps.PageHeight - ps.TopMargin - ps.BottomMargin : ps.PageHeight;


    ImageSize size = image.ImageData.ImageSize;
    Boolean exceedsMaxPageSize = size.WidthPoints > freePageWidth || size.HeightPoints > freePageHeight
        || image.Width > freePageWidth || image.Height > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        Boolean widthLonger = (size.WidthPoints > size.HeightPoints);
        double ratio = widthLonger ? freePageWidth / size.WidthPoints : freePageHeight / size.HeightPoints;

        // Set the new size.
        image.Width = size.WidthPoints * ratio;
        image.Height = size.HeightPoints * ratio;
    }
}