gpt4 book ai didi

pdfbox - Apache PDFBOX 中的文本替换为图像

转载 作者:行者123 更新时间:2023-12-02 02:40:46 36 4
gpt4 key购买 nike

任何人都可以帮我了解如何使用 Apache PDFBOX 将文本替换为图像吗?

最佳答案

import java.io.File;    
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

public class RubberImagePaste {
public void createPDFFromImage( String inputFile, String imagePath, String outputFile )
throws IOException
{
// the document
PDDocument doc = null;
try
{
doc = PDDocument.load( new File(inputFile) );

PDPage page = doc.getPage(0);

// createFromFile is the easiest way with an image file
// if you already have the image in a BufferedImage,
// call LosslessFactory.createFromImage() instead
PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);

// contentStream.drawImage(ximage, 20, 20 );
// better method inspired by http://stackoverflow.com/a/22318681/535646
// reduce this value if the image is too large
float scale = 1f;
contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);

contentStream.close();
doc.save( outputFile );
}
finally
{
if( doc != null )
{
doc.close();
}
}
}


/**
*
*
* @param args The command line arguments.
*
* @throws IOException If there is an error parsing the document.
*/
public static void main( String[] args ) throws IOException
{
String documentFile = "D:/ABC.pdf";
String stampFile = "D:/Sign.png";
String outFile = "D:ABCnew.pdf";

new File("target/test-output").mkdirs();

String[] args1 = new String[] { documentFile, outFile, stampFile };
RubberImagePaste rubberStamp = new RubberImagePaste();
rubberStamp.createPDFFromImage( documentFile, stampFile, outFile);
}

/**
* This will print the usage for this example.
*/
private void usage()
{
System.err.println( "Usage: java "+getClass().getName()+" <input-pdf> <output-pdf> <image-filename>" );
}
}

特别感谢蒂尔曼

关于pdfbox - Apache PDFBOX 中的文本替换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223101/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com