mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-15 19:54:52 +08:00
#26 add missing operation and expose the content stream directly to the user through the page
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using PdfPig.Graphics;
|
||||
using PdfPig.Graphics.Core;
|
||||
using PdfPig.Graphics.Operations.General;
|
||||
@@ -11,6 +12,7 @@
|
||||
using PdfPig.Graphics.Operations.TextState;
|
||||
using PdfPig.Parser;
|
||||
using PdfPig.Tokens;
|
||||
using PdfPig.Util;
|
||||
using Xunit;
|
||||
|
||||
public class PageContentParserTests
|
||||
@@ -29,6 +31,64 @@
|
||||
Assert.NotEmpty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CorrectlyWritesOperations()
|
||||
{
|
||||
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Parser", "SimpleGoogleDocPageContent.txt");
|
||||
var content = File.ReadAllText(path);
|
||||
var input = StringBytesTestConverter.Convert(content, false);
|
||||
|
||||
var result = parser.Parse(input.Bytes);
|
||||
|
||||
var replacementRegex = new Regex(@"\s(\.\d+)\b");
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
foreach (var operation in result)
|
||||
{
|
||||
operation.Write(stream);
|
||||
}
|
||||
|
||||
var text = OtherEncodings.BytesAsLatin1String(stream.ToArray());
|
||||
|
||||
text = text.Replace("\n", " ");
|
||||
content = content.Replace("\r\n", " ");
|
||||
content = replacementRegex.Replace(content, " 0$1");
|
||||
|
||||
Assert.Equal(content, text);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CorrectlyWritesSmallTextContent()
|
||||
{
|
||||
const string s = @"BT
|
||||
/F13 48 Tf
|
||||
20 38 Td
|
||||
1 Tr
|
||||
2 w
|
||||
(ABC) Tj
|
||||
ET";
|
||||
var input = StringBytesTestConverter.Convert(s, false);
|
||||
|
||||
var result = parser.Parse(input.Bytes);
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
foreach (var operation in result)
|
||||
{
|
||||
operation.Write(stream);
|
||||
}
|
||||
|
||||
var text = OtherEncodings.BytesAsLatin1String(stream.ToArray());
|
||||
|
||||
text = text.Replace("\n", " ").Trim();
|
||||
var expected = s.Replace("\r\n", " ");
|
||||
|
||||
Assert.Equal(expected, text);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CorrectlyExtractsOptionsInTextContext()
|
||||
{
|
||||
|
@@ -92,6 +92,7 @@
|
||||
"UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState.ModifyCurrentTransformationMatrix",
|
||||
"UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState.Pop",
|
||||
"UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState.Push",
|
||||
"UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState.SetGraphicsStateParametersFromDictionary",
|
||||
"UglyToad.PdfPig.Graphics.Operations.StrokePath",
|
||||
"UglyToad.PdfPig.Graphics.Operations.TextObjects.BeginText",
|
||||
"UglyToad.PdfPig.Graphics.Operations.TextObjects.EndText",
|
||||
|
@@ -4,6 +4,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Annotations;
|
||||
using Graphics.Operations;
|
||||
using Tokens;
|
||||
using Util;
|
||||
using Util.JetBrains.Annotations;
|
||||
@@ -52,6 +53,11 @@
|
||||
/// </summary>
|
||||
public PageSize Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The parsed graphics state operations in the content stream for this page.
|
||||
/// </summary>
|
||||
public IReadOnlyList<IGraphicsStateOperation> Operations => Content.GraphicsStateOperations;
|
||||
|
||||
/// <summary>
|
||||
/// Access to members whose future locations within the API will change without warning.
|
||||
/// </summary>
|
||||
|
@@ -0,0 +1,54 @@
|
||||
namespace UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState
|
||||
{
|
||||
using System.IO;
|
||||
using Tokens;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Set the specified parameters in the graphics state using the ExtGState subdictionary with the given name.
|
||||
/// </summary>
|
||||
public class SetGraphicsStateParametersFromDictionary : IGraphicsStateOperation
|
||||
{
|
||||
/// <summary>
|
||||
/// The symbol for this operation in a stream.
|
||||
/// </summary>
|
||||
public const string Symbol = "gs";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Operator => Symbol;
|
||||
|
||||
/// <summary>
|
||||
/// The name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary.
|
||||
/// </summary>
|
||||
public NameToken Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="SetGraphicsStateParametersFromDictionary"/>.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the ExtGState dictionary.</param>
|
||||
public SetGraphicsStateParametersFromDictionary(NameToken name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Run(IOperationContext operationContext)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Write(Stream stream)
|
||||
{
|
||||
stream.WriteText($"/{Name.Data}");
|
||||
stream.WriteWhiteSpace();
|
||||
stream.WriteText(Symbol);
|
||||
stream.WriteNewLine();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Name} {Symbol}";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user