Free Support Forum - aspose.com

PdfFileAmend saving empty file


#1

Hi,

I am trying to add an image to a PDF File like below, and for some reason it’s spitting out just the empty file and not writing anything back to the output file. Anything I am doing wrong here?

using System;
using Aspose.Pdf.Facades;
using Aspose.Pdf;
using System.IO;

namespace DocumentSigning
{
    class Program
    {
        private const string InputFileName = "/path/to/input.pdf";
        private const string OutputFileName = "/path/to/output.pdf";
        private const string signatureFile = "/path/to/image.png";

        static void Main(string[] args)
        {
            FileStream inImgStream = new FileStream(@signatureFile, FileMode.Open);
            FileStream outputStream = new FileStream(@OutputFileName, FileMode.Create);

            Document doc = new Document(InputFileName);

            PdfFileMend fileMend = new PdfFileMend(doc);

            fileMend.AddImage(inImgStream, 1, 50, 50, 100, 100);

            doc.Save(outputStream, SaveFormat.Pdf);

            fileMend.Close();

            outputStream.Close();

            Console.WriteLine("!!FINISHED EDITING THE PDF!!");
        }
    }
}

#2

@harishupad

Thank you for contacting support.

Would you please try using below code snippet to add an image to a PDF page with Aspose.PDF for .NET 19.3 and then share your kind feedback with us.

// Instantiate Document Object
Document doc = new Document();
// Add a page to pages collection of document
Page page = doc.Pages.Add();
// Load the source image file to Stream object
FileStream fs = new FileStream( dataDir + @"Test.jpg", FileMode.Open);
// Set margins so image will fit
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = fs;
// Save resultant PDF file
doc.Save(dataDir + "JPG2PDF_19.3.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

Thank you for the response @Farhan.Raza. I think the code seems to be adding an image on an empty PDF, I am trying to add an image on a specific location of the file with whole bunch of pages with text already in it already, is it possible to do so?


#4

@harishupad

Thank you for elaborating it further.

You may add an image on any location of a PDF page by specifying the values of XIndent and YIndentproperties of ImageStamp as explained in Adding Image Stamp in PDF File.

Feel free to share your source and expected output files so that we may investigate further in case of any problem.


#5

Thank you @Farhan.Raza , I really appreciate your prompt response.

This is my scenario:
I have a PDF document with bunch of text and I am trying to search for a specific text ( key word ) and replace that text/key word with an Image. While doing that, I am getting the following error.

"Invalid image stream (The type initializer for 'Gdip' threw an exception.)"

I am running .NET Core on Mac OS, if that helps

bird.jpg (98.7 KB)

Attaching the Image as well just in case, you want to try

  static void Main(string[] args)
    {
        FileStream signatureImageStream = new FileStream(@signatureFile, FileMode.Open);
        FileStream outputStream = new FileStream(@OutputFileName, FileMode.Create);

        Document pdfDocument = new Document(InputFileName);
        // Create TextAbsorber object to find all instances of the input search phrase
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Independent");

        // Accept the absorber for all the pages
        pdfDocument.Pages.Accept(textFragmentAbsorber);

        // Get the extracted text fragments
        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

        // Loop through the fragments
        int count = 1;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            count += 1;
            Console.WriteLine("Text Fragment Count: {0}", count);

            // Set coordinates
            int lowerLeftX = (int)textFragment.Position.XIndent;
            int lowerLeftY = (int)textFragment.Position.YIndent;
            int upperRightX = (int)textFragment.Position.XIndent + 50;
            int upperRightY = (int)textFragment.Position.YIndent + 50;

            Page page = pdfDocument.Pages[count];

            page.Resources.Images.Add(signatureImageStream);

            Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

            Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

            page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));

            XImage ximage = page.Resources.Images[page.Resources.Images.Count];

            page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

            page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

            pdfDocument.Save(OutputFileName);

            Console.WriteLine("\nImage added successfully.\nFile saved at " + OutputFileName);
        }
    }

#6

@harishupad

Thank you for elaborating it further.

We have noticed that the same code works fine in Windows 10, thus in Mac OS please install:

  1. libgdiplus package
  2. package with Microsoft compatible fonts: ttf-mscorefonts-installer. (e.g.: sudo apt-get install ttf-mscorefonts-installer)

These fonts should be placed in “/usr/share/fonts/truetype/msttcorefonts” folder (our API scan this folder on Linux like systems). If OS has other default folder for fonts, you should add following command at the beginning of your code:

Aspose.Pdf.Text.FontRepository.Sources.Add(new FolderFontSource("<user's path to ms fonts>"))


#7

@Farhan.Raza thank you once again for your response. I installed mono-libgdiplus and seems to go beyond the above error. Now I am getting a new error

System.NullReferenceException: "Object reference not set to an instance of an object."

and it’s happening while saving the pdf to an output file. I checked “pdfDocument” object is not Null and OutputFileName is not empty either.

pdfDocument.Save(OutputFileName);

static void Main(string[] args)
    {
        FileStream signatureImageStream = new FileStream(@signatureFile, FileMode.Open);
        FileStream outputStream = new FileStream(@OutputFileName, FileMode.Create);

        Document pdfDocument = new Document(InputFileName);
        // Create TextAbsorber object to find all instances of the input search phrase
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Independent");

        // Accept the absorber for all the pages
        pdfDocument.Pages.Accept(textFragmentAbsorber);

        // Get the extracted text fragments
        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

        // Loop through the fragments
        int count = 1;
        foreach (TextFragment textFragment in textFragmentCollection)
        {
            count += 1;
            Console.WriteLine("Text Fragment Count: {0}", count);

            // Set coordinates
            int lowerLeftX = (int)textFragment.Position.XIndent;
            int lowerLeftY = (int)textFragment.Position.YIndent;
            int upperRightX = (int)textFragment.Position.XIndent + 50;
            int upperRightY = (int)textFragment.Position.YIndent + 50;

            Page page = pdfDocument.Pages[count];

            page.Resources.Images.Add(signatureImageStream);

            Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

            Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

            page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));

            XImage ximage = page.Resources.Images[page.Resources.Images.Count];

            page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

            page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

            pdfDocument.Save(OutputFileName);

            Console.WriteLine("\nImage added successfully.\nFile saved at " + OutputFileName);
        }
    }

#8

@Farhan.Raza sounds like I got past through all the problems, The NullReferenceException is because I am on a Trail Version and it wasn’t letting me save it. I got Temporary License and after running, it seems to be working just fine. Thank you so much for the support and I gotta say, ASPOSE is pretty awesome.


#9

@harishupad

Thanks for your kind feedback.

It is good to know that your issue has been resolved. Please keep using our API and in case of any further assistance, please feel free to let us know.