mirror of
https://github.com/wmjordan/PDFPatcher.git
synced 2026-10-17 04:41:14 +08:00
-修复处理共用 Root 和 Info 的文档后,导致文档损坏的问题
This commit is contained in:
@@ -90,6 +90,21 @@ namespace PDFPatcher.Processor
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有些奇怪的 PDF 会让 Root 和 Info 共用一个对象,
|
||||
/// 这将导致处理后的文档无法读取,应将其复制成副本,分别修改。
|
||||
/// </summary>
|
||||
internal static void SeparateConjoinedRootAndInfo(this PdfReader pdf) {
|
||||
if (pdf.Catalog == pdf.Trailer.GetAsDict(PdfName.INFO)) {
|
||||
var d = new PdfDictionary();
|
||||
pdf.Trailer.Put(PdfName.INFO, d);
|
||||
d.Merge(pdf.Catalog);
|
||||
d.Put(PdfName.TYPE, PdfName.INFO);
|
||||
d.Remove(PdfName.PAGES);
|
||||
d.Remove(PdfName.OUTLINES);
|
||||
}
|
||||
}
|
||||
|
||||
internal static MuPDF.Document OpenMuDocument(string sourceFile) {
|
||||
var d = MuPDF.Context.Instance.OpenDocument(sourceFile);
|
||||
if (d.NeedsPassword) {
|
||||
|
||||
@@ -197,8 +197,8 @@ namespace PDFPatcher.Processor
|
||||
pdf.Trailer.Remove(PdfName.INFO);
|
||||
pdf.Catalog.Remove(PdfName.METADATA);
|
||||
}
|
||||
else if (pdf.Catalog == pdf.Trailer.GetAsDict(PdfName.INFO)) {
|
||||
FixIncorrectTrailerInfo(pdf);
|
||||
else {
|
||||
pdf.SeparateConjoinedRootAndInfo();
|
||||
}
|
||||
if (options.EnableFullCompression) {
|
||||
pdf.RemoveUnusedObjects();
|
||||
@@ -215,15 +215,6 @@ namespace PDFPatcher.Processor
|
||||
Tracker.TraceMessage(Tracker.Category.Alert, $"成功提取文件内容到 <<{targetFile}>>。");
|
||||
}
|
||||
|
||||
static void FixIncorrectTrailerInfo(PdfReader pdf) {
|
||||
var d = new PdfDictionary();
|
||||
pdf.Trailer.Put(PdfName.INFO, d);
|
||||
d.Merge(pdf.Catalog);
|
||||
d.Put(PdfName.TYPE, PdfName.INFO);
|
||||
d.Remove(PdfName.PAGES);
|
||||
d.Remove(PdfName.OUTLINES);
|
||||
}
|
||||
|
||||
static string RewriteTargetFileName(string sourceFile, string targetFile, PdfReader pdf) {
|
||||
DocInfoExporter.RewriteDocInfoWithEncoding(pdf, AppContext.Encodings.DocInfoEncoding);
|
||||
targetFile = Worker.ReplaceTargetFileNameMacros(sourceFile, targetFile, pdf);
|
||||
|
||||
@@ -350,6 +350,7 @@ namespace PDFPatcher.Processor
|
||||
if (pdf == null) {
|
||||
return false;
|
||||
}
|
||||
pdf.SeparateConjoinedRootAndInfo();
|
||||
try {
|
||||
DocInfoImporter import;
|
||||
var xInfoDoc = infoDoc as PdfInfoXmlDocument;
|
||||
|
||||
Reference in New Issue
Block a user