반응형
반응형
반응형

샘플1

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.1</version>
</dependency>

package com.sorimiso.dvmcmm.service;

import java.io.File; 
import java.io.IOException;  

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class Test {

	public static void main(String args[]) throws IOException {     
	      
	      //Loading an existing document 
	      PDDocument doc = PDDocument.load(new File("D:/pdfBox/설치완료 확인서.pdf")); 

	      //Creating a PDF Document 
	      PDPage page = doc.getPage(0);       
	      PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
	      
	      //Begin the Content stream 
	      contentStream.beginText(); 

	      //Setting the font to the Content stream  
	      contentStream.setFont( PDType1Font.TIMES_ROMAN, 16 ); 

	      //Setting the position for the line 
	      contentStream.newLineAtOffset(25, 725 ); 
	      String text = "This is an example of adding text to a page in the pdf document. " +
	         "we can add as many lines as we want like this using the draw string method " +
	         "of the ContentStream class"; 

	      //Adding text in the form of string 
	      contentStream.showText(text); 

	      //Ending the content stream 
	      contentStream.endText(); 
	      System.out.println("Content added");       

	      //Closing the content stream 
	      contentStream.close();      

	      //Saving the document  
	      doc.save(new File("D:/pdfBox/AddText_OP.pdf")); 

	      //Closing the document  
	      doc.close();  
	   }  
}

샘플2

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>21.2</version>
</dependency>

package com.sorimiso.dvmcmm.service;

import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.Page;
import com.aspose.pdf.Position;
import com.aspose.pdf.TextBuilder;
import com.aspose.pdf.TextFragment;

public class Test2 {
	
	public static void main(String[] args) {
		AddingText();
	}
	
	
	public static void AddingText() {
		String _dataDir = "D:\\pdfBox\\";
	    // Load the PDF file
	    Document document = new Document(_dataDir + "설치완료 확인서.pdf");

	    // get particular page
	    Page pdfPage = document.getPages().get_Item(1);
	    // create text fragment
	    TextFragment textFragment = new TextFragment("Aspose.PDF");
	    textFragment.setPosition(new Position(80, 700));

	    // set text properties
	    textFragment.getTextState().setFont(FontRepository.findFont("Verdana"));
	    textFragment.getTextState().setFontSize(14);
	    textFragment.getTextState().setForegroundColor(Color.getBlue());
	    textFragment.getTextState().setBackgroundColor(Color.getLightGray());

	    // create TextBuilder object
	    TextBuilder textBuilder = new TextBuilder(pdfPage);
	    // append the text fragment to the PDF page
	    textBuilder.appendText(textFragment);

	    // Save resulting PDF document.
	    document.save(_dataDir + "AddText_out.pdf");
	}

}

 

 

 

반응형
반응형
반응형

+ Recent posts