2015-02-09 74 views
1

我想打印出一個帶有水印的tiff圖像。因此,首先我將tiff圖像轉換爲pdf並將水印添加到生成的pdf中。但水印不會出現在最終的pdf中。有沒有任何建議打印出帶有水印的tiff圖像。任何幫助將不勝感激。這是我的代碼。如何在java中使用itext 2.1.7爲TIFF圖像添加水印

public class TiffToPdf { 
private static int TEXT_TILT_ANGLE = 25; 
private static Color MEDIUM_GRAY = new Color(242,17,72); 
private static int PRIMARY_FONT_SIZE = 50; 
private static String tif = "C:/Sample.tif"; 
private static String pdf = "C:/Sample.pdf"; 
private static String watermarkpdf = "C:/Watermark.pdf" 

public static void main(String[] args) throws Exception { 
    convert(tif, pdf); 
    File watermark = new File(watermarkpdf); 
    OutputStream outputStream = new FileOutputStream(watermark); 
    addWaterMark(pdf, outputStream, "This is Sample WaterMark"); 
} 

public static File convert(String tif, String pdf) { 
    File pdfFile = null; 
    String imgeFilename = tif; 
    Document document = new Document(); 
    try { 
     PdfWriter writer = PdfWriter.getInstance(
       document, 
       new FileOutputStream(pdf)); 
     writer.setStrictImageSequence(true); 
     Image image; 
     document.open(); 
     RandomAccessFileOrArray ra = new RandomAccessFileOrArray(imgeFilename); 
     int pagesTif = TiffImage.getNumberOfPages(ra); 
     for (int i = 1; i <= pagesTif; i++) { 
      image = TiffImage.getTiffImage(ra, i); 
      image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getHeight()); 
      document.setMargins(0, 0, 0, 0); 
      document.newPage(); 
      document.add(image); 
     } 
     pdfFile = new File(pdf); 
     document.close(); 
    } catch (Exception ex) { 
     //do nothing 
    } 
    return pdfFile; 
} 

public static void addWaterMark(String pdfFile,OutputStream outputStream, String watermark) throws Exception{ 
    PdfReader reader = new PdfReader(pdfFile); 
    int numPages = reader.getNumberOfPages(); 
    // Create a stamper that will copy the document to the output 
    // stream. 
    PdfStamper stamp = new PdfStamper(reader, outputStream); 
    int page=1; 

    BaseFont baseFont = 
     BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE, 
      BaseFont.WINANSI, BaseFont.EMBEDDED); 

    float width; 
    float height; 

    while (page <= numPages) { 
     PdfContentByte cb = stamp.getOverContent(page); 
     height = reader.getPageSizeWithRotation(page).getHeight()/2; 
     width = reader.getPageSizeWithRotation(page).getWidth()/2; 

     cb = stamp.getUnderContent(page); 
     cb.saveState(); 
     cb.setColorFill(MEDIUM_GRAY); 

     // Primary Text 
     cb.beginText(); 
     cb.setFontAndSize(baseFont, PRIMARY_FONT_SIZE); 
     cb.showTextAligned(Element.ALIGN_CENTER, watermark, width, 
       height, TEXT_TILT_ANGLE); 
     cb.endText(); 
     cb.restoreState(); 

     page++; 
    } 

    stamp.close(); 
} 

}

+0

您正在使用不再支持的iText版本。請停止使用[出於這個原因](http://stackoverflow.com/a/25698526/1622493)。無論您的客戶是誰,他都不會意識到您將過時的軟件引入其代碼庫的事實。 – 2015-02-09 07:25:00

回答

2

很多事情是你的代碼錯誤。例如:你說水印沒有出現在你的最終PDF中,但是如果你看得更近,你會看到水印確實被添加了,但是它被一個不透明的圖像覆蓋(你在之前添加的TIFF走)。

看看你的代碼。首先定義cb作爲覆蓋圖像層:

PdfContentByte cb = stamp.getOverContent(page); 

但幾乎立即那行之後,你重新定義cb作爲現有的圖像下那張層:

cb = stamp.getUnderContent(page); 

刪除此行並你的水印會出現。

你沒有做你的客戶的忙,因爲:

  1. 您正在使用一個版本的iText的,可以給他帶來麻煩。
  2. 您的addWatermark()方法假定座標系的原點爲(0, 0),但情況並非總是如此,所以如果您的客戶開始將您的方法用於其他PDF,它可能會在頁面的可見區域外添加水印。
  3. 您首先創建PDF,然後閱讀該PDF以添加水印。爲什麼不在使用頁面事件創建期間添加水印?
  4. 當您創建PDF時,您將縮放TIFF圖像以精確匹配A4頁面。這意味着如果圖像的縱橫比不同於A4頁面的縱橫比,圖像將嚴重失真。例如:您創建一個縱向頁面,但如果TIFF是橫向頁面,則會使TIFF變得難以辨認。

總而言之,您的代碼不適合生產。我的建議是重寫它並使用最新的iText版本。