Skip to content

Commit a5c95f4

Browse files
committed
render annotations nicely
1 parent 0980c34 commit a5c95f4

File tree

9 files changed

+85
-50
lines changed

9 files changed

+85
-50
lines changed
557 KB
Binary file not shown.
132 KB
Binary file not shown.
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
66

7-
<ItemGroup>
8-
<PackageReference Include="Docnet.Core" Version="2.4.0-alpha.2" />
9-
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
11-
<PackageReference Include="xunit" Version="2.4.1" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13-
<PrivateAssets>all</PrivateAssets>
14-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15-
</PackageReference>
16-
</ItemGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Docnet.Core" Version="2.4.0-alpha.2" />
9+
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
11+
<PackageReference Include="xunit" Version="2.4.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
</ItemGroup>
1717

18-
<ItemGroup>
19-
<Content Include="..\Assets\image_0.jpeg" Link="Assets\image_0.jpeg">
20-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21-
</Content>
22-
<Content Include="..\Assets\wikipedia_0.pdf" Link="Assets\wikipedia_0.pdf">
23-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24-
</Content>
25-
</ItemGroup>
18+
<ItemGroup>
19+
<Content Include="..\Assets\image_0.jpeg" Link="Assets\image_0.jpeg">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</Content>
22+
<Content Include="..\Assets\wikipedia_0.pdf" Link="Assets\wikipedia_0.pdf">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
25+
<Content Include="..\Assets\annotations_01.pdf" Link="Assets\annotations_01.pdf">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</Content>
28+
<Content Include="..\Assets\annotations_02.pdf" Link="Assets\annotations_02.pdf">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</Content>
31+
</ItemGroup>
2632

2733
</Project>

examples/nuget-usage/NugetUsageAnyCpu/PdfToImageExamples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace NugetUsageAnyCpu
1212
[Collection("Example collection")]
1313
public class PdfToImageExamples
1414
{
15-
private const string Path = "Assets/wikipedia_0.pdf";
15+
private const string Path = "Assets/annotations_01.pdf";
1616

1717
private readonly ExampleFixture _fixture;
1818

examples/nuget-usage/nuget-usage.sln

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30320.27
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32516.85
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetUsageAnyCpu", "NugetUsageAnyCpu\NugetUsageAnyCpu.csproj", "{6D97BB9F-80B8-414D-8472-725EA638B580}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetUsageX86", "NugetUsage
1111
EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Assets", "Assets", "{F61EB8D0-A0C9-4AEA-9E28-DBCFFF7E50F9}"
1313
ProjectSection(SolutionItems) = preProject
14+
Assets\annotations_01.pdf = Assets\annotations_01.pdf
15+
Assets\annotations_02.pdf = Assets\annotations_02.pdf
1416
Assets\image_0.png = Assets\image_0.png
1517
Assets\wikipedia_0.pdf = Assets\wikipedia_0.pdf
1618
EndProjectSection

src/Docnet.Core/Bindings/PdfiumWrapper.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
using System.Text;
2525
using Docnet.Core.Models;
2626

27+
#pragma warning disable CS0169
28+
2729
namespace Docnet.Core.Bindings
2830
{
2931
public enum FPDF_DUPLEXTYPE_
@@ -430,7 +432,7 @@ protected FpdfDestT(void* native, bool skipVTables = false)
430432
}
431433

432434
[StructLayout(LayoutKind.Sequential)]
433-
public class FPDF_FORMFILLINFO
435+
internal class FPDF_FORMFILLINFO
434436
{
435437
public int version;
436438

@@ -2308,10 +2310,16 @@ public static FpdfBitmapT FPDFBitmapCreate(int width, int height, int alpha)
23082310
return __result0;
23092311
}
23102312

2311-
public static IntPtr FPDFDOCInitFormFillEnvironment(IntPtr bitmap, FPDF_FORMFILLINFO formInfo)
2313+
public static IntPtr FPDFDOCInitFormFillEnvironment(FpdfDocumentT document, int version)
23122314
{
2313-
//var __arg0 = ReferenceEquals(bitmap, null) ? IntPtr.Zero : bitmap.__Instance;
2314-
var __ret = __Internal.FPDFDOC_InitFormFillEnvironment(bitmap, formInfo);
2315+
var formInfo = new FPDF_FORMFILLINFO
2316+
{
2317+
version = version
2318+
};
2319+
2320+
var __arg0 = ReferenceEquals(document, null) ? IntPtr.Zero : document.__Instance;
2321+
2322+
var __ret = __Internal.FPDFDOC_InitFormFillEnvironment(__arg0, formInfo);
23152323

23162324
return __ret;
23172325
}
@@ -2326,9 +2334,7 @@ public static void FPDFFFLDraw(IntPtr form_handle,
23262334
PageRotate rotate,
23272335
RenderFlags flags)
23282336
{
2329-
23302337
__Internal.FPDFFFLDraw(form_handle, bitmap.__Instance, page.__Instance, start_x, start_y, size_x, size_y, rotate, flags);
2331-
23322338
}
23332339

23342340
public static FpdfBitmapT FPDFBitmapCreateEx(int width, int height, int format,

src/Docnet.Core/Readers/PageReader.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,26 @@ namespace Docnet.Core.Readers
1010
{
1111
internal sealed class PageReader : IPageReader
1212
{
13+
private readonly DocumentWrapper _docWrapper;
14+
1315
private readonly FpdfPageT _page;
1416
private readonly FpdfTextpageT _text;
1517

1618
private readonly double _scaling;
1719

18-
private IntPtr formHandle;
19-
20-
private FPDF_FORMFILLINFO formInfo = new FPDF_FORMFILLINFO();
21-
2220
/// <inheritdoc />
2321
public int PageIndex { get; }
2422

2523
public PageReader(DocumentWrapper docWrapper, int pageIndex, PageDimensions pageDimensions)
2624
{
25+
_docWrapper = docWrapper;
26+
2727
PageIndex = pageIndex;
2828

2929
lock (DocLib.Lock)
3030
{
3131
_page = fpdf_view.FPDF_LoadPage(docWrapper.Instance, pageIndex);
3232

33-
for (int i = 1; i <= 2; i++)
34-
{
35-
formInfo.version = i;
36-
37-
formHandle = fpdf_view.FPDFDOCInitFormFillEnvironment(docWrapper.Instance.__Instance, formInfo);
38-
if (formHandle != IntPtr.Zero)
39-
{
40-
break;
41-
}
42-
}
43-
4433
if (_page == null)
4534
{
4635
throw new DocnetException($"failed to open page for page index {pageIndex}");
@@ -228,12 +217,30 @@ public byte[] GetImage(RenderFlags flags)
228217
clipping.Bottom = 0;
229218
clipping.Top = height;
230219

220+
var formHandle = IntPtr.Zero;
221+
222+
if (flags.HasFlag(RenderFlags.RenderAnnotations))
223+
{
224+
for (var i = 1; i <= 2; i++)
225+
{
226+
formHandle = fpdf_view.FPDFDOCInitFormFillEnvironment(_docWrapper.Instance, i);
227+
228+
if (formHandle != IntPtr.Zero)
229+
{
230+
break;
231+
}
232+
}
233+
}
234+
231235
fpdf_view.FPDF_RenderPageBitmapWithMatrix(bitmap, _page, matrix, clipping, (int)flags);
232236

233-
// var form_handle = fpdf_view.FPDFDOCInitFormFillEnvironment(bitmap, formInfo);
234-
PageRotate rotate = PageRotate.Normal;
235-
fpdf_view.FPDFFFLDraw(formHandle, bitmap, _page, 0, 0, width, height, rotate, flags);
236-
fpdf_view.FPDF_ExitFormFillEnvironment(formHandle);
237+
if (flags.HasFlag(RenderFlags.RenderAnnotations))
238+
{
239+
fpdf_view.FPDFFFLDraw(formHandle, bitmap, _page, 0, 0, width, height, PageRotate.Normal, flags);
240+
241+
fpdf_view.FPDF_ExitFormFillEnvironment(formHandle);
242+
}
243+
237244
var buffer = fpdf_view.FPDFBitmapGetBuffer(bitmap);
238245

239246
Marshal.Copy(buffer, result, 0, result.Length);
557 KB
Binary file not shown.

src/Docnet.Tests.Integration/PageReaderTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void Reader_WhenCalledFromDifferentThreads_ShouldBeAbleToHandle(Input typ
268268
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 1, 595, 841)]
269269
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 10, 5953, 8419)]
270270
[InlineData(Input.FromFile, "Docs/simple_0.pdf", null, 15, 8929, 12628)]
271-
public void GetPageWidthOrHeight_WhenCalledWithScalingFactor_ShouldMach(Input type, string filePath, string password, double scaling, int expectedWidth, int expectedHeight)
271+
public void GetPageWidthOrHeight_WhenCalledWithScalingFactor_ShouldMatch(Input type, string filePath, string password, double scaling, int expectedWidth, int expectedHeight)
272272
{
273273
ExecuteForDocument(type, filePath, password, scaling, 0, pageReader =>
274274
{
@@ -322,6 +322,20 @@ public void GetImage_WhenCalledWithRenderAnnotationsAndGrayscaleFlags_ShouldRend
322322
});
323323
}
324324

325+
[Theory]
326+
[InlineData("Docs/sample_form_fields_01.pdf")]
327+
[InlineData("Docs/sample_form_fields_02.pdf")]
328+
public void GetImage_WhenAnnotationsRendered_ShouldHaveDifferentBytes(string filePath)
329+
{
330+
ExecuteForDocument(Input.FromFile, filePath, null, 1, 0, pageReader =>
331+
{
332+
var bytesWithoutAnnotations = pageReader.GetImage().ToArray();
333+
var bytesWithAnnotations = pageReader.GetImage(RenderFlags.RenderAnnotations).ToArray();
334+
335+
Assert.NotEqual(bytesWithAnnotations.Count(x => x != 0), bytesWithoutAnnotations.Count(x => x != 0));
336+
});
337+
}
338+
325339
private static int GetNonZeroByteCount(Input type, string filePath, LibFixture fixture)
326340
{
327341
using (var reader = fixture.GetDocReader(type, filePath, null, 1000, 1000))

0 commit comments

Comments
 (0)