Skip to content

Commit 873eb7a

Browse files
committed
Added the ability to load PDF documents from memory (byte[])
1 parent 43f9413 commit 873eb7a

11 files changed

+147
-92
lines changed

Diff for: src/MoonPdf/Commands.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class Commands
5656
public Commands(MainWindow wnd)
5757
{
5858
var pdfPanel = wnd.MoonPdfPanel;
59-
Predicate<object> isPdfLoaded = f => wnd.IsFileOpen(); // used for the CanExecute callback
59+
Predicate<object> isPdfLoaded = f => wnd.IsPdfLoaded(); // used for the CanExecute callback
6060

6161
this.OpenCommand = new DelegateCommand("Open...", f =>
6262
{

Diff for: src/MoonPdf/FullscreenCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void wnd_PreviewKeyDown(object sender, KeyEventArgs e)
5050

5151
public override bool CanExecute(object parameter)
5252
{
53-
return wnd.IsFileOpen();
53+
return wnd.IsPdfLoaded();
5454
}
5555

5656
public override void Execute(object parameter)

Diff for: src/MoonPdf/MainWindow.xaml.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public MainWindow()
5454
moonPdfPanel.ViewTypeChanged += moonPdfPanel_ViewTypeChanged;
5555
moonPdfPanel.ZoomTypeChanged += moonPdfPanel_ZoomTypeChanged;
5656
moonPdfPanel.PageRowDisplayChanged += moonPdfPanel_PageDisplayChanged;
57-
moonPdfPanel.FileLoaded += moonPdfPanel_FileLoaded;
57+
moonPdfPanel.PdfLoaded += moonPdfPanel_PdfLoaded;
5858
moonPdfPanel.PasswordRequired += moonPdfPanel_PasswordRequired;
5959

6060
this.UpdatePageDisplayMenuItem();
@@ -137,7 +137,7 @@ private void UpdateViewTypeItems()
137137
}
138138
}
139139

140-
void moonPdfPanel_FileLoaded(object sender, EventArgs e)
140+
void moonPdfPanel_PdfLoaded(object sender, EventArgs e)
141141
{
142142
this.UpdateTitle();
143143
}
@@ -147,12 +147,23 @@ private void UpdateTitle()
147147
if( appName == null )
148148
appName = ((AssemblyProductAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true).First()).Product;
149149

150-
this.Title = IsFileOpen() ? string.Format("{0} - {1}", System.IO.Path.GetFileName(this.moonPdfPanel.CurrentFilename), appName) : appName;
150+
if (IsPdfLoaded())
151+
{
152+
var fs = moonPdfPanel.CurrentSource as FileSource;
153+
154+
if( fs != null )
155+
{
156+
this.Title = string.Format("{0} - {1}", System.IO.Path.GetFileName(fs.Filename), appName);
157+
return;
158+
}
159+
}
160+
161+
this.Title = appName;
151162
}
152163

153-
internal bool IsFileOpen()
164+
internal bool IsPdfLoaded()
154165
{
155-
return !string.IsNullOrEmpty(moonPdfPanel.CurrentFilename);
166+
return moonPdfPanel.CurrentSource != null;
156167
}
157168

158169
protected override void OnPreviewKeyDown(KeyEventArgs e)

Diff for: src/MoonPdf/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ You should have received a copy of the GNU General Public License
6464
// You can specify all the values or you can default the Build and Revision Numbers
6565
// by using the '*' as shown below:
6666
// [assembly: AssemblyVersion("1.0.*")]
67-
[assembly: AssemblyVersion("0.2.3.0")]
68-
[assembly: AssemblyFileVersion("0.2.3.0")]
67+
[assembly: AssemblyVersion("0.3.0.0")]
68+
[assembly: AssemblyFileVersion("0.3.0.0")]

Diff for: src/MoonPdfLib/ContinuousMoonPdfPanel.xaml.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ void ContinuousMoonPdfPanel_SizeChanged(object sender, SizeChangedEventArgs e)
5858
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
5959
}
6060

61-
public void Load(string pdfFilename, string password = null)
62-
{
63-
this.virtualPanel = VisualTreeHelperEx.FindChild<CustomVirtualizingPanel>(this);
64-
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
65-
this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
66-
this.imageProvider = new PdfImageProvider(pdfFilename, this.parent.TotalPages,
67-
new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
61+
public void Load(IPdfSource source, string password = null)
62+
{
63+
this.virtualPanel = VisualTreeHelperEx.FindChild<CustomVirtualizingPanel>(this);
64+
this.scrollViewer = VisualTreeHelperEx.FindChild<ScrollViewer>(this);
65+
this.virtualPanel.PageRowBounds = this.parent.PageRowBounds.Select(f => f.SizeIncludingOffset).ToArray();
66+
this.imageProvider = new PdfImageProvider(source, this.parent.TotalPages,
67+
new PageDisplaySettings(this.parent.GetPagesPerRow(), this.parent.ViewType, this.parent.HorizontalMargin, this.parent.Rotation),
6868
password: password);
6969

70-
if (this.parent.ZoomType == ZoomType.Fixed)
71-
this.CreateNewItemsSource();
72-
else if (this.parent.ZoomType == ZoomType.FitToHeight)
73-
this.ZoomToHeight();
74-
else if (this.parent.ZoomType == ZoomType.FitToWidth)
75-
this.ZoomToWidth();
76-
77-
if( this.scrollViewer != null )
78-
this.scrollViewer.ScrollToTop();
79-
}
80-
70+
if (this.parent.ZoomType == ZoomType.Fixed)
71+
this.CreateNewItemsSource();
72+
else if (this.parent.ZoomType == ZoomType.FitToHeight)
73+
this.ZoomToHeight();
74+
else if (this.parent.ZoomType == ZoomType.FitToWidth)
75+
this.ZoomToWidth();
76+
77+
if (this.scrollViewer != null)
78+
this.scrollViewer.ScrollToTop();
79+
}
80+
8181
private void CreateNewItemsSource()
8282
{
8383
var pageTimeout = TimeSpan.FromSeconds(2);

Diff for: src/MoonPdfLib/IMoonPdfPanel.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
You should have received a copy of the GNU General Public License
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
!*/
17+
using MoonPdfLib.MuPdf;
1718
using System;
1819
using System.Collections.Generic;
1920
using System.Linq;
@@ -31,7 +32,7 @@ internal interface IMoonPdfPanel
3132
ScrollViewer ScrollViewer { get; }
3233
UserControl Instance { get; }
3334
float CurrentZoom { get; }
34-
void Load(string pdfFilename, string password = null);
35+
void Load(IPdfSource source, string password = null);
3536
void Zoom(double zoomFactor);
3637
void ZoomIn();
3738
void ZoomOut();

Diff for: src/MoonPdfLib/MoonPdfPanel.xaml.cs

+29-25
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace MoonPdfLib
3737
{
3838
public partial class MoonPdfPanel : UserControl
3939
{
40-
public event EventHandler FileLoaded;
40+
public event EventHandler PdfLoaded;
4141
public event EventHandler ZoomTypeChanged;
4242
public event EventHandler ViewTypeChanged;
4343
public event EventHandler PageRowDisplayChanged;
@@ -115,7 +115,7 @@ public PageRowDisplayType PageRowDisplay
115115
#endregion
116116

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

162162
void PdfViewerPanel_SizeChanged(object sender, SizeChangedEventArgs e)
163163
{
164-
if (this.CurrentFilename == null)
164+
if (this.CurrentSource == null)
165165
return;
166166

167167
resizeTimer.Stop();
@@ -172,7 +172,7 @@ void resizeTimer_Tick(object sender, EventArgs e)
172172
{
173173
resizeTimer.Stop();
174174

175-
if (this.CurrentFilename == null)
175+
if (this.CurrentSource == null)
176176
return;
177177

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

184-
public void OpenFile(string pdfFilename, string password = null)
185-
{
186-
if (!File.Exists(pdfFilename))
187-
throw new FileNotFoundException(string.Empty, pdfFilename);
184+
public void OpenFile(string pdfFilename, string password = null)
185+
{
186+
if (!File.Exists(pdfFilename))
187+
throw new FileNotFoundException(string.Empty, pdfFilename);
188+
189+
this.Open(new FileSource(pdfFilename), password);
190+
}
188191

192+
public void Open(IPdfSource source, string password = null)
193+
{
189194
var pw = password;
190195

191-
if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(pdfFilename) && pw == null)
196+
if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(source) && pw == null)
192197
{
193198
var e = new PasswordRequiredEventArgs();
194199
this.PasswordRequired(this, e);
@@ -199,21 +204,21 @@ public void OpenFile(string pdfFilename, string password = null)
199204
pw = e.Password;
200205
}
201206

202-
this.LoadPdfFile(pdfFilename, pw);
203-
this.CurrentFilename = pdfFilename;
207+
this.LoadPdf(source, pw);
208+
this.CurrentSource = source;
204209
this.CurrentPassword = pw;
205210

206-
if (this.FileLoaded != null)
207-
this.FileLoaded(this, EventArgs.Empty);
211+
if (this.PdfLoaded != null)
212+
this.PdfLoaded(this, EventArgs.Empty);
208213
}
209-
210-
private void LoadPdfFile(string pdfFilename, string password)
211-
{
212-
var pageBounds = MuPdfWrapper.GetPageBounds(pdfFilename, this.Rotation, password);
214+
215+
private void LoadPdf(IPdfSource source, string password)
216+
{
217+
var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password);
213218
this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType);
214-
this.TotalPages = pageBounds.Length;
215-
this.innerPanel.Load(pdfFilename, password);
216-
}
219+
this.TotalPages = pageBounds.Length;
220+
this.innerPanel.Load(source, password);
221+
}
217222

218223
private PageRowBound[] CalculatePageRowBounds(Size[] singlePageBounds, ViewType viewType)
219224
{
@@ -343,7 +348,7 @@ public void RotateLeft()
343348
public void Rotate(ImageRotation rotation)
344349
{
345350
var currentPage = this.innerPanel.GetCurrentPageIndex(this.ViewType) + 1;
346-
this.LoadPdfFile(this.CurrentFilename, this.CurrentPassword);
351+
this.LoadPdf(this.CurrentSource, this.CurrentPassword);
347352
this.innerPanel.GotoPage(currentPage);
348353
}
349354

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

399-
if (this.CurrentFilename != null)
404+
if (this.CurrentSource != null)
400405
{
401406
currentPage = this.innerPanel.GetCurrentPageIndex(viewType) + 1;
402407
zoom = this.innerPanel.CurrentZoom;
@@ -408,7 +413,7 @@ private void UpdateAndReload(Action updateAction, ViewType viewType)
408413
{
409414
Action reloadAction = () =>
410415
{
411-
this.LoadPdfFile(this.CurrentFilename, this.CurrentPassword);
416+
this.LoadPdf(this.CurrentSource, this.CurrentPassword);
412417
this.innerPanel.Zoom(zoom);
413418
this.innerPanel.GotoPage(currentPage);
414419
};
@@ -440,7 +445,7 @@ protected override void OnDrop(DragEventArgs e)
440445
{
441446
string pw = null;
442447

443-
if (MuPdfWrapper.NeedsPassword(filename))
448+
if (MuPdfWrapper.NeedsPassword(new FileSource(filename)))
444449
{
445450
if (this.PasswordRequired == null)
446451
return;
@@ -451,7 +456,6 @@ protected override void OnDrop(DragEventArgs e)
451456
if (args.Cancel)
452457
return;
453458

454-
//this.OpenFile(filename, args.Password);
455459
pw = args.Password;
456460
}
457461

0 commit comments

Comments
 (0)