import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class PdfTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("C:\\1.pdf");
int n = reader.getNumberOfPages();
Document document = new Document(reader.getPageSize(n));
float width = document.getPageSize().width();
// Create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader,
new FileOutputStream("c:\\text1.pdf"));
PdfContentByte over;
Image img = Image.getInstance("c:\\view.png");
width = width - img.width();
System.out.println("width:" + width);
img.setAbsolutePosition(width, 0);
img.setAlignment(Image.ALIGN_RIGHT);
if(n > 0)
{
// Text over the existing page
over = stamp.getOverContent(n);
over.addImage(img);
}
stamp.close();
}
}
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class PdfTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("C:\\1.pdf");
int n = reader.getNumberOfPages();
Document document = new Document(reader.getPageSize(n));
float width = document.getPageSize().width();
// Create a stamper that will copy the document to a new file
PdfStamper stamp = new PdfStamper(reader,
new FileOutputStream("c:\\text1.pdf"));
PdfContentByte over;
Image img = Image.getInstance("c:\\view.png");
width = width - img.width();
System.out.println("width:" + width);
img.setAbsolutePosition(width, 0);
img.setAlignment(Image.ALIGN_RIGHT);
if(n > 0)
{
// Text over the existing page
over = stamp.getOverContent(n);
over.addImage(img);
}
stamp.close();
}
}
本文介绍了一个使用Java实现的在PDF文件中特定页面添加图片水印的例子。该示例利用了iText库来操作PDF文件,并展示了如何加载现有PDF、定位图片位置以及将图片作为水印添加到PDF页面上。
1万+

被折叠的 条评论
为什么被折叠?



