微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用链接到另一页在现有 PDF 文件中设置段落

如何解决使用链接到另一页在现有 PDF 文件中设置段落

我需要为 PDF 文件添加一个索引,并带有指向其相应页面链接。 我将 Paragraphs 类型的数组插入到与索引相对应的 pdf 的每一页中。 我创建了一个简化的项目,以便我们可以做一个简单的测试来解决同样的问题:创建一个可点击的索引。现在,它添加了文本,但它们没有任何指向我希望它们跳转到的页面链接

主要代码为:

public string CreateIndex(string source,string destination)
{
    // Title to write in Index's Page 1
    Link[] chapters = new Link[4] { new Link(6,"Notes" ),new Link(7,"Presentation"),new Link(8,"Summary"),new Link(9,"Families") };
    // List of itext.Layout.Element.Paragraph
    List<Paragraph> lchapters = new List<Paragraph>();
    // Paragraph's Y Position
    int bottom = Constants.initialBottom;

    // For each Line to write in Index
    foreach (Link l in chapters)
    {
        // Create a Paragraph element
        Paragraph p = PDF.CreateText(l.Text,Constants.fontSize,Constants.backColor,Constants.fontColor,Constants.initialLeft,bottom,Constants.width);
        // Make Paragraph Linkable
        PDF.CreateClicableParagraph(ref p,l.Page);
        // Add to List of Paragraphs
        lchapters.Add(p);
        // New Y Position
        bottom -= Constants.jumpDown;
    }

    // Create a temporaly PDf with Index's Page 1 writed with linked Paragrapfs
    string temp_dest = destination.Replace(".pdf","temp.pdf");
    PDF.ModifyPageDocument(source,temp_dest,lchapters.ToArray(),3);

    // The same process to write a second Index Page
    chapters = new Link[2] { new Link(19,"Annex"),new Link(20,"Backcover") };
    lchapters = new List<Paragraph>();
    bottom = Constants.abajoInicial;

    foreach (Link l in chapters)
    {
        Paragraph p = PDF.CreateText(l.Text,Constants.width);
        PDF.CreateClicableParagraph(ref p,l.Page);
        lchapters.Add(p);
        bottom -= Constants.jumpDown;
    }

    PDF.ModifyPageDocument(temp_dest,destination,4);

     return destination;
}

我使用的辅助类是:

操作 PDF 对象(使用 iText)。一个名为 PDF 的类。

using iText.IO.Font;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout;
using iText.Kernel.Pdf.Canvas;

public static class PDF
{        
    public static void ModifyPageDocument(string src,string dest,Paragraph[] listText,int page)
    {            
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(src),new PdfWriter(dest));
        pdfpage pdfpage = pdfDoc.GetPage(page);
        PdfCanvas template = new PdfCanvas(pdfpage);
        Canvas canvas = new Canvas(template,pdfpage.GetPageSize());

        foreach (Paragraph p in listText) canvas.Add(p);            

        pdfDoc.Close();
    }

    public static Paragraph CreateText(string text,float fontSize,string backColor,string fontColor,float left,float bottom,float width,bool bold = false,bool italic = false,bool underline = false,TextAlignment textAlign = TextAlignment.LEFT)
    {
        PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(FontConstants.TIMES_ROMAN),PdfEncodings.WINANSI,true);

        Paragraph p = new Paragraph(text);
        p.SetFontSize(fontSize);

        if (bold) p.SetFont(font).SetBold();
        else p.SetFont(font);

        if (italic) p.SetItalic();

        if (underline) p.SetUnderline();

        if (backColor != null) p.SetBackgroundColor(GetColor(backColor));

        p.SetFixedPosition(left,width);
        p.SetTextAlignment(textAlign);
        p.SetFontColor(GetColor(fontColor));

        return p;
    }

    public static void CreateClicableParagraph(ref Paragraph p,int page)
    {
        p.SetAction(iText.Kernel.Pdf.Action.PdfAction.CreateGoTo(iText.Kernel.Pdf.Navigation.PdfExplicitRemoteGoToDestination.CreateFitB(page)));
    }

    public static DeviceRgb GetColor(string color)
    {
        switch (color)
        {
            case "WHITE":
                return (DeviceRgb)DeviceRgb.WHITE;

            case "BLACK":
                return (DeviceRgb)DeviceRgb.BLACK;

            default:
                return (DeviceRgb)DeviceRgb.WHITE;
        }
    }
}

最后,一个 Constants 类和一个用于构造索引元素的类:

public static class Constants
{
    public static int initialBottom = 700;
    public static int initialLeft = 100;
    public static int width = 100;
    public static int jumpDown = 200;
    public static int fontSize = 12;
    public static string backColor = Color.White.Value;
    public static string fontColor = Color.Black.Value;
}    



public class Link
{
    public int Page;
    public string Text;

    public Link(int p,string t)
    {
        this.Page = p;
        this.Text = t;
    }
}

我想我没有忘记任何事情。希望你能帮助我。谢谢!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。