We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

URL Content as Image in PDF

Hi Team,


We have a requirement, where a URL is given by the User as Input.
The Content of the URL should be captured as an Image and Saved to PDF.

Is it possible with the current Aspose PDF Library?

Thanks,
Koundinya P

Hi Koundinya,


Thanks for contacting support.

Is it possible with the current Aspose PDF Library?

Yes, it is possible with Aspose.Pdf API to read the content of the URL and then save them as an image in the PDF document. Please see following code snippet as a sample.

C#

WebRequest request = WebRequest.Create(“https://En.wikipedia.org/wiki/Main_Page”);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Time out in miliseconds before the request times out
// Request.Timeout = 100;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();

MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseFromServer));
HtmlLoadOptions options = new HtmlLoadOptions(“https://En.wikipedia.org/wiki/”);
// Load HTML file
Document pdfDocument = new Document(stream, options);

options.PageInfo.IsLandscape = true;

// Save output as PDF format
MemoryStream memorystream = new MemoryStream();
pdfDocument.Save(memorystream);
Document final_doc = new Document(memorystream);

for (int pageCount = 1; pageCount <= final_doc.Pages.Count; pageCount++)
{
MemoryStream imageStream = new MemoryStream();
// create Resolution object
Resolution resolution = new Resolution(150);
// create PngDevice object with particular resolution
JpegDevice jpgDevice = new JpegDevice(resolution);
// convert a particular page and save the image to stream
jpgDevice.Process(final_doc.Pages[pageCount], imageStream);
// Get the page where image needs to be added
Page final_page = final_doc.Pages.Add();
// Load image into stream
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
final_page.Paragraphs.Add(img);
img.ImageStream = imageStream;
img.HorizontalAlignment = HorizontalAlignment.Center;
img.ImageScale = 1.0;

final_doc.ProcessParagraphs();
imageStream.Close();
}

final_doc.Save(dataDir + “final_file1.pdf”);

If you still face any issue, please feel free to contact us.

Best Regards,

Hi Fahad,


Thanks for your reply. This is really helpful.

We have a license file but facing issues when including it in the solution.

Code Snippet for license:
<div class=“line number1 index0 alt2” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace; font-size: 14px; white-space: pre-wrap; color: rgb(51, 51, 51); border-radius: 0px !important; background: none rgb(255, 255, 255) !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;”><code class=“java plain” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important;”>Aspose.Pdf.License license = <code class=“java keyword” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-weight: bold !important; min-height: auto !important; color: rgb(51, 102, 153) !important;”>new <code class=“java plain” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important;”>Aspose.Pdf.License();
<div class=“line number2 index1 alt1” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace; font-size: 14px; white-space: pre-wrap; color: rgb(51, 51, 51); border-radius: 0px !important; background: none rgb(255, 255, 255) !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em 0px 0em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important;”><code class=“java plain” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important;”>license.SetLicense(<code class=“java string” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 51, 102) !important;”>“C:\Aspose.Pdf.lic”<code class=“java plain” style=“font-family: Consolas, “Bitstream Vera Sans Mono”, “Courier New”, Courier, monospace !important; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 20px !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; color: rgb(0, 0, 0) !important;”>);

Can you let us know how we can integrate our license file to the above code.

Thanks,
Koundinya P

Hi Koundinya,


Thanks for your reply. This is really helpful.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

Thanks for your feedback. It is good to know that suggested code helped you.


Can you let us know how we can integrate our license file to the above code.

Thanks for your inquiry. We will appreciate it if you please confirm how you are applying the license. Please make sure that you have set the license before using any other object of Aspose.Pdf API. However, if the issue persist then please share sample project along with your license file via email as suggested here to replicate the issue.
Furthermore, It is suggested to set license once in some Application Level event. When you will set the license in application level event/scope, it will remain valid till the lifespan of application. Please try to set license as per your application structure and share the results.

  • If you are developing an ASP.NET application, you can call License.SetLicense from the Global.asax.cs (Global.asax.vb) file, in the Application_Start protected method. It is called once when the application starts. Do not call License.SetLicense from within Page_Load methods since it means the license will be loaded every time a web page is loaded.
  • If you are developing a Windows Forms or console application, call License.SetLicense in your startup code, before using Aspose.Pdf for .NET classes.
  • If you are developing a class library, you can call License.SetLicense from a static constructor of your class that uses Aspose.Pdf for .NET. The static constructor will execute before an instance of your class is created making sure Aspose.PDF for .NET license is properly set.
  • in case of WCF service, depending upon its hosting:
- if it is custom ServiceHost - then you can set license in service host start event
- if it is IIS - then set license can be called in Global.asax

We are sorry for the inconvenience.

Best Regards,

Hi Team,


I am looking at a similar feature. I have a WebPage which contains multiple Charts.
In this case, would the same code can help me capture the Webpage as image and place the image into PDF?

Thanks,
Pavithra

Hi Pavithra,


Thanks for your inquiry.

You can definitely try above shared code snippet to achieve the requirement and in case if you face any issue, please share your HTML file or Webpage URL, so that we can test the scenario in our environment and address it accordingly.


Best Regards,