Hi!
I want to export a part of a pdf to an image. (code)
If the pdf is very large i want to zoom in before generating the image. Any Ideas?
My Code (working without zoom)
PdfLoadedDocument ldoc = new PdfLoadedDocument(filestream);
PdfDocument doc = new PdfDocument();
PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage;
RectangleF cropBounds = new RectangleF(100, 200, lpage.Size.Width, 300);
PdfSection section = doc.Sections.Add();
section.PageSettings.Size = new SizeF(300, 300);
PdfPage page = section.Pages.Add();
PdfGraphics graphics = page.Graphics;
PdfTemplate template = lpage.CreateTemplate();
graphics.DrawPdfTemplate(template, new PointF(-cropBounds.X, -cropBounds.Y));
PdfRenderer pdfExportImage = new PdfRenderer();
MemoryStream cropdoc = new MemoryStream();
doc.Save(cropdoc);
pdfExportImage.Load(cropdoc);
SKBitmap bitmapimage = pdfExportImage.ExportAsImage(0);
SKImage image = SKImage.FromBitmap(bitmapimage);
var imageData = image.Encode(SKEncodedImageFormat.Png, 100);
MemoryStream final = new MemoryStream();
imageData.SaveTo(final);
doc.Close(true);
ldoc.Close(true);
final.Position = 0;
string base64String = Convert.ToBase64String(final.ToArray());
string returnString = "data:" + "image/png" + ";base64, " + base64String;
return returnString;
Regards, Bernhard
We suggest you create a PDF document that sizes lesser than that of the input document the draw the template of the existing pdf document into a new document. We have followed the same on our end and it worked well. We will share here the code snippet and sample for your reference. Please try this on your end and let us know the result.
FileStream filestream = new FileStream("Input.pdf",FileMode.Open,FileAccess.ReadWrite); PdfLoadedDocument ldoc = new PdfLoadedDocument(filestream); PdfDocument doc = new PdfDocument(); PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage; doc.PageSettings.Size = new SizeF(300, 400); doc.PageSettings.Margins.All=0; PdfPage page = doc.Pages.Add(); PdfGraphics graphics = page.Graphics; PdfTemplate template = lpage.CreateTemplate(); graphics.DrawPdfTemplate(template, new PointF(0,0), new SizeF(page.GetClientSize().Width,page.GetClientSize().Height)); MemoryStream input = new MemoryStream(); doc.Save(input); System.IO.File.WriteAllBytes("Output.pdf",input.ToArray()); PdfRenderer pdfviewer = new PdfRenderer(); var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "Output.pdf"); var docPath = System.IO.File.OpenRead(imagePath); pdfviewer.Load(docPath); SKBitmap bitmapimage = pdfviewer.ExportAsImage(0); var image = SKImage.FromBitmap(bitmapimage); using (var stream1 = new FileStream(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page0.png"), FileMode.Create)) { var data1 = image.Encode(SKEncodedImageFormat.Png,500); data1.SaveTo(stream1); } var imageFileStream = System.IO.File.OpenRead(Path.Combine(_hostingEnvironment.WebRootPath, "data/Page0.png")); return File(imageFileStream, "image/png"); |
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication51371067884