c# - 导入 pdf 中的指定目的地

标签 c# pdf itext

我正在开发一个应用程序,其中将word文档转换为pdf。我的问题太复杂了,请帮帮我。

我的 Word 文档有目录、书签、尾注和超链接。当我将此文档另存为 pdf 时,仅转换书签。经过长时间的研究,我发现 PDF 文档不支持书签到书签超链接,它需要页码或命名目的地。

因此我为此选择命名目的地,但我再次陷入困境,因为简单的“另存为”无法在pdf文档中生成命名目的地。因此,我在 adobe PDF 打印机上打印了 word doc,并根据需要指定了目标,但该文档中既没有书签也没有超链接。所以我决定从一个单词生成两个 pdf,第一个通过另存为选项,第二个是通过打印。

  1. test.pdf(另存为)(包含书签、超链接)
  2. test_p.pdf(通过打印)(仅包含指定目的地)

然后我再次研究,找到了一种通过 itextsharp 函数将所有命名目的地从 test_p.pdf 提取到 XML 中的方法。但不幸的是,我没有任何方法可以导入回这个 test.pdf 中的 xml .. 这就是我来这里的原因。

如果这种方法可行,请指导我下一步该怎么做。否则建议我任何其他方法来完成这项任务。

最佳答案

我不久前编写了一个类来替换 PDF 文件中的网址:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using iTextSharp.text.pdf;

namespace ReplaceLinks
{
    public class ReplacePdfLinks
    {
        Dictionary<string, PdfObject> _namedDestinations;
        PdfReader _reader;

        public string InputPdf { set; get; }
        public string OutputPdf { set; get; }
        public Func<Uri, string> UriToNamedDestination { set; get; }

        public void Start()
        {
            updatePdfLinks();
            saveChanges();
        }

        private PdfArray getAnnotationsOfCurrentPage(int pageNumber)
        {
            var pageDictionary = _reader.GetPageN(pageNumber);
            var annotations = pageDictionary.GetAsArray(PdfName.ANNOTS);
            return annotations;
        }

        private static bool hasAction(PdfDictionary annotationDictionary)
        {
            return annotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK);
        }

        private static bool isUriAction(PdfDictionary annotationAction)
        {
            return annotationAction.Get(PdfName.S).Equals(PdfName.URI);
        }

        private void replaceUriWithLocalDestination(PdfDictionary annotationAction)
        {
            var uri = annotationAction.Get(PdfName.URI) as PdfString;
            if (uri == null)
                return;

            if (string.IsNullOrWhiteSpace(uri.ToString()))
                return;

            var namedDestination = UriToNamedDestination(new Uri(uri.ToString()));
            if (string.IsNullOrWhiteSpace(namedDestination))
                return;

            PdfObject entry;
            if (!_namedDestinations.TryGetValue(namedDestination, out entry))
                return;

            annotationAction.Remove(PdfName.S);
            annotationAction.Remove(PdfName.URI);

            var newLocalDestination = new PdfArray();
            annotationAction.Put(PdfName.S, PdfName.GOTO);
            var xRef = ((PdfArray)entry).First(x => x is PdfIndirectReference);
            newLocalDestination.Add(xRef);
            newLocalDestination.Add(PdfName.FITH);
            annotationAction.Put(PdfName.D, newLocalDestination);
        }

        private void saveChanges()
        {
            using (var fileStream = new FileStream(OutputPdf, FileMode.Create, FileAccess.Write, FileShare.None))
            using (var stamper = new PdfStamper(_reader, fileStream))
            {
                stamper.Close();
            }
        }

        private void updatePdfLinks()
        {
            _reader = new PdfReader(InputPdf);
            _namedDestinations = _reader.GetNamedDestinationFromStrings();

            var pageCount = _reader.NumberOfPages;
            for (var i = 1; i <= pageCount; i++)
            {
                var annotations = getAnnotationsOfCurrentPage(i);
                if (annotations == null || !annotations.Any())
                    continue;

                foreach (var annotation in annotations.ArrayList)
                {
                    var annotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(annotation);

                    if (!hasAction(annotationDictionary))
                        continue;

                    var annotationAction = annotationDictionary.Get(PdfName.A) as PdfDictionary;
                    if (annotationAction == null)
                        continue;

                    if (!isUriAction(annotationAction))
                        continue;

                    replaceUriWithLocalDestination(annotationAction);
                }
            }
        }
    }    
}

使用它:

    new ReplacePdfLinks
    {
        InputPdf = @"test.pdf",
        OutputPdf = "mod.pdf",
        UriToNamedDestination = uri =>
        {
            if (uri.Host.ToLowerInvariant().Contains("google.com"))
            {
                return "entry1";
            }

            return string.Empty;
        }
    }.Start();

此示例将修改所有包含 google.com 的网址,使其指向特定的命名目标“entry1”。 这是测试上述类的示例文件:

void WriteFile()
{
    using (var doc = new Document(PageSize.LETTER))
    {
        using (var fs = new FileStream("test.pdf", FileMode.Create))
        {
            using (var writer = PdfWriter.GetInstance(doc, fs))
            {
                doc.Open();
                var blueFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLUE);
                doc.Add(new Chunk("Go to URL", blueFont).SetAction(new PdfAction("http://www.google.com/", false)));

                doc.NewPage();
                doc.Add(new Chunk("Go to Test", blueFont).SetLocalGoto("entry1"));

                doc.NewPage();
                doc.Add(new Chunk("Test").SetLocalDestination("entry1"));

                doc.Close();
            }
        }
    }
}

关于c# - 导入 pdf 中的指定目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15847730/

相关文章:

pdf - 使用 eps 图像的 Latex 构建缓慢

java - itext 绝对定位文字

c# - 将音频字节流式传输到 MediaPlayer

c# - 获取 JSON 子元素值

c# - 如何在代码隐藏 C# 中将参数从 Repeater 传递给 UserControl

php - 使用 PHP、JS 或 JQuery 生成多页 PDF 订单?

html - Chrome 保存为 PDF 自定义文件名

java - 更改 PDF 文件中轮廓(书签)的缩放级别

c# - 打开 PDF 文档并为其添加书签

c# - C# 是否对事件使用任何脚本语言?