pdf - 以编程方式向 PDF 添加链接

标签 pdf itext

我有大约 180 个从地理数据库生成的 PDF 文件。我想根据需要以编程方式将链接添加为顶部、底部、左侧和右侧的热点(无文本),以导航到相邻的页面文件。我还想在页面左下角的 3x3 网格上添加链接以进行额外导航。网格已经在现有的 PDF 中,只是没有链接。总共可能有 14 个链接添加到每个页面

我愿意听取有关如何着手的建议。我正在使用 Acrobat Pro XI,熟悉各种编程语言 python、vb.net、C#...只是没有直接使用 PDF 文件的经验。

最佳答案

这是很晚的答案。实际上,我正在寻找上述付费图书馆的免费替代品。我发现以下链接可能对其他人有帮助。

Apache PDFBox是一个庞大的 Java 库,用于以编程方式创建 pdf。

TomRoush/PdfBox-Android是它的android实现。您可以找到包含此实现的示例项目。

我已经使用上面的 android 库和示例项目添加了用于在 pdf 中创建可点击链接的代码。

public void createPdf(View v) {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);

    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.HELVETICA;
    // Or a custom font
    //try {
    //  PDType0Font font = PDType0Font.load(document, assetManager.open("MyFontFile.TTF"));
    //} catch(IOException e) {
    //  e.printStackTrace();
    //}

    PDPageContentStream contentStream;

    try {
        // Define a content stream for adding to the PDF
        contentStream = new PDPageContentStream(document, page);

        String preText = "Icons made by ";
        String linkText = "My_Site";

        float upperRightX = page.getMediaBox().getUpperRightX();
        float upperRightY = page.getMediaBox().getUpperRightY();
        // Write linkText in blue text
        contentStream.beginText();
        contentStream.setNonStrokingColor(15, 38, 192);
        contentStream.setFont(font, 18);
        contentStream.moveTextPositionByAmount( 0, upperRightY-20);
        contentStream.drawString(preText + linkText);
        contentStream.endText();

        // create a link annotation
        PDAnnotationLink txtLink = new PDAnnotationLink();

        // set up the markup area
        float offset = (font.getStringWidth(preText) / 1000) * 18;
        float textWidth = (font.getStringWidth(linkText) / 1000) * 18;
        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(offset);
        position.setLowerLeftY(upperRightY - 24f);
        position.setUpperRightX(offset + textWidth);
        position.setUpperRightY(upperRightY -4);
        txtLink.setRectangle(position);

        // add an action
        PDActionURI action = new PDActionURI();
        action.setURI("https://www.**********.com/");
        txtLink.setAction(action);

        // and that's all ;-)
        page.getAnnotations().add(txtLink);

        // load 'Social media' icons from 'vector' resources.
        float padding = 5, startX = 5, startY = upperRightY-100, width = 25, height=25;
        loadVectorIconWithLink(document, page, contentStream, R.drawable.ic_facebook,
                "https://www.facebook.com/My_Name/", startX, startY, width, height);
        startX += (width + padding);
        loadVectorIconWithLink(document, page, contentStream, R.drawable.ic_instagram,
                "https://www.instagram.com/My_Name", startX, startY, width, height);

        // Make sure that the content stream is closed:
        contentStream.close();

        // Save the final pdf document to a file
        String path = root.getAbsolutePath() + "/Download/Created.pdf";
        document.save(path);
        document.close();
        tv.setText("Successfully wrote PDF to " + path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void loadVectorIconWithLink( PDDocument theDocument,
                                     PDPage thePage,
                                     PDPageContentStream theContentStream,
                                     @DrawableRes int theDrawableId,
                                     String theUriString,
                                     float x, float y, float width, float height
                                     ) throws IOException
{
    Bitmap alphaImage = getBitmapFromDrawable(this, theDrawableId);
    PDImageXObject alphaXimage = LosslessFactory.createFromImage(theDocument, alphaImage);
    theContentStream.drawImage(alphaXimage, x, y, width, height );

    // create a link annotation
    PDAnnotationLink iconLink = new PDAnnotationLink();
    PDRectangle position = new PDRectangle( x, y, width, height );
    iconLink.setRectangle(position);

    // add an action
    PDActionURI action1 = new PDActionURI();
    action1.setURI(theUriString);
    iconLink.setAction(action1);

    // and that's all ;-)
    thePage.getAnnotations().add(iconLink);
}

public static Bitmap getBitmapFromDrawable(Context context, @DrawableRes int drawableId) {
    Drawable drawable = AppCompatResources.getDrawable(context, drawableId);

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof VectorDrawableCompat || drawable instanceof VectorDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    } else {
        throw new IllegalArgumentException("unsupported drawable type");
    }
}

关于pdf - 以编程方式向 PDF 添加链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272420/

相关文章:

java - PDF 文件无法从 Java 代码打开

pdf - itext 获取内容大小

java - 提取嵌入在 pdf 中的图像的名称

c# - iTextSharp HTML 到 PDF - 处理图像

iOS - 检查 webView 是否加载了非 -'pdf' 文件扩展名的 PDF 内容

java - 在jsp中显示pdf

java - 使用 iText 2.1.7 合并大型 PDF

objective-c - PDF 在 iOS 4.3 中显示不正确,但在 iOS 5.0 及更高版本中正常显示

java - 如何在Java中将ppt文件转换成pdf文件?

java - PdfCopy 中的 iText mergeFields 创建无效的 pdf