我正在尝试使用iText创建一个新的PDF文件,新文件应该由从SVG文件(磁贴)中的区域生成的页面组成 . 并且每个生成的页面都需要在其上叠加“绘图边框”,以使其显示为每个生成的页面的顶层 . 我认为可以使用iText压模实现叠加,但我看到的示例仅适用于现有的PDF,我没有,因为我想在运行中做所有这些以减少生成结果所需的时间PDF . 我有一个单页的PDF文件,表示PDF层将叠加在PDF中生成的每个页面上 .

我在网上找到了一个示例,它将SVG画布划分为图块并从每个图块创建单独的PDF文件 . 但是,创建中间PDF文件浪费时间,因为我需要再次读取它们来创建我想要创建的文件(每个页面都有叠加层) .

我不想在这里转储大量代码,但这里是我用来为每个SVG磁贴生成一个页面PDF文件的代码示例:http://people.apache.org/~clay/batik/rasterizerTutorial.pdf(参见第5页:SaveAsJPEGTiles.java)

由于我是iText新手,也许有人在这里取得了类似的成就,或者知道如何做到这一点?

谢谢 .

我正在使用ByteArrayOutputStream而不是FileOutputStream,但是当我尝试将其与iText的Document类型结合使用时,我不断得到一个异常,说明文档中没有页面 - 请参阅下面的代码 . 我想我需要将每个磁贴的ByteArrayOutputStream添加到iText文档中,以便为每个磁贴创建一个新页面,但我无法从iText API中看到如何做到这一点 . 到目前为止,这是我的代码,但不太合适:

package examples.aoi;

import java.io.*;
import java.awt.*;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;

public class FromCanvasToPDFPages {

    PDFTranscoder trans = new PDFTranscoder();

    public FromCanvasToPDFPages() {
    }

    public void tile(TranscoderInput input, TranscoderOutput output, Rectangle aoi) throws Exception {

        trans.addTranscodingHint(PDFTranscoder.KEY_WIDTH, new Float(aoi.width));
        trans.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, new Float(aoi.height));
        trans.addTranscodingHint(PDFTranscoder.KEY_AOI, aoi); 

        trans.transcode(input, output);

    }

    public static void main(String [] args) throws Exception {

        FromCanvasToPDFPages p = new FromCanvasToPDFPages();

        String in = "C:\\Documents and Settings/krell/My Documents/1.svg";
        String svgURI = new File(in).toURL().toString();
        TranscoderInput input = new TranscoderInput(svgURI);

        String outputFilename = "canvasPages.pdf";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TranscoderOutput output = new TranscoderOutput(baos);

        int A4_landscape_width = 1103; // use proper dimensions from class when found
        int A4_landscape_height = 780; // use proper dimensions from class when found

        int documentWidth = 3309;     // read this directly from canvas file (svg)
        int documentHeight = 1560;    // read this directly from canvas file (svg)

        int columns = documentWidth / A4_landscape_width;
        int rows = documentHeight / A4_landscape_height;

        int tile_width = A4_landscape_width;
        int tile_height = A4_landscape_height;

        System.err.println(">>> Canvas document width = "+documentWidth);
        System.err.println(">>> Canvas document height = "+documentHeight);

        System.err.println(">>> A4 width = "+A4_landscape_width);
        System.err.println(">>> A4 height = "+A4_landscape_height);

        System.err.println(">>> canvas columns = "+columns);
        System.err.println(">>> canvas rows = "+rows);

        System.err.println(">>> tile_width = "+tile_width);
        System.err.println(">>> tile_height = "+tile_height);


        // Create A4 document to write tile data into
        Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
        PdfWriter writer = PdfWriter.getInstance(document, baos);

        // open the document
        document.open();

        // now process each tile, row by row, column by column until done
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                // get tile coordinates
                Rectangle tile = new Rectangle(column*tile_width, row*tile_height, (column+1)*tile_width, (row+1)*tile_height);
                p.tile(input, output, tile);
            }
        }

        // now write the baos to disk:
        OutputStream fileOutputStream = new FileOutputStream(outputFilename);
        baos.writeTo(fileOutputStream);

        // close stuff
        fileOutputStream.flush();
        fileOutputStream.close();
        document.close();

        baos.close();

        System.exit(0);
    }

}