2013-06-24 30 views
0

我試圖編輯文本與圖書館PDFBox,並不沒有如何。我不知道如何獲取單個文本對象的流,所以我可以編輯文本和/或顏色。Java,編輯PDF存在與PDFBox的文本

任何想法,例如? 謝謝

+0

見http://stackoverflow.com/questions/7269257/editing-pdf-from-java-using-pdfbox –

+0

我發現這一點,但只是添加文本。不編輯。在圖書館是類PDFTextStripper和工作,但它只提取文本,沒有風格等...而我需要編輯存在的文本。 – cervotoc

回答

0

我發現在pdf中編輯文本是不可靠的,所以嘗試用矩形(白色/背景色填充)清除文本,並將新文本寫入清除的位置。這是一個示例代碼。

//to add a link in footer 
//to replace a text 
//to replace a link/url/href 
public static void editTextorUrl(String inputFile, String outputFile) 
     throws IOException, COSVisitorException { 
    // the document 
    PDDocument doc = null; 
    try { 
     System.out.println(inputFile); 
     doc = PDDocument.load(inputFile); 
     List pages = doc.getDocumentCatalog().getAllPages(); 
     for (int i = 0; i < pages.size(); i++) { 

      float inch = 72; 
      PDGamma colourRed = new PDGamma(); 
      colourRed.setR(1); 
      PDGamma colourBlue = new PDGamma(); 
      colourBlue.setB(1); 
      PDGamma white = new PDGamma(); 
      white.setR(1); 
      white.setB(1); 
      white.setG(1); 

      PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); 
      borderThick.setWidth(inch/12); // 12th inch 
      PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary(); 
      borderThin.setWidth(inch/72); // 1 point 
      PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); 
      borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); 
      borderULine.setWidth(inch/72); // 1 point 

      PDPage page = (PDPage) pages.get(i); 
        PDFont font = PDType1Font.HELVETICA; 

      PDPageContentStream contentStream = new PDPageContentStream(
        doc, page, true, false); 
      contentStream.setNonStrokingColor(Color.WHITE); 
      contentStream.fillRect(55, 27, 144, 17); 
      contentStream.setNonStrokingColor(Color.BLUE); 
      contentStream.beginText(); 
      contentStream.setFont(font, 11); 
      contentStream.moveTextPositionByAmount(55, 37); 
      contentStream.drawString("www.loasoftwares.com"); //text to be replaced 

      contentStream.endText(); 
      contentStream.setLineWidth(inch/300); 
      contentStream.setStrokingColor(Color.BLUE); 
      contentStream.drawLine(55, 34, 188, 34); 
      contentStream.close(); 

      PDAnnotationLink txtLink = new PDAnnotationLink(); 
      PDRectangle position = new PDRectangle(); 
      position.setLowerLeftX(55); 
      position.setLowerLeftY(27); 
      position.setUpperRightX(188); 
      position.setUpperRightY(50); 
      txtLink.setRectangle(position); 

      // add an action 
      PDActionURI action = new PDActionURI(); 
      action.setURI("www.loasoftwares.com"); 
      txtLink.setBorderStyle(borderULine); 
      txtLink.setAction(action); 
      txtLink.setColour(white); 

      page.getAnnotations().add(txtLink); 
     } 
     doc.save(outputFile); 
    } finally { 
     if (doc != null) { 
      doc.close(); 
     } 
    } 
} 
+0

儘管真正編輯PDF中的文本通常很困難,但簡單地用白色矩形覆蓋文本通常不是一個好主意:文本仍可通過複製和粘貼訪問,而白色可能在非純白色的背景。 – mkl