Itextsharp:导入页面时有没有办法复制链接?

时间:2012-06-13 19:36:56

标签: asp.net c#-4.0 itextsharp hyperlink

我正在开展一个项目,我需要在显示之前编辑pdf

我需要

  • 添加水印
  • 编辑权限(锁定以避免'复制/粘贴'和'另存为')
  • 编辑查看器首选项

我做到了......并且工作正常,除了一件事,原始文件中的链接在新文件中不起作用......任何想法?

注意:实际上,这是我的代码(我正在使用itextsharp)

    private void loadPdf()
    {
        if (Request.QueryString.HasKeys())
        {
            if (Request.QueryString.GetKey(0) == "thepath" && Request.QueryString.GetKey(1) == "isprintable" && Request.QueryString.GetKey(2) == "type")
            {
                #region kuak
                Document doc = new Document();
                PdfReader pdfReader = new PdfReader(Request.QueryString["thepath"]);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    PdfWriter pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
                    pdfWriter.ViewerPreferences = PdfWriter.PageModeUseOutlines;
                    //pdfWriter.ViewerPreferences = PdfWriter.PageLayoutTwoColumnLeft;  /// Despliega el docuemnto en pares de hojas
                    pdfWriter.ViewerPreferences = PdfWriter.PageLayoutOneColumn;
                    pdfWriter.ViewerPreferences = PdfWriter.HideToolbar;
                    //pdfWriter.ViewerPreferences = PdfWriter.HideWindowUI; /// quita los scrollbars y el panel de la derecha qur contiene los bookmarks y las buskedas dentro del pdf
                    if (Request.QueryString["isprintable"] == "n")
                    {
                        pdfWriter.ViewerPreferences = PdfWriter.HideMenubar;
                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        pdfWriter.SetEncryption(null, encoding.GetBytes("mYpAssss"), 0, PdfWriter.STRENGTH40BITS);
                    }
                    doc.Open();
                    PdfContentByte pdfContentByte = pdfWriter.DirectContent;
                    doc.AddDocListener(pdfWriter);
                    for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                    {
                        //doc.SetPageSize(pdfReader.GetPageSize(page));
                        doc.SetPageSize(pdfReader.GetPageSizeWithRotation(page));
                        doc.NewPage();
                        PdfImportedPage pdfImportedPage = pdfWriter.GetImportedPage(pdfReader, page);
                        int rot = pdfReader.GetPageRotation(page);
                        if (rot == 90 || rot == 270)
                            pdfContentByte.AddTemplate(pdfImportedPage, 0, -1.0F, 1.0F, 0, 0, pdfReader.GetPageSizeWithRotation(page).Height);
                        else
                            pdfContentByte.AddTemplate(pdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0);
                        string theId = findId();
                        if (isWatermarkNeeded(theId))
                        {
                            #region ADD TEXT WATERMARK
                            //pdfContentByte.BeginText();
                            //iTextSharp.text.Rectangle pageSize = pdfReader.GetPageSizeWithRotation(page);
                            //BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, System.Text.Encoding.ASCII.EncodingName, false);
                            //pdfContentByte.SetFontAndSize(baseFont, 200);
                            //BaseColor baseColor = new BaseColor(255, 0, 0, 20);
                            //pdfContentByte.SetColorFill(baseColor);
                            //float textAngle = (float)GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
                            //pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "DRAFT", 350, pageSize.Height / 2, textAngle);
                            //pdfContentByte.EndText();
                            #endregion
                            #region ADD IMAGE WATERMARK

                            string fechaExp = "Este documento vence: " + GetExpirationDate(theId).ToShortDateString();
                            pdfContentByte.BeginText();
                            //iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/watermark3.png"));
                            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ImageCheck.CreatePicture(@"C:\Users\myUser\Desktop\watermark.png", fechaExp).ToArray());
                            img.SetAbsolutePosition(0, 0);
                            pdfContentByte.AddImage(img);
                            pdfContentByte.EndText();
                            #endregion
                        }

                    }
                    pdfReader.Close();
                    doc.Close();
                    byte[] content = memoryStream.ToArray();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-length", content.Length.ToString());
                    Response.BinaryWrite(content);
                }
                #endregion
            }
            else
            {
                //hay querystring pro no corresponden con los que se necesita
            }
        }
        else
        {
            //no se enviaron los querystring
        }


    }

1 个答案:

答案 0 :(得分:3)

您必须从原始PDF获取链接

var links = reader.GetLinks(pageNumber);

并将它们写入新的PDF

 foreach (var link in links)
                    {
                        var annotation = link.CreateAnnotation(pdfWriter);
                        writer.AddAnnotation(annotation);
                    }