Skip to content

Commit

Permalink
Implement filled form drawing (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Charles Childress <[email protected]>
  • Loading branch information
charch1219 and Charles Childress authored Jun 17, 2022
1 parent 6842edb commit 0980c34
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
130 changes: 130 additions & 0 deletions src/Docnet.Core/Bindings/PdfiumWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Docnet.Core.Models;

namespace Docnet.Core.Bindings
{
Expand All @@ -33,6 +34,14 @@ public enum FPDF_DUPLEXTYPE_
DuplexFlipLongEdge = 3
}

public enum PageRotate
{
Normal = 0,
Rotate90 = 1,
Rotate180 = 2,
Rotate270 = 3
}

internal unsafe partial class FpdfActionT
{
[StructLayout(LayoutKind.Explicit, Size = 0)]
Expand Down Expand Up @@ -420,6 +429,76 @@ protected FpdfDestT(void* native, bool skipVTables = false)
}
}

[StructLayout(LayoutKind.Sequential)]
public class FPDF_FORMFILLINFO
{
public int version;

private IntPtr Release;

private IntPtr FFI_Invalidate;

private IntPtr FFI_OutputSelectedRect;

private IntPtr FFI_SetCursor;

private IntPtr FFI_SetTimer;

private IntPtr FFI_KillTimer;

private IntPtr FFI_GetLocalTime;

private IntPtr FFI_OnChange;

private IntPtr FFI_GetPage;

private IntPtr FFI_GetCurrentPage;

private IntPtr FFI_GetRotation;

private IntPtr FFI_ExecuteNamedAction;

private IntPtr FFI_SetTextFieldFocus;

private IntPtr FFI_DoURIAction;

private IntPtr FFI_DoGoToAction;

private IntPtr m_pJsPlatform;

// XFA support i.e. version 2

private IntPtr FFI_DisplayCaret;

private IntPtr FFI_GetCurrentPageIndex;

private IntPtr FFI_SetCurrentPage;

private IntPtr FFI_GotoURL;

private IntPtr FFI_GetPageViewRect;

private IntPtr FFI_PageEvent;

private IntPtr FFI_PopupMenu;

private IntPtr FFI_OpenFile;

private IntPtr FFI_EmailTo;

private IntPtr FFI_UploadTo;

private IntPtr FFI_GetPlatform;

private IntPtr FFI_GetLanguage;

private IntPtr FFI_DownloadFromURL;

private IntPtr FFI_PostRequestURL;

private IntPtr FFI_PutRequestURL;
}

internal unsafe partial class FpdfDocumentT
{
[StructLayout(LayoutKind.Explicit, Size = 0)]
Expand Down Expand Up @@ -1748,6 +1827,16 @@ public partial struct __Internal
internal static extern IntPtr FPDF_LoadDocument(
IntPtr file_path, [MarshalAs(UnmanagedType.LPStr)] string password);

[SuppressUnmanagedCodeSecurity]
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "FPDFDOC_InitFormFillEnvironment")]
internal static extern IntPtr FPDFDOC_InitFormFillEnvironment(IntPtr document, FPDF_FORMFILLINFO formInfo);

[SuppressUnmanagedCodeSecurity]
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "FPDFDOC_ExitFormFillEnvironment")]
internal static extern void FPDFDOC_ExitFormFillEnvironment(IntPtr form_handle);

[SuppressUnmanagedCodeSecurity]
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "FPDF_LoadMemDocument")]
Expand Down Expand Up @@ -1850,6 +1939,19 @@ internal static extern int FPDF_PageToDevice(IntPtr page, int start_x, int start
EntryPoint = "FPDFBitmap_Create")]
internal static extern IntPtr FPDFBitmapCreate(int width, int height, int alpha);

[SuppressUnmanagedCodeSecurity]
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "FPDF_FFLDraw")]
internal static extern void FPDFFFLDraw(IntPtr form_handle,
IntPtr bitmap,
IntPtr page,
int start_x,
int start_y,
int size_x,
int size_y,
PageRotate rotate,
RenderFlags flags);

[SuppressUnmanagedCodeSecurity]
[DllImport("pdfium", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "FPDFBitmap_CreateEx")]
Expand Down Expand Up @@ -2049,6 +2151,11 @@ public static uint FPDF_GetLastError()
return __ret;
}

public static void FPDF_ExitFormFillEnvironment(IntPtr form_handle)
{
__Internal.FPDFDOC_ExitFormFillEnvironment(form_handle);
}

public static uint FPDF_GetDocPermissions(FpdfDocumentT document)
{
var __arg0 = ReferenceEquals(document, null) ? IntPtr.Zero : document.__Instance;
Expand Down Expand Up @@ -2201,6 +2308,29 @@ public static FpdfBitmapT FPDFBitmapCreate(int width, int height, int alpha)
return __result0;
}

public static IntPtr FPDFDOCInitFormFillEnvironment(IntPtr bitmap, FPDF_FORMFILLINFO formInfo)
{
//var __arg0 = ReferenceEquals(bitmap, null) ? IntPtr.Zero : bitmap.__Instance;
var __ret = __Internal.FPDFDOC_InitFormFillEnvironment(bitmap, formInfo);

return __ret;
}

public static void FPDFFFLDraw(IntPtr form_handle,
FpdfBitmapT bitmap,
FpdfPageT page,
int start_x,
int start_y,
int size_x,
int size_y,
PageRotate rotate,
RenderFlags flags)
{

__Internal.FPDFFFLDraw(form_handle, bitmap.__Instance, page.__Instance, start_x, start_y, size_x, size_y, rotate, flags);

}

public static FpdfBitmapT FPDFBitmapCreateEx(int width, int height, int format,
IntPtr first_scan, int stride)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Docnet.Core/Readers/PageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ internal sealed class PageReader : IPageReader

private readonly double _scaling;

private IntPtr formHandle;

private FPDF_FORMFILLINFO formInfo = new FPDF_FORMFILLINFO();

/// <inheritdoc />
public int PageIndex { get; }

Expand All @@ -26,6 +30,17 @@ public PageReader(DocumentWrapper docWrapper, int pageIndex, PageDimensions page
{
_page = fpdf_view.FPDF_LoadPage(docWrapper.Instance, pageIndex);

for (int i = 1; i <= 2; i++)
{
formInfo.version = i;

formHandle = fpdf_view.FPDFDOCInitFormFillEnvironment(docWrapper.Instance.__Instance, formInfo);
if (formHandle != IntPtr.Zero)
{
break;
}
}

if (_page == null)
{
throw new DocnetException($"failed to open page for page index {pageIndex}");
Expand Down Expand Up @@ -215,6 +230,10 @@ public byte[] GetImage(RenderFlags flags)

fpdf_view.FPDF_RenderPageBitmapWithMatrix(bitmap, _page, matrix, clipping, (int)flags);

// var form_handle = fpdf_view.FPDFDOCInitFormFillEnvironment(bitmap, formInfo);
PageRotate rotate = PageRotate.Normal;
fpdf_view.FPDFFFLDraw(formHandle, bitmap, _page, 0, 0, width, height, rotate, flags);
fpdf_view.FPDF_ExitFormFillEnvironment(formHandle);
var buffer = fpdf_view.FPDFBitmapGetBuffer(bitmap);

Marshal.Copy(buffer, result, 0, result.Length);
Expand Down

0 comments on commit 0980c34

Please sign in to comment.