Skip to content

Commit

Permalink
render annotations nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
Modest-as committed Jun 17, 2022
1 parent 0980c34 commit a5c95f4
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 50 deletions.
Binary file added examples/nuget-usage/Assets/annotations_01.pdf
Binary file not shown.
Binary file added examples/nuget-usage/Assets/annotations_02.pdf
Binary file not shown.
48 changes: 27 additions & 21 deletions examples/nuget-usage/NugetUsageAnyCpu/NugetUsageAnyCpu.csproj
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Docnet.Core" Version="2.4.0-alpha.2" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Docnet.Core" Version="2.4.0-alpha.2" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="..\Assets\image_0.jpeg" Link="Assets\image_0.jpeg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\Assets\wikipedia_0.pdf" Link="Assets\wikipedia_0.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="..\Assets\image_0.jpeg" Link="Assets\image_0.jpeg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\Assets\wikipedia_0.pdf" Link="Assets\wikipedia_0.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\Assets\annotations_01.pdf" Link="Assets\annotations_01.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\Assets\annotations_02.pdf" Link="Assets\annotations_02.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NugetUsageAnyCpu
[Collection("Example collection")]
public class PdfToImageExamples
{
private const string Path = "Assets/wikipedia_0.pdf";
private const string Path = "Assets/annotations_01.pdf";

private readonly ExampleFixture _fixture;

Expand Down
6 changes: 4 additions & 2 deletions examples/nuget-usage/nuget-usage.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
# Visual Studio Version 17
VisualStudioVersion = 17.2.32516.85
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetUsageAnyCpu", "NugetUsageAnyCpu\NugetUsageAnyCpu.csproj", "{6D97BB9F-80B8-414D-8472-725EA638B580}"
EndProject
Expand All @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetUsageX86", "NugetUsage
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Assets", "Assets", "{F61EB8D0-A0C9-4AEA-9E28-DBCFFF7E50F9}"
ProjectSection(SolutionItems) = preProject
Assets\annotations_01.pdf = Assets\annotations_01.pdf
Assets\annotations_02.pdf = Assets\annotations_02.pdf
Assets\image_0.png = Assets\image_0.png
Assets\wikipedia_0.pdf = Assets\wikipedia_0.pdf
EndProjectSection
Expand Down
18 changes: 12 additions & 6 deletions src/Docnet.Core/Bindings/PdfiumWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using System.Text;
using Docnet.Core.Models;

#pragma warning disable CS0169

namespace Docnet.Core.Bindings
{
public enum FPDF_DUPLEXTYPE_
Expand Down Expand Up @@ -430,7 +432,7 @@ protected FpdfDestT(void* native, bool skipVTables = false)
}

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

Expand Down Expand Up @@ -2308,10 +2310,16 @@ public static FpdfBitmapT FPDFBitmapCreate(int width, int height, int alpha)
return __result0;
}

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

var __arg0 = ReferenceEquals(document, null) ? IntPtr.Zero : document.__Instance;

var __ret = __Internal.FPDFDOC_InitFormFillEnvironment(__arg0, formInfo);

return __ret;
}
Expand All @@ -2326,9 +2334,7 @@ public static void FPDFFFLDraw(IntPtr form_handle,
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,
Expand Down
45 changes: 26 additions & 19 deletions src/Docnet.Core/Readers/PageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,26 @@ namespace Docnet.Core.Readers
{
internal sealed class PageReader : IPageReader
{
private readonly DocumentWrapper _docWrapper;

private readonly FpdfPageT _page;
private readonly FpdfTextpageT _text;

private readonly double _scaling;

private IntPtr formHandle;

private FPDF_FORMFILLINFO formInfo = new FPDF_FORMFILLINFO();

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

public PageReader(DocumentWrapper docWrapper, int pageIndex, PageDimensions pageDimensions)
{
_docWrapper = docWrapper;

PageIndex = pageIndex;

lock (DocLib.Lock)
{
_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 @@ -228,12 +217,30 @@ public byte[] GetImage(RenderFlags flags)
clipping.Bottom = 0;
clipping.Top = height;

var formHandle = IntPtr.Zero;

if (flags.HasFlag(RenderFlags.RenderAnnotations))
{
for (var i = 1; i <= 2; i++)
{
formHandle = fpdf_view.FPDFDOCInitFormFillEnvironment(_docWrapper.Instance, i);

if (formHandle != IntPtr.Zero)
{
break;
}
}
}

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);
if (flags.HasFlag(RenderFlags.RenderAnnotations))
{
fpdf_view.FPDFFFLDraw(formHandle, bitmap, _page, 0, 0, width, height, PageRotate.Normal, flags);

fpdf_view.FPDF_ExitFormFillEnvironment(formHandle);
}

var buffer = fpdf_view.FPDFBitmapGetBuffer(bitmap);

Marshal.Copy(buffer, result, 0, result.Length);
Expand Down
Binary file not shown.
16 changes: 15 additions & 1 deletion src/Docnet.Tests.Integration/PageReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void Reader_WhenCalledFromDifferentThreads_ShouldBeAbleToHandle(Input typ
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 1, 595, 841)]
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 10, 5953, 8419)]
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 15, 8929, 12628)]
public void GetPageWidthOrHeight_WhenCalledWithScalingFactor_ShouldMach(Input type, string filePath, string password, double scaling, int expectedWidth, int expectedHeight)
public void GetPageWidthOrHeight_WhenCalledWithScalingFactor_ShouldMatch(Input type, string filePath, string password, double scaling, int expectedWidth, int expectedHeight)
{
ExecuteForDocument(type, filePath, password, scaling, 0, pageReader =>
{
Expand Down Expand Up @@ -322,6 +322,20 @@ public void GetImage_WhenCalledWithRenderAnnotationsAndGrayscaleFlags_ShouldRend
});
}

[Theory]
[InlineData("Docs/sample_form_fields_01.pdf")]
[InlineData("Docs/sample_form_fields_02.pdf")]
public void GetImage_WhenAnnotationsRendered_ShouldHaveDifferentBytes(string filePath)
{
ExecuteForDocument(Input.FromFile, filePath, null, 1, 0, pageReader =>
{
var bytesWithoutAnnotations = pageReader.GetImage().ToArray();
var bytesWithAnnotations = pageReader.GetImage(RenderFlags.RenderAnnotations).ToArray();

Assert.NotEqual(bytesWithAnnotations.Count(x => x != 0), bytesWithoutAnnotations.Count(x => x != 0));
});
}

private static int GetNonZeroByteCount(Input type, string filePath, LibFixture fixture)
{
using (var reader = fixture.GetDocReader(type, filePath, null, 1000, 1000))
Expand Down

0 comments on commit a5c95f4

Please sign in to comment.