使用pdfbox将透明图像插入pdf

时间:2017-05-14 14:16:58

标签: java pdfbox

我按以下方式将图片加载到pdf:

<?php
require_once 'global.php';

// Process login (field "p" is created in uservalidation.js and contain the SHA512 hash of the password so raw password will never be sent to server)
if(isset($_POST['user_email']) && isset($_POST['p'])) {
    if($USER->login($_POST['user_email'],$_POST['p'])) {
        //header('location:index.php');
        $html.="<pre>";
        $html.=print_r($_SESSION,TRUE);
        $html.=print_r($USER,TRUE);
        $html.="</pre>";
        $html.="<a href='index.php'>Go on!</a>";
    } else {
        $html="<p>Could not log in...</p>";
    }
} else {
    $theform=new htmlForm('login.php');
    $theform->addInput('Username',array('type' => 'email', 'name' => 'user_email', 'required' => '', 'autocomplete' => 'off'));
    $theform->addInput('Password',array('type' => 'password', 'name' => 'password', 'required' => ''));
    $theform->addInput(FALSE,array('type' => 'button', 'value' => 'Login', 'class' => 'button', 'onclick' => 'formhash(this.form);'));
    $html=$theform->render();
}

// Render Page
//=================================================================================================
?>

<!doctype html>
<html class="no-js" lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NGI Skunkworks</title>
    <link rel="stylesheet" href="css/foundation.css">
    <link rel="stylesheet" href="css/app.css">
    <link rel="stylesheet" href="css/icons/foundation-icons.css" />
</head>

<body>
<?php require '_menu.php'; ?>

<div class="row">
<br>
<div class="large-12 columns">
<?php echo $html; ?>
</div>
</div>

<script src="js/vendor/jquery.js"></script>
<script src="js/vendor/what-input.js"></script>
<script src="js/vendor/foundation.js"></script>
<script src="js/app.js"></script>
<script src="js/sha512.js"></script>
<script src="js/uservalidation.js"></script>
</body>

</html>

我正在尝试让图片看起来透明,就像它会在文档的标题中看到的那样(谷歌文档,文字等) 有一个简单的方法吗?

1 个答案:

答案 0 :(得分:3)

使用扩展图形状态:

stream.saveGraphicsState();
PDExtendedGraphicsState pdExtGfxState = new PDExtendedGraphicsState();
pdExtGfxState.getCOSObject().setItem(COSName.BM, COSName.MULTIPLY); // pdExtGfxState.setBlendMode(BlendMode.MULTIPLY) doesn't work yet, maybe in later version 
pdExtGfxState.setNonStrokingAlphaConstant(0.5f);
contentStream.setGraphicsStateParameters(pdExtGfxState);
// do your stuff
stream.restoreGraphicsState();