Here some open source product which manages docx/odt to pdf/html converters :
Here criteria that I think which are important for converters :
In this article I will introduce those 3 Java frameworks converters and I will compare it to give Pros/Cons for each framework and try to be more frankly because I’m one of XDocReport developer.
给PDF加水印的方法:
一、Adobe Acrobat JavaScript
1、直接加文字:
this.addWatermarkFromText({
cText: "中华人民共和国",
nTextAlign: app.constants.align.right,
nHorizAlign: app.constants.align.right,
nVertAlign: app.constants.align.top,
nHorizValue: -72, nVertValue: -72
});
2、将某个水印文件加入到PDF中
this.addWatermarkFromFile({
cDIPath: "/C/watermark.pdf",
nHorizAlign: app.constants.align.left,
nVertAlign: app.constants.align.top,
nHorizValue: 144, nVertValue: -72,
nRotation: 45
});
这两种方法速度极快,操作PDF资源占用极小, 而且支持任何东西方语言。当然还有很多细节问题,例如字体等等,具体用法可参阅Adobe Acrobat Scripting developmenthttp://www.adobe.com/devnet/acrobat/pdfs/js_api_reference.pdf
二、用java开源项目itextpdf来实现
下载itext 5.4.1,地址:http://itextpdf.com/download.php
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public static final String CHARACTOR_FONT_CH_FILE = "SIMHEI.TTF"; //黑体常规
public static void AddWaterMark(String filename,String outPutFile,String waterText,
String pdfTitle,String Subject,String Keywords,String Creator,
String Author) throws DocumentException,IOException{
PdfReader.unethicalreading = true;
// 待加水印的文件
PdfReader reader = new PdfReader(filename);
// 加完水印的文件
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outPutFile));
HashMap
方法三:利用Apache pdfbox实现,下载地址:http://pdfbox.apache.org/
引入pdfbox相应包,略
public static void main(String args[]){
try{
PDDocument document = PDDocument.load(musiqueFileName);
List pages = doc.getDocumentCatalog().getAllPages();
PDPage page = (PDPage) pages.get(0); //给第一页加水印,如果需要全部加,那么循环即可
//创建有个输出流,注意第3个参数必须为true,
PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);
//加文字水印
contentStream.beginText();
contentStream.setFont( PDType1Font.TIMES_ITALIC, 12f );
contentStream.moveTextPositionByAmount( 10,10 );
String msg = "Hello World";
contentStream.drawString(msg);
contentStream.endText();
//加图片水印
String image = "D:/img.jpg";
PDXObjectImage ximage = null;
if( image.toLowerCase().endsWith( ".jpg" ) )
{
ximage = new PDJpeg(document, new FileInputStream( image ) );
}
else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
{
ximage = new PDCcitt(document, new RandomAccessFile(new File(image),"r"));
}
else
{
throw new IOException( "Image type not supported:" + image );
}
contentStream.drawImage( ximage, 0, 0);
contentStream.close(); //这一句很重要,不关闭将无法写入
document.save("C:/lucene/Test.pdf");
document.close();
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
阅读全文……