Itext PDF显示空白页面 [英] Itext PDF shows blank page

查看:879
本文介绍了Itext PDF显示空白页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码,它使用iText生成PDF文件。

I have the below sample code which generates a PDF file using iText.

我的问题是当我通过DatatypeConverter.printBase64Binary方法创建base64Binary时..
我试图复制base64Binary的Sysem.out.println。
使用在线base64在线解码器工具解码内容并将其输出保存为sample.pdf和
,当我尝试打开sample.pdf时显示为空。我不确定为什么它的表现和帮助将非常感激。
但是当我使用java直接解码并将其写入磁盘文件时,它会显示上下文。

The question I have is when I create the base64Binary by DatatypeConverter.printBase64Binary method.. I tried to copy the Sysem.out.println of "base64Binary". Used a online base64 online decoder tool to decode the content and save it output as sample.pdf and when I try to open the sample.pdf it shows empty. I am not sure why its behaving this way and help would be much appreciated. But when I directly decode using java and write it to a disk file it shows the context.

当我尝试将base64Binary输出保存为sample.pdf时,有人可以帮助我理解为什么显示空白。

Can someone help me understand why it shows blank when I try to save "base64Binary" output as sample.pdf.

谢谢。

以下是代码段:

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.DatatypeConverter;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Creates a PDF file in memory.
 */
public class HelloWorldMemory {

    /** Path to the resulting PDF file. */
    public static final String RESULT = "C:////hello_memory.pdf";

    public static void main(final String[] args) throws DocumentException, IOException {
        // step 1
        final Document document = new Document();

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PdfWriter writer = PdfWriter.getInstance(document, baos);

        document.open();
        final PdfContentByte cb = writer.getDirectContent();

        cb.beginText();
        cb.setFontAndSize(getBaseFont(Font.NORMAL), 24);
        final float exPosition = (PageSize.A4.getWidth()) / 2;
        cb.showTextAligned(Element.ALIGN_CENTER, "Test No", exPosition, 670, 0);
        cb.endText();
        document.add(new Paragraph("Hello World!"));

        document.close();
        System.out.println("baos.toByteArray():" + baos.toByteArray());
        final String base64Binary = DatatypeConverter.printBase64Binary(baos.toByteArray());
        System.out.println("base64Binary:" + base64Binary);
        final byte[] txt = DatatypeConverter.parseBase64Binary(base64Binary);

        final FileOutputStream fos = new FileOutputStream(RESULT);
        fos.write(txt);

        fos.close();
    }

    private static BaseFont getBaseFont(final int fontType) {
        final Font f = new Font(FontFamily.HELVETICA, 0, fontType);
        final BaseFont baseFont = f.getCalculatedBaseFont(true);
        return baseFont;
    }
}


推荐答案

PDF是基于Carousel对象系统(COS)语法和AIM(Adobe Imaging Model)的二进制文件格式。 COS对象使用ASCII作为文件结构,但图像流和AIM语法通常是二进制的。当您复制PDF文件而不考虑文件的二进制方面时,PDF查看器可以基于ASCII COS对象呈现文档结构(页面),但不能呈现内容(页面上的内容)。这可能就是你的情况:你正在破坏内容流中的字节。

PDF is a binary file format based on the Carousel Object System (COS) syntax and the AIM (Adobe Imaging Model). The COS objects use ASCII for the file structures, but the streams for images and AIM syntax are usually binary. When you copy a PDF file without respecting the binary aspect of the file, a PDF viewer can render the document structure (the pages) based on the ASCII COS objects, but not the content (what's on the pages). This is probably what happens in your case: you're corrupting the bytes in the content streams.

这篇关于Itext PDF显示空白页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆