itext 5.5纵向方向无法在新页面中使用

时间:2014-06-02 10:52:02

标签: java pdf itext

我使用表格运行HTML文件的以下代码 我可以将HTML转换为PDF作为第一页,并且所有方面都有边距 但正如我document.newPage();并申请document.setPageSize();一样,它不起作用。保证金不存在。
PDF是无国界的,没有任何边缘 请指导。

代码:

public class Potrait_ParseHtmlObjects {

public static final String HTML = "C:/h.html";
public static final String DEST = "C:/test33.pdf";

public void createPdf(String file)  {

    // Parse HTML into Element list

    try{ 
    XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
    // CSS
    CSSResolver cssResolver = helper.getDefaultCssResolver(true);

    CssFile cssFile = helper.getCSS(new FileInputStream("D:\\Itext_Test\\Test\\src\\test.css"));
    cssResolver.addCss(cssFile);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.autoBookmark(false);

    //mycode starts
    FontFactory.registerDirectories();
    //mycode ends


    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
    HtmlPipeline html = new HtmlPipeline(htmlContext, end);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);


    //mycode starts        

    p.parse(new FileInputStream(HTML),Charset.forName("UTF-8"));//changed for Charset Encoding

    // step 1


    Document document = new Document();

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    writer.setInitialLeading(12.5f);

    // step 3
    document.open();

    // step 4

    Rectangle left =  new Rectangle(33,33,550,770); 

    document.setPageSize(left);
    System.out.println("1"+document.getPageSize());



    ColumnText column = new ColumnText(writer.getDirectContent());
    column.setSimpleColumn(left);

    int runDirection = PdfWriter.RUN_DIRECTION_LTR;

    column.setRunDirection(runDirection);

    int status = ColumnText.START_COLUMN;
    for (Element e : elements) {

        if (e instanceof PdfPTable) {

            PdfPTable table = (PdfPTable) e;
            for (PdfPRow row : table.getRows()) {
                for (PdfPCell cell : row.getCells()) {
                    if(cell!=null)
                    cell.setRunDirection(runDirection);
                }
            }
        }


        if (ColumnText.isAllowedElement(e)) {
            column.addElement(e);
            status = column.go();

            while (ColumnText.hasMoreText(status)) {
                Rectangle left1 = new Rectangle(50,50,500,700); 
                    document.newPage();
                    document.setPageSize(left1);
                    column.setSimpleColumn(left1);
                status = column.go();
            }

        }
    }



    // step 5
    document.close();
}catch(Exception ex)
{ex.printStackTrace();}
}

/**
 * Main method
 */
public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new Potrait_ParseHtmlObjects().createPdf(DEST);
}
}  

1 个答案:

答案 0 :(得分:1)

您执行document.newPage()时初始化所有页面参数,因此在触发document.newPage()后更改页面大小或页边距无效。如果您需要不同的页面大小(或方向或边距),则需要在调用document.newPage()之前设置页面大小,方向和边距的值(以及之前的 / em> document.open()如果你想改变第一页。)

例如:在您的情况下,您应该像这样创建文档:

Document document = new Document(new Rectangle(33,33,550,770));

您应该像这样更改页面大小:

document.setPageSize(left1);
document.newPage();
column.setSimpleColumn(left1);

您没有任何边距,因为您使用的页面大小与列相同Rectangle。您正在创建一个PDF,其左下角的坐标不等于(0, 0)。这不是非法的,但它并不常见。我的猜测是你想做这样的事情:

document.setPageSize(new Rectangle(0, 0, 550, 750););
document.newPage();
column.setSimpleColumn(new Rectangle(50,50,500,700));

这将导致页面尺寸为7.64 x 10.42英寸(550 x 750 pt),并且您的边距为0 每侧69英寸(50磅)。