Save pdf as png

Hi, I am trying to save only the first page of the PDF as an image using the code below in a .Net Core 5 project

    GemBox.Pdf.ImageSaveOptions options = new GemBox.Pdf.ImageSaveOptions();
    options.Format = GemBox.Pdf.ImageSaveFormat.Png;
    options.PageCount = 1;
    options.PageNumber = 0;
    PdfDocument.Load("document.pdf").Save("Output.png", options);

But the Save throws the following exception:

System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly ‘PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’. The system cannot find the file specified.
Source=GemBox.Pdf

Can you help me?

Hi,

Note that GemBox components currently use WPF for saving to image formats, so you’ll need to enable WPF.

Please check the “Create PNG, JPG, or XPS files on Windows” section on the ASP.NET Core example.

For a Console application, you can use this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Pdf" Version="*" />
  </ItemGroup>

</Project>

Regards,
Mario

Thanks for the quick answer.
I have an ASP.NET Core 5 Web application hosted on an Azure App Service, I don’t know if the .NET Core Runtime and .NET Windows Desktop Runtime are installed on the server.

Could I have problems with the solution you suggested?

Hi Fran,

Unfortunately yes, Azure App Service doesn’t support WPF.

Please check the " Limitations on Azure Functions and App Services" section on our Azure example.

Note that we do have a feature request for cross-platform support for saving to image formats and please feel free to vote for this in order to increase its priority:
https://support.gemboxsoftware.com/community/view/support-for-saving-to-image-format-in-net-standard-2-0

But at the moment I cannot say when this will be available.
Currently we’re working on some other features that have a greater priority (more user requests).

Regards,
Mario

Thanks. You can probably help me find a workaround.
My intention was to create the image (containing a qr code plus custom text) from a pdf page, and then use it as a signature icon (PdfSignatureAppearance.Icon)
Is there a way to put pdf page content as signature content?

For example

Thank you in advance

Hi Fran,

Yes, please check the Digitally sign a PDF file with a visible signature example.

Instead of this line:

signatureAppearance.Icon = PdfImage.Load("GemBoxSignature.png");

You could use something like this:

PdfPage yourPdfPageWithDesiredContent = ...
signatureAppearance.TextPlacement = PdfTextPlacement.IconOnly;
signatureAppearance.Icon = yourPdfPageWithDesiredContent.ConvertToForm(document);

Or you could just add the QR code to PdfSignatureAppearance.Icon and use something like this to add your custom text:

signatureAppearance.Reason = "My custom text for signature's appearance.";
signatureAppearance.ReasonLabel = "";
signatureAppearance.DateFormat = "";

...

signatureField.Sign(signer);
signatureAppearance.Name = "";

I hope this helps.

Regards,
Mario

Thank you very much!
Your solution works perfectly.
I noticed that for some pdf, if I add the signature

var signatureField = document.Form.Fields.AddSignature(page, quad.Left, quad.Bottom, quad.Right - quad.Left, quad.Top - quad.Bottom);
...
signatureField.Sign(signer);

and then I try to save the pdf

document.Save("Visible Digital Signature.pdf");

the operation never ends

Can you help me find the problem?

Hi Fran,

Can you send us a PDF file that reproduces this issue?

Regards,
Mario

Hi Mario,
unfortunately the file contains sensitive data and I must first ask for permission.

Where can I send you the file?

Francesco

You can submit a support ticket or send us an email, see the Contact page.

Hi Mario,
I still don’t have permission to send you the file but I’ve done some testing.
The file with the described problem is a signed pdf, I removed the existing signature:

var signatureFields = document.Form.Fields.
    Where(f => f.FieldType == PdfFieldType.Signature).
    Cast<PdfSignatureField>().
    ToList();

// Either remove the signature or the signature field.
for (int i = 0; i < signatureFields.Count; ++i)
    if (i % 2 == 0)
        signatureFields[i].Value = null;
    else
        document.Form.Fields.Remove(signatureFields[i]);

and then I added my signature:

var signatureField = document.Form.Fields.AddSignature(page, quad.Left, quad.Bottom, quad.Right - quad.Left, quad.Top - quad.Bottom);
...
signatureField.Sign(signer);
document.Save("Visible Digital Signature.pdf");

in this case the operation was completed successfully

Are there any signatures that can cause the anomaly?

I tried with other signed pdf files and I added my signature without problems

I’m not aware of such a problem.
We would need to investigate this in order to resolve any issue.

I finally got permission to send you the file, I sent it to info@gemboxsoftware.com