Title

Here some open source product which manages docx/odt to pdf/html converters :

  • JODConverter : JODConverter automates conversions between office document formats using OpenOffice.org or LibreOffice. Supported formats include OpenDocument, PDF, RTF, HTML, Word, Excel, PowerPoint, and Flash. It can be used as a Java library, a command line tool, or a web application.
  • docx4j: docx4j is a Java library for creating and manipulating Microsoft Open XML (Word docx, Powerpoint pptx, and Excel xlsx) files. It is similar to Microsoft’s OpenXML SDK, but for Java. docx4j uses JAXB to create the in-memory object representation.
  • XDocReport which provides:
    • docx converters which works with Apache POI XWPF and iText 2.3.7 for PDF.
    • odt converters which works with ODFDOM and iText 2.3.7 for PDF.

Here criteria that I think which are important for converters :

  • best renderer : the converter must not loose some formatting information.
  • fast : the converter must be the more fast.
  • less memory intensive to avoid OutOfMemory problem.
  • streaming: use InputStream/OutputStream instead of File. Using streaming instead of File, avoids some problems (hard disk is not used, no need to have write right on the hard disk)
  • easy to install: no need to install OpenOffice/LibreOffice, MS Word on the server to manage converter.

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());
  }
 }

 

 

阅读全文……

你可能感兴趣的:(java)