Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose method in PdfPage and using(page) in Test.Console, and also PdfPage.HasTransparency #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Binary file modified Nuget/pdfium_x64.dll
Binary file not shown.
Binary file modified Nuget/pdfium_x86.dll
Binary file not shown.
5 changes: 4 additions & 1 deletion PDFiumSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDFiumSharp", "PDFiumSharp\PDFiumSharp.csproj", "{782D0C5A-2A13-480C-AF88-6DADC726566E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PDFiumSharp", "PDFiumSharp\PDFiumSharp.csproj", "{782D0C5A-2A13-480C-AF88-6DADC726566E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PDFiumSharp.GdiPlus", "PDFiumSharp.GdiPlus\PDFiumSharp.GdiPlus.csproj", "{2D52FEFF-2445-4446-A4A0-65DCEBD639AA}"
EndProject
Expand Down Expand Up @@ -80,4 +80,7 @@ Global
{C07538E4-238C-42AC-BC19-8AFA66BD6BC2} = {488D350A-7DF4-4E38-9398-409C89A2A9E6}
{AE8ED031-D549-449C-9273-5F0369BF7E56} = {488D350A-7DF4-4E38-9398-409C89A2A9E6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CF9ACF66-BB1D-4F64-9F8F-80249BC60195}
EndGlobalSection
EndGlobal
24 changes: 13 additions & 11 deletions PDFiumSharp/PDFium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ public static FPDF_DOCUMENT FPDF_LoadDocument(byte[] data, int index = 0, int co
return FPDF_LoadMemDocument(ref data[index], count, password);
}

/// <summary>
/// Loads a PDF document from '<paramref name="count"/>' bytes read from a stream.
/// </summary>
/// <param name="count">
/// The number of bytes to read from the <paramref name="stream"/>.
/// If the value is equal to or smaller than 0, the stream is read to the end.
/// </param>
public static FPDF_DOCUMENT FPDF_LoadDocument(Stream stream, int count = 0, string password = null)
{
return FPDF_LoadCustomDocument(FPDF_FILEREAD.FromStream(stream, count), password);
}
/// <summary>
/// Loads a PDF document from '<paramref name="count"/>' bytes read from a stream.
/// </summary>
/// <param name="fileRead"></param>
/// <param name="count">
/// The number of bytes to read from the <paramref name="stream"/>.
/// If the value is equal to or smaller than 0, the stream is read to the end.
/// </param>
/// <param name="stream"></param>
public static FPDF_DOCUMENT FPDF_LoadDocument(Stream stream, FPDF_FILEREAD fileRead, int count = 0, string password = null)
{
return FPDF_LoadCustomDocument(fileRead, password);
}

//public static string FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document, string key)
//{
Expand Down
4 changes: 2 additions & 2 deletions PDFiumSharp/PDFium.tt
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ foreach (var import in Imports)
if (returnType == "void")
{
// --------------------------------------------------------- Code End ----------------------------------------------------- #>
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
if (RuntimeInformation.ProcessArchitecture == Architecture.X64 || (RuntimeInformation.ProcessArchitecture == Architecture.X86 && IntPtr.Size == 8))
lock(_lock) { PlatformInvoke.<#=name#>_x64(<#=args#>); }
else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
lock(_lock) { PlatformInvoke.<#=name#>_x86(<#=args#>); }
Expand All @@ -818,7 +818,7 @@ foreach (var import in Imports)
else
{
// --------------------------------------------------------- Code End ----------------------------------------------------- #>
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
if (RuntimeInformation.ProcessArchitecture == Architecture.X64 || (RuntimeInformation.ProcessArchitecture == Architecture.X86 && IntPtr.Size == 8))
lock(_lock) { return PlatformInvoke.<#=name#>_x64(<#=args#>); }
else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
lock(_lock) { return PlatformInvoke.<#=name#>_x86(<#=args#>); }
Expand Down
25 changes: 14 additions & 11 deletions PDFiumSharp/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed class PdfDocument : NativeWrapper<FPDF_DOCUMENT>

public PdfDestinationCollection Destinations { get; }

/// <summary>
/// <summary>
/// Gets the PDF file version. File version: 14 for 1.4, 15 for 1.5, ...
/// </summary>
public int FileVersion { get { PDFium.FPDF_GetFileVersion(Handle, out int fileVersion); return fileVersion; } }
Expand Down Expand Up @@ -91,16 +91,19 @@ public PdfDocument(string fileName, string password = null)
public PdfDocument(byte[] data, int index = 0, int count = -1, string password = null)
: this(PDFium.FPDF_LoadDocument(data, index, count, password)) { }

/// <summary>
/// Loads a <see cref="PdfDocument"/> from '<paramref name="count"/>' bytes read from a <paramref name="stream"/>.
/// <see cref="Close"/> must be called in order to free unmanaged resources.
/// </summary>
/// <param name="count">
/// The number of bytes to read from the <paramref name="stream"/>.
/// If the value is equal to or smaller than 0, the stream is read to the end.
/// </param>
public PdfDocument(Stream stream, int count = 0, string password = null)
: this(PDFium.FPDF_LoadDocument(stream, count, password)) { }
/// <summary>
/// Loads a <see cref="PdfDocument"/> from '<paramref name="count"/>' bytes read from a <paramref name="stream"/>.
/// <see cref="Close"/> must be called in order to free unmanaged resources.
/// </summary>
/// <param name="stream"></param>
/// <param name="fileRead"></param>
/// <param name="count">
/// The number of bytes to read from the <paramref name="stream"/>.
/// If the value is equal to or smaller than 0, the stream is read to the end.
/// </param>
/// <param name="password"></param>
public PdfDocument(Stream stream, FPDF_FILEREAD fileRead, int count = 0, string password = null)
: this(PDFium.FPDF_LoadDocument(stream, fileRead, count, password)) { }

/// <summary>
/// Closes the <see cref="PdfDocument"/> and frees unmanaged resources.
Expand Down
17 changes: 12 additions & 5 deletions PDFiumSharp/PdfPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ public PageOrientations Orientation
set => PDFium.FPDFPage_SetRotation(Handle, value);
}

/// <summary>
/// Gets the zero-based index of the page in the <see cref="Document"/>
/// </summary>
public int Index { get; internal set; } = -1;
/// <summary>
/// Get the transparency of the page
/// </summary>
public bool HasTransparency => PDFium.FPDFPage_HasTransparency(Handle);

/// <summary>
/// Gets the zero-based index of the page in the <see cref="Document"/>
/// </summary>
public int Index { get; internal set; } = -1;

/// <summary>
/// Gets the <see cref="PdfDocument"/> which contains the page.
Expand Down Expand Up @@ -116,7 +121,9 @@ public void Render(PDFiumBitmap renderTarget, PageOrientations orientation = Pag

public FlattenResults Flatten(FlattenFlags flags) => PDFium.FPDFPage_Flatten(Handle, flags);

protected override void Dispose(FPDF_PAGE handle)
public void Dispose() => ((IDisposable)this).Dispose();

protected override void Dispose(FPDF_PAGE handle)
{
PDFium.FPDF_ClosePage(handle);
}
Expand Down
2 changes: 1 addition & 1 deletion PDFiumSharp/Types/FPDF_COLOR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct FPDF_COLOR
readonly uint _argb;

public byte A => _a;
public byte R => _b;
public byte R => _r;
public byte G => _g;
public byte B => _b;
public int ARGB => unchecked((int)_argb);
Expand Down
18 changes: 10 additions & 8 deletions Samples/Test.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ static void Main(string[] args)
{
int i = 0;
foreach (var page in doc.Pages)
{
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, true))
using (var stream = new FileStream($"{i++}.bmp", FileMode.Create))
{
page.Render(bitmap);
bitmap.Save(stream);
}
}
using (page)
{
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, true))
using (var stream = new FileStream($"{i++}.bmp", FileMode.Create))
{
page.Render(bitmap);
bitmap.Save(stream);
}
}

}
Console.ReadKey();
}
Expand Down