Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 21 additions & 36 deletions src/Docnet.Core/Editors/DocEditor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using Docnet.Core.Bindings;
using Docnet.Core.Exceptions;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Docnet.Core.Editors
{
Expand All @@ -15,7 +15,8 @@ public byte[] Merge(string fileOne, string fileTwo)
using (var docOneWrapper = new DocumentWrapper(fileOne, null))
using (var docTwoWrapper = new DocumentWrapper(fileTwo, null))
{
return Merge(docOneWrapper, docTwoWrapper);
DocumentWrapper[] docWrappers = { docOneWrapper, docTwoWrapper };
return Merge(docWrappers);
}
}
}
Expand All @@ -27,7 +28,8 @@ public byte[] Merge(byte[] fileOne, byte[] fileTwo)
using (var docOneWrapper = new DocumentWrapper(fileOne, null))
using (var docTwoWrapper = new DocumentWrapper(fileTwo, null))
{
return Merge(docOneWrapper, docTwoWrapper);
DocumentWrapper[] docWrappers = { docOneWrapper, docTwoWrapper };
return Merge(docWrappers);
}
}
}
Expand All @@ -36,17 +38,21 @@ public byte[] Merge(IReadOnlyList<byte[]> files)
{
lock (DocLib.Lock)
{
var documentWrappers = OpenDocuments(files);

try
{
return Merge(documentWrappers);
}
finally
using (var newWrapper = new DocumentWrapper(fpdf_edit.FPDF_CreateNewDocument()))
{
foreach (DocumentWrapper documentWrapper in documentWrappers)
var documentWrappers = OpenDocuments(files);
documentWrappers = documentWrappers.Prepend(newWrapper).ToArray();

try
{
documentWrapper.Dispose();
return Merge(documentWrappers);
}
finally
{
foreach (DocumentWrapper documentWrapper in documentWrappers)
{
documentWrapper.Dispose();
}
}
}
}
Expand Down Expand Up @@ -113,27 +119,6 @@ private static byte[] Merge(IList<DocumentWrapper> docWrappers)
}
}

private static byte[] Merge(DocumentWrapper docOneWrapper, DocumentWrapper docTwoWrapper)
{
using (var stream = new MemoryStream())
{
var pageCountOne = fpdf_view.FPDF_GetPageCount(docOneWrapper.Instance);

var success = fpdf_ppo.FPDF_ImportPages(
docOneWrapper.Instance,
docTwoWrapper.Instance,
null,
pageCountOne) == 1;

if (!success)
{
throw new DocnetException("failed to merge files");
}

return GetBytes(stream, docOneWrapper);
}
}

public byte[] Split(string filePath, int pageFromIndex, int pageToIndex)
{
lock (DocLib.Lock)
Expand Down Expand Up @@ -276,4 +261,4 @@ private static byte[] GetBytes(MemoryStream stream, DocumentWrapper docWrapper)
return stream.ToArray();
}
}
}
}