如何在Xamarin表单中生成PDF第一页的缩略图

时间:2018-11-27 17:48:32

标签: pdf xamarin.forms png syncfusion

我正在使用Xamarin Forms的Syncfusion PDF查看器来显示PDF文件的集合,似乎是从PDF转换为PNG(提取PDF文件的第一页(封面)以显示它)到用户,进入轮播)在Xamarin中不起作用[请参阅https://www.syncfusion.com/kb/9112/how-to-convert-pdf-to-png]

我想知道是否有一种方法可以在Xamarin平台上将PDF转换为PNG,还是应该在服务器端将其转换。

谢谢。

1 个答案:

答案 0 :(得分:6)

通过使用PdfRendererCGPDFDocumentPdfDocument类,可以在不使用Syncfusion PDF Viewer控件的情况下将PDF页面导出到图像。

Xamarin.Forms.Android:

protected override void OnCreate(Bundle savedInstanceState)
{
  base.OnCreate(savedInstanceState);
  SetContentView(Resource.Layout.activity_main);

  // ... do some stuff here, that is really not important for this post...

  // Get the FrameManager (in Java this is like: FragmentManager fm = getFragmentManager();
  var fragmentManager = this.SupportFragmentManager; 

  // Start transaction
  var transaction = fragmentManager.BeginTransaction();

  // Add a new fragment of type FragmentA that derives from FragmenBase.
  transaction.Add(Resource.Id.frame_base, new FragmentA(), "FrgmA");

  // Make it happen
  transaction.Commit();
}

private void OnToolbarButton_A()
{
  // Replace current fragment by FragmentA
  var fragmentManager = this.SupportFragmentManager;
  var transaction = fragmentManager.BeginTransaction();
  transaction.Replace(Resource.Id.frame_base, new FragmentA(), "FrgmA");
  transaction.Commit();
}

private void OnToolbarButton_B()
{
  // Replace current fragment by FragmentB
  var fragmentManager = this.SupportFragmentManager;
  var transaction = fragmentManager.BeginTransaction();
  transaction.Replace(Resource.Id.frame_base, new FragmentB(), "FrgmB");
  transaction.Commit();
}

Xamarin.Forms.iOS:

//initialize PDFRenderer by passing PDF file from location.
PdfRenderer renderer = new PdfRenderer(GetSeekableFileDescriptor()); 
int pageCount = renderer.PageCount;
for(int i=0;i<pageCount;i++)
{
// Use `openPage` to open a specific page in PDF.
Page page =  renderer.OpenPage(i); 
//Creates bitmap
Bitmap bmp = Bitmap.CreateBitmap(page.Width, page.Height, Bitmap.Config.Argb8888); 
//renderes page as bitmap, to use portion of the page use second and third parameter
page.Render(bmp, null, null, PdfRenderMode.ForDisplay);
//Save the bitmap
SaveImage(bmp);
page.Close();
}

//Method to retrieve PDF file from the location
private ParcelFileDescriptor GetSeekableFileDescriptor()
{
ParcelFileDescriptor fileDescriptor = null;
try
{
string root = Android.OS.Environment.ExternalStorageDirectory.ToString()+ "/Syncfusion/sample.pdf";
fileDescriptor = ParcelFileDescriptor.Open(new Java.IO.File(root),ParcelFileMode.ReadOnly
);
}
catch (FileNotFoundException e)
{

}
return fileDescriptor;
}

Xamarin.Forms.UWP

public void ConvertToImage(Stream fileStream)  //Pass PDF stream

{
MemoryStream stream = new MemoryStream();
// Create memory stream from file stream.
fileStream.CopyTo(stream);
// Create data provider from bytes.
CGDataProvider provider = new CGDataProvider(stream.ToArray());
try
{
//Load a PDF file.
m_pdfDcument = new CGPDFDocument(provider);
}
catch (Exception)
{
}
//Get PDF's page and convert as image.
using (CGPDFPage pdfPage = m_pdfDcument.GetPage(2))
{
//initialise image context.
UIGraphics.BeginImageContext(pdfPage.GetBoxRect(CGPDFBox.Media).Size);
// get current context.
CGContext context = UIGraphics.GetCurrentContext();
context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
// Gets page's bounds.
CGRect bounds = new CGRect(pdfPage.GetBoxRect(CGPDFBox.Media).X, pdfPage.GetBoxRect(CGPDFBox.Media).Y, pdfPage.GetBoxRect(CGPDFBox.Media).Width, pdfPage.GetBoxRect(CGPDFBox.Media).Height);
if (pdfPage != null)
{
context.FillRect(bounds);
context.TranslateCTM(0, bounds.Height);
context.ScaleCTM(1.0f, -1.0f);
context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, bounds, 0, true));
context.SetRenderingIntent(CGColorRenderingIntent.Default);
context.InterpolationQuality = CGInterpolationQuality.Default;
// Draw PDF page in the context.
context.DrawPDFPage(pdfPage);
// Get image from current context.
pdfImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
}
}
// Get bytes from UIImage object.
using (var imageData = pdfImage.AsPNG())
{
imageBytes = new byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, imageBytes, 0, Convert.ToInt32(imageData.Length));
//return bytes;
}
//Create image from bytes.
imageStream = new MemoryStream(imageBytes);
//Save the image. It is a custom method to save the image
Save("PDFtoImage.png", "image/png", imageStream);
}

我为Syncfusion工作。