本文整理汇总了C#中PdfDocument.GetPage方法的典型用法代码示例。如果您正苦于以下问题:C# PdfDocument.GetPage方法的具体用法?C# PdfDocument.GetPage怎么用?C# PdfDocument.GetPage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PdfDocument
的用法示例。
在下文中一共展示了PdfDocument.GetPage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateCoverImage
public async Task<string> GenerateCoverImage(Book book, uint pageIndex, ISourceRepository sourceRepository, StorageFolder storageFolder, StorageFile pdfFile)
{
try
{
// var pdfFile = await storageFolder.GetFileAsync(book.FileName);
try
{
_pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile);
}
catch (Exception)
{
return null;
}
if (_pdfDocument == null || _pdfDocument.PageCount <= 0) return null;
//Get Pdf page
using (var pdfPage = _pdfDocument.GetPage(pageIndex))
{
if (pdfPage == null) return null;
// next, generate a bitmap of the page
var thumbfolder = await Globals.GetCoversFolder();
var pngFile = await thumbfolder.CreateFileAsync(Utils.GenerateRandomString() + ".png", CreationCollisionOption.ReplaceExisting);
if (pngFile == null) return null;
using (var randomStream = await pngFile.OpenAsync(FileAccessMode.ReadWrite))
{
await pdfPage.RenderToStreamAsync(randomStream, new PdfPageRenderOptions() { DestinationHeight = 340, DestinationWidth = 240 });
await randomStream.FlushAsync();
}
return pngFile.Path;
}
}
catch (Exception)
{
//rootPage.NotifyUser("Error: " + err.Message, NotifyType.ErrorMessage);
return null;
}
}
示例2: LoadPdfFileAsync
private async System.Threading.Tasks.Task LoadPdfFileAsync()
{
try
{
StorageFile pdfFile = await ApplicationData.Current.LocalFolder.GetFileAsync("2.pdf");
//Load Pdf File
pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile); ;
ObservableCollection<SampleDataItem> items = new ObservableCollection<SampleDataItem>();
this.DefaultViewModel["Items"] = items;
//StorageFolder tempFolderDel = ApplicationData.Current.TemporaryFolder;
// await tempFolderDel.DeleteAsync(StorageDeleteOption.Default);
// Get the app's local folder.
//StorageFile localFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync("ba4755e5-520b-42eb-bcf8-1f0cddd5e5ca.png");
//await localFile.DeleteAsync();
// Create a new subfolder in the current folder.
// Raise an exception if the folder already exists.
//string desiredName = "TempState2";
//StorageFolder newFolder = await localFolder.CreateFolderAsync(desiredName,CreationCollisionOption.FailIfExists);
if (pdfDocument != null && pdfDocument.PageCount > 0)
{
string file = "";
//Get Pdf page
for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++)
{
var pdfPage = pdfDocument.GetPage((uint)pageIndex);
if (pdfPage != null)
{
// next, generate a bitmap of the page
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
StorageFile pngFile = await tempFolder.CreateFileAsync((file=Guid.NewGuid().ToString() + ".png"), CreationCollisionOption.ReplaceExisting);
files_for_Delete.Add(file);
if (pngFile != null)
{
IRandomAccessStream randomStream = await pngFile.OpenAsync(FileAccessMode.ReadWrite);
PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
pdfPageRenderOptions.DestinationWidth = (uint)(this.ActualWidth - 130);
await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
await randomStream.FlushAsync();
randomStream.Dispose();
pdfPage.Dispose();
items.Add(new SampleDataItem(
pageIndex.ToString(),
pageIndex.ToString(),
pngFile.Path));
}
}
}
}
t = true;
}
catch (Exception err)
{
t = false;
}
}
示例3: LoadPdf
private void LoadPdf(PdfDocument pdfDocument, StackPanel stackPanel, ScrollViewer pdfScrollView)
{
_pdfDocument = pdfDocument;
_pdfStack = stackPanel;
for (uint i = 0; i < pdfDocument.PageCount; i++)
{
stackPanel.Children.Add(CreateImageContainer(pdfDocument.GetPage(i).Dimensions.ArtBox.Height, pdfDocument.GetPage(i).Dimensions.ArtBox.Width, (int)i+1, (int)pdfDocument.PageCount));
}
LoadPdfImage(0, (Grid)stackPanel.Children[0], (Image)((Grid)(stackPanel.Children[0])).Children[0]);
pdfScrollView.Loaded += PdfScrollView_Loaded;
}
示例4: RenderPDFPage
/// <summary>
/// This renders PDF page with render options
/// Rendering a pdf page requires following 3 steps
/// 1. PdfDocument.LoadFromFileAsync(pdfFile) which returns pdfDocument
/// 2. pdfDocument.GetPage(pageIndex)
/// 3. pdfPage.RenderToStreamAsync(stream) or pdfPage.RenderToStreamAsync(stream,pdfPageRenderOptions)
/// </summary>
private async Task RenderPDFPage(string pdfFileName,RENDEROPTIONS renderOptions)
{
try
{
StorageFile pdfFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(pdfFileName);
//Load Pdf File
_pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile); ;
if (_pdfDocument != null && _pdfDocument.PageCount > 0)
{
//Get Pdf page
var pdfPage = _pdfDocument.GetPage(PDF_PAGE_INDEX);
if (pdfPage != null)
{
// next, generate a bitmap of the page
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
StorageFile jpgFile = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".png", CreationCollisionOption.ReplaceExisting);
if (jpgFile != null)
{
IRandomAccessStream randomStream = await jpgFile.OpenAsync(FileAccessMode.ReadWrite);
PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
switch (renderOptions)
{
case RENDEROPTIONS.NORMAL:
//Render Pdf page with default options
await pdfPage.RenderToStreamAsync(randomStream);
break;
case RENDEROPTIONS.ZOOM:
//set PDFPageRenderOptions.DestinationWidth or DestinationHeight with expected zoom value
Size pdfPageSize = pdfPage.Size;
pdfPageRenderOptions.DestinationHeight = (uint)pdfPageSize.Height * ZOOM_FACTOR;
//Render pdf page at a zoom level by passing pdfpageRenderOptions with DestinationLength set to the zoomed in length
await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
break;
case RENDEROPTIONS.PORTION:
//Set PDFPageRenderOptions.SourceRect to render portion of a page
pdfPageRenderOptions.SourceRect = PDF_PORTION_RECT;
//Render portion of a page
await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
break;
}
await randomStream.FlushAsync();
randomStream.Dispose();
pdfPage.Dispose();
await DisplayImageFileAsync(jpgFile);
}
}
}
}
catch (Exception err)
{
rootPage.NotifyUser("Error: " + err.Message, NotifyType.ErrorMessage);
}
}