mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-06-28 15:30:17 +08:00
fix name output for merged documents
This commit is contained in:
parent
391b650e3c
commit
19047f62ae
@ -3,6 +3,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using PdfPig;
|
||||||
using PdfPig.Writer;
|
using PdfPig.Writer;
|
||||||
|
|
||||||
internal static class MergePdfDocuments
|
internal static class MergePdfDocuments
|
||||||
@ -20,6 +21,11 @@
|
|||||||
var output = Path.Combine(location, "outputOfMerge.pdf");
|
var output = Path.Combine(location, "outputOfMerge.pdf");
|
||||||
File.WriteAllBytes(output, resultFileBytes);
|
File.WriteAllBytes(output, resultFileBytes);
|
||||||
Console.WriteLine($"File output to: {output}");
|
Console.WriteLine($"File output to: {output}");
|
||||||
|
|
||||||
|
using (var doc = PdfDocument.Open(resultFileBytes))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Generated document with {doc.NumberOfPages} pages.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -48,6 +48,28 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Merge multiple PDF documents together with the pages in the order the file paths are provided.
|
||||||
|
/// </summary>
|
||||||
|
public static byte[] Merge(params string[] filePaths)
|
||||||
|
{
|
||||||
|
var bytes = new List<byte[]>(filePaths.Length);
|
||||||
|
|
||||||
|
for (var i = 0; i < filePaths.Length; i++)
|
||||||
|
{
|
||||||
|
var filePath = filePaths[i];
|
||||||
|
|
||||||
|
if (filePath == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(filePaths), $"Null filepath at index {i}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes.Add(File.ReadAllBytes(filePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Merge(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Merge the set of PDF documents.
|
/// Merge the set of PDF documents.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -3,11 +3,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Core;
|
using Core;
|
||||||
using Graphics.Operations;
|
using Graphics.Operations;
|
||||||
using Tokens;
|
using Tokens;
|
||||||
|
using Util;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes any type of <see cref="IToken"/> to the corresponding PDF document format output.
|
/// Writes any type of <see cref="IToken"/> to the corresponding PDF document format output.
|
||||||
@ -56,6 +58,20 @@
|
|||||||
|
|
||||||
private static readonly byte[] Xref = OtherEncodings.StringAsLatin1Bytes("xref");
|
private static readonly byte[] Xref = OtherEncodings.StringAsLatin1Bytes("xref");
|
||||||
|
|
||||||
|
private static readonly HashSet<char> DelimiterChars = new HashSet<char>
|
||||||
|
{
|
||||||
|
'(',
|
||||||
|
')',
|
||||||
|
'<',
|
||||||
|
'>',
|
||||||
|
'[',
|
||||||
|
']',
|
||||||
|
'{',
|
||||||
|
'}',
|
||||||
|
'/',
|
||||||
|
'%'
|
||||||
|
};
|
||||||
|
|
||||||
/// <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>
|
||||||
@ -276,7 +292,29 @@
|
|||||||
|
|
||||||
private static void WriteName(string name, Stream outputStream)
|
private static void WriteName(string name, Stream outputStream)
|
||||||
{
|
{
|
||||||
var bytes = OtherEncodings.StringAsLatin1Bytes(name);
|
/*
|
||||||
|
* Beginning with PDF 1.2, any character except null (character code 0) may be
|
||||||
|
* included in a name by writing its 2-digit hexadecimal code, preceded by the number sign character (#).
|
||||||
|
* This is required for delimiter and whitespace characters.
|
||||||
|
* This is recommended for characters whose codes are outside the range 33 (!) to 126 (~).
|
||||||
|
*/
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var c in name)
|
||||||
|
{
|
||||||
|
if (c < 33 || c > 126 || DelimiterChars.Contains(c))
|
||||||
|
{
|
||||||
|
var str = Hex.GetString(new[] { (byte)c });
|
||||||
|
sb.Append('#').Append(str);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bytes = OtherEncodings.StringAsLatin1Bytes(sb.ToString());
|
||||||
|
|
||||||
outputStream.WriteByte(NameStart);
|
outputStream.WriteByte(NameStart);
|
||||||
outputStream.Write(bytes, 0, bytes.Length);
|
outputStream.Write(bytes, 0, bytes.Length);
|
||||||
|
Loading…
Reference in New Issue
Block a user