Fix 540 Copy page with inline image.

This commit is contained in:
Fred Natzke 2023-01-16 14:27:04 +10:00
parent 65bc754f5b
commit 324de1da67
4 changed files with 63 additions and 3 deletions

View File

@ -11,6 +11,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using Tests.Fonts.TrueType; using Tests.Fonts.TrueType;
using Xunit; using Xunit;
using System;
using UglyToad.PdfPig.Graphics.Operations.InlineImages;
public class PdfDocumentBuilderTests public class PdfDocumentBuilderTests
{ {
@ -167,6 +169,9 @@
return result; return result;
} }
[Fact] [Fact]
public void CanWriteSinglePageStandard14FontHelloWorld() public void CanWriteSinglePageStandard14FontHelloWorld()
{ {
@ -1138,6 +1143,45 @@
Assert.True(tw.WroteCrossReferenceTable); Assert.True(tw.WroteCrossReferenceTable);
} }
[Fact]
public void CanCopyInLineImage()
{
var docPath = IntegrationHelpers.GetDocumentPath("ssm2163.pdf");
using (var docOrig = PdfDocument.Open(docPath))
{
// Copy original document with inline images into pdf bytes for opening and checking.
PdfDocumentBuilder pdfBuilder = new PdfDocumentBuilder();
var numberOfPages = docOrig.NumberOfPages;
for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
{
var sourcePage = docOrig.GetPage(pageNumber);
pdfBuilder.AddPage(sourcePage.Width, sourcePage.Height).CopyFrom(sourcePage);
}
var pdfBytes = pdfBuilder.Build();
using (var docCopy = PdfDocument.Open(pdfBytes))
{
var pageNum = 7;
var origPage = docOrig.GetPage(pageNum);
var copyPage = docCopy.GetPage(pageNum);
var opsOrig = origPage.Operations.Where(v => v.Operator == BeginInlineImageData.Symbol).Select(v => (BeginInlineImageData)v).ToArray();
var opCopy = copyPage.Operations.Where(v => v.Operator == BeginInlineImageData.Symbol).Select(v => (BeginInlineImageData)v).ToArray();
var dictOrig = opCopy.Select(v => v.Dictionary).ToArray();
var dictCopy = opCopy.Select(v => v.Dictionary).ToArray();
var exampleCopiedDictionary = dictCopy.FirstOrDefault();
Assert.NotNull(exampleCopiedDictionary);
Assert.True(exampleCopiedDictionary.Count>0);
}
}
}
private static void WriteFile(string name, byte[] bytes, string extension = "pdf") private static void WriteFile(string name, byte[] bytes, string extension = "pdf")
{ {
try try

View File

@ -4,6 +4,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Tokens; using Tokens;
using UglyToad.PdfPig.Writer;
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
@ -41,6 +42,16 @@
/// <inheritdoc /> /// <inheritdoc />
public void Write(Stream stream) public void Write(Stream stream)
{ {
var tokenWriter = TokenWriter.Instance;
foreach (var item in Dictionary)
{
var name = item.Key;
var value = item.Value;
stream.WriteText($"{name} ");
tokenWriter.WriteToken(value, stream);
stream.WriteNewLine();
}
stream.WriteText(Symbol); stream.WriteText(Symbol);
stream.WriteNewLine(); stream.WriteNewLine();
} }

View File

@ -80,6 +80,11 @@
'%' '%'
}; };
/// <summary>
/// Single global instance
/// </summary>
public static TokenWriter Instance { get; } = new TokenWriter();
/// <summary> /// <summary>
/// Writes the given input token to the output stream with the correct PDF format and encoding including whitespace and line breaks as applicable. /// Writes the given input token to the output stream with the correct PDF format and encoding including whitespace and line breaks as applicable.
/// </summary> /// </summary>