Skip to content

Commit

Permalink
Added the ability to load PDF documents from memory (byte[])
Browse files Browse the repository at this point in the history
  • Loading branch information
reliak committed Nov 28, 2013
1 parent 43f9413 commit 873eb7a
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/MoonPdf/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Commands
public Commands(MainWindow wnd)
{
var pdfPanel = wnd.MoonPdfPanel;
Predicate<object> isPdfLoaded = f => wnd.IsFileOpen(); // used for the CanExecute callback
Predicate<object> isPdfLoaded = f => wnd.IsPdfLoaded(); // used for the CanExecute callback

this.OpenCommand = new DelegateCommand("Open...", f =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/MoonPdf/FullscreenCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void wnd_PreviewKeyDown(object sender, KeyEventArgs e)

public override bool CanExecute(object parameter)
{
return wnd.IsFileOpen();
return wnd.IsPdfLoaded();
}

public override void Execute(object parameter)
Expand Down
21 changes: 16 additions & 5 deletions src/MoonPdf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public MainWindow()
moonPdfPanel.ViewTypeChanged += moonPdfPanel_ViewTypeChanged;
moonPdfPanel.ZoomTypeChanged += moonPdfPanel_ZoomTypeChanged;
moonPdfPanel.PageRowDisplayChanged += moonPdfPanel_PageDisplayChanged;
moonPdfPanel.FileLoaded += moonPdfPanel_FileLoaded;
moonPdfPanel.PdfLoaded += moonPdfPanel_PdfLoaded;
moonPdfPanel.PasswordRequired += moonPdfPanel_PasswordRequired;

this.UpdatePageDisplayMenuItem();
Expand Down Expand Up @@ -137,7 +137,7 @@ private void UpdateViewTypeItems()
}
}

void moonPdfPanel_FileLoaded(object sender, EventArgs e)
void moonPdfPanel_PdfLoaded(object sender, EventArgs e)
{
this.UpdateTitle();
}
Expand All @@ -147,12 +147,23 @@ private void UpdateTitle()
if( appName == null )
appName = ((AssemblyProductAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true).First()).Product;

this.Title = IsFileOpen() ? string.Format("{0} - {1}", System.IO.Path.GetFileName(this.moonPdfPanel.CurrentFilename), appName) : appName;
if (IsPdfLoaded())
{
var fs = moonPdfPanel.CurrentSource as FileSource;

if( fs != null )
{
this.Title = string.Format("{0} - {1}", System.IO.Path.GetFileName(fs.Filename), appName);
return;
}
}

this.Title = appName;
}

internal bool IsFileOpen()
internal bool IsPdfLoaded()
{
return !string.IsNullOrEmpty(moonPdfPanel.CurrentFilename);
return moonPdfPanel.CurrentSource != null;
}

protected override void OnPreviewKeyDown(KeyEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/MoonPdf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ You should have received a copy of the GNU General Public License
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.3.0")]
[assembly: AssemblyFileVersion("0.2.3.0")]
[assembly: AssemblyVersion("0.3.0.0")]
[assembly: AssemblyFileVersion("0.3.0.0")]
36 changes: 18 additions & 18 deletions src/MoonPdfLib/ContinuousMoonPdfPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ void ContinuousMoonPdfPanel_SizeChanged(object sender, SizeChangedEventArgs e)
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
}

public void Load(string pdfFilename, string password = null)
{
this.virtualPanel = VisualTreeHelperEx.FindChild<CustomVirtualizingPanel>(this);
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
this.imageProvider = new PdfImageProvider(pdfFilename, this.parent.TotalPages,
new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
public void Load(IPdfSource source, string password = null)
{
this.virtualPanel = VisualTreeHelperEx.FindChild<CustomVirtualizingPanel>(this);
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
password: password);

if (this.parent.ZoomType == ZoomType.Fixed)
this.CreateNewItemsSource();
else if (this.parent.ZoomType == ZoomType.FitToHeight)
this.ZoomToHeight();
else if (this.parent.ZoomType == ZoomType.FitToWidth)
this.ZoomToWidth();

if( this.scrollViewer != null )
this.scrollViewer.ScrollToTop();
}

if (this.parent.ZoomType == ZoomType.Fixed)
this.CreateNewItemsSource();
else if (this.parent.ZoomType == ZoomType.FitToHeight)
this.ZoomToHeight();
else if (this.parent.ZoomType == ZoomType.FitToWidth)
this.ZoomToWidth();

if (this.scrollViewer != null)
this.scrollViewer.ScrollToTop();
}
private void CreateNewItemsSource()
{
var pageTimeout = TimeSpan.FromSeconds(2);
Expand Down
3 changes: 2 additions & 1 deletion src/MoonPdfLib/IMoonPdfPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
!*/
using MoonPdfLib.MuPdf;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -31,7 +32,7 @@ internal interface IMoonPdfPanel
ScrollViewer ScrollViewer { get; }
UserControl Instance { get; }
float CurrentZoom { get; }
void Load(string pdfFilename, string password = null);
void Load(IPdfSource source, string password = null);
void Zoom(double zoomFactor);
void ZoomIn();
void ZoomOut();
Expand Down
54 changes: 29 additions & 25 deletions src/MoonPdfLib/MoonPdfPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace MoonPdfLib
{
public partial class MoonPdfPanel : UserControl
{
public event EventHandler FileLoaded;
public event EventHandler PdfLoaded;
public event EventHandler ZoomTypeChanged;
public event EventHandler ViewTypeChanged;
public event EventHandler PageRowDisplayChanged;
Expand Down Expand Up @@ -115,7 +115,7 @@ public PageRowDisplayType PageRowDisplay
#endregion

public double HorizontalMargin { get { return this.PageMargin.Right; } }
public string CurrentFilename { get; private set; }
public IPdfSource CurrentSource { get; private set; }
public string CurrentPassword { get; private set; }
public int TotalPages { get; private set; }
internal PageRowBound[] PageRowBounds { get { return this.pageRowBounds; } }
Expand Down Expand Up @@ -161,7 +161,7 @@ public MoonPdfPanel()

void PdfViewerPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.CurrentFilename == null)
if (this.CurrentSource == null)
return;

resizeTimer.Stop();
Expand All @@ -172,7 +172,7 @@ void resizeTimer_Tick(object sender, EventArgs e)
{
resizeTimer.Stop();

if (this.CurrentFilename == null)
if (this.CurrentSource == null)
return;

if (this.ZoomType == ZoomType.FitToWidth)
Expand All @@ -181,14 +181,19 @@ void resizeTimer_Tick(object sender, EventArgs e)
ZoomToHeight();
}

public void OpenFile(string pdfFilename, string password = null)
{
if (!File.Exists(pdfFilename))
throw new FileNotFoundException(string.Empty, pdfFilename);
public void OpenFile(string pdfFilename, string password = null)
{
if (!File.Exists(pdfFilename))
throw new FileNotFoundException(string.Empty, pdfFilename);

this.Open(new FileSource(pdfFilename), password);
}

public void Open(IPdfSource source, string password = null)
{
var pw = password;

if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(pdfFilename) && pw == null)
if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(source) && pw == null)
{
var e = new PasswordRequiredEventArgs();
this.PasswordRequired(this, e);
Expand All @@ -199,21 +204,21 @@ public void OpenFile(string pdfFilename, string password = null)
pw = e.Password;
}

this.LoadPdfFile(pdfFilename, pw);
this.CurrentFilename = pdfFilename;
this.LoadPdf(source, pw);
this.CurrentSource = source;
this.CurrentPassword = pw;

if (this.FileLoaded != null)
this.FileLoaded(this, EventArgs.Empty);
if (this.PdfLoaded != null)
this.PdfLoaded(this, EventArgs.Empty);
}

private void LoadPdfFile(string pdfFilename, string password)
{
var pageBounds = MuPdfWrapper.GetPageBounds(pdfFilename, this.Rotation, password);
private void LoadPdf(IPdfSource source, string password)
{
var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password);
this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType);
this.TotalPages = pageBounds.Length;
this.innerPanel.Load(pdfFilename, password);
}
this.TotalPages = pageBounds.Length;
this.innerPanel.Load(source, password);
}

private PageRowBound[] CalculatePageRowBounds(Size[] singlePageBounds, ViewType viewType)
{
Expand Down Expand Up @@ -343,7 +348,7 @@ public void RotateLeft()
public void Rotate(ImageRotation rotation)
{
var currentPage = this.innerPanel.GetCurrentPageIndex(this.ViewType) + 1;
this.LoadPdfFile(this.CurrentFilename, this.CurrentPassword);
this.LoadPdf(this.CurrentSource, this.CurrentPassword);
this.innerPanel.GotoPage(currentPage);
}

Expand Down Expand Up @@ -396,7 +401,7 @@ private void UpdateAndReload(Action updateAction, ViewType viewType)
var currentPage = -1;
var zoom = 1.0f;

if (this.CurrentFilename != null)
if (this.CurrentSource != null)
{
currentPage = this.innerPanel.GetCurrentPageIndex(viewType) + 1;
zoom = this.innerPanel.CurrentZoom;
Expand All @@ -408,7 +413,7 @@ private void UpdateAndReload(Action updateAction, ViewType viewType)
{
Action reloadAction = () =>
{
this.LoadPdfFile(this.CurrentFilename, this.CurrentPassword);
this.LoadPdf(this.CurrentSource, this.CurrentPassword);
this.innerPanel.Zoom(zoom);
this.innerPanel.GotoPage(currentPage);
};
Expand Down Expand Up @@ -440,7 +445,7 @@ protected override void OnDrop(DragEventArgs e)
{
string pw = null;

if (MuPdfWrapper.NeedsPassword(filename))
if (MuPdfWrapper.NeedsPassword(new FileSource(filename)))
{
if (this.PasswordRequired == null)
return;
Expand All @@ -451,7 +456,6 @@ protected override void OnDrop(DragEventArgs e)
if (args.Cancel)
return;

//this.OpenFile(filename, args.Password);
pw = args.Password;
}

Expand Down
Loading

0 comments on commit 873eb7a

Please sign in to comment.