Sonntag, 28. August 2016

merge/overlay 2x PDF and get mixed PDF

Based on the library http://itextpdf.com/.
Usecase: Merge a pdf-invoice with a pdf-background file. The pdf-invoice does not include a company header and footer, so you want to merge it with a pdf-background template including specific information.

REM /**
REM  * pdf_tpl_merge.bbj
REM  * @author VANDEN
REM  *
REM  */

use java.io.FileOutputStream
use com.itextpdf.text.Document
use com.itextpdf.text.pdf.PdfReader
use com.itextpdf.text.pdf.PdfWriter
use com.itextpdf.text.pdf.PdfContentByte
use com.itextpdf.text.pdf.PdfImportedPage

declare Document destDoc!
declare PdfWriter destWriter!
declare PdfReader mainDocReader!
declare PdfReader singlePageBackgroundReader!
declare PdfImportedPage mainDocPage!
declare PdfImportedPage backgroundPage!
declare PdfContentByte dcb!
declare PdfContentByte ucb!

tplFile$ = "c:/0/background.pdf"
srcFile$ = "c:/0/source1.pdf"
destFile$ = "c:/0/dest"+str(tim)+".pdf"

destDoc! = new Document()
destWriter! = PdfWriter.getInstance(destDoc!, new FileOutputStream(destFile$))
destDoc!.open()
dcb! = destWriter!.getDirectContent()
ucb! = destWriter!.getDirectContentUnder()
mainDocReader! = new PdfReader(srcFile$)
singlePageBackgroundReader! = new PdfReader(tplFile$)
backgroundPage! = destWriter!.getImportedPage(singlePageBackgroundReader!,1)
for i=1 to mainDocReader!.getNumberOfPages()
    destDoc!.newPage()
    mainDocPage! = destWriter!.getImportedPage(mainDocReader!,i)
    dcb!.addTemplate(mainDocPage!,0,0)
    ucb!.addTemplate(backgroundPage!,0,0)
next i
destDoc!.close()

end

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Keine Kommentare:

Kommentar veröffentlichen