Blank page when copy document

混江龙づ霸主 提交于 2019-12-11 10:14:40

问题


When I copy document

var document = new Document();

    var writer = PdfWriter.GetInstance(document, memoryStream);
    document.Open();
    var cb = writer.DirectContent;
    var reader = new PdfReader(this_file_name);
    document.SetPageSize(reader.GetPageSizeWithRotation(1));
    document.NewPage();
    var page = writer.GetImportedPage(reader, 1);
    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    document.CloseDocument();       writer.Close();

When I open new document - it's empty. I try change pdf version and compression level - no results. I can't use PdfStamper, bacause after copy document need insert some text and image.this bad file


回答1:


That PDF is a good example of why it is never a good idea to assume that 0,0 corresponds to the "lower left corner". PDFs are actually free to redefine their coordinate space as they see fit. That PDF has this specific entry for page 1:

/MediaBox   [0, -1693.08, 2396.52, 0]

This offsets the y parameter by 1693.08 units "downwards". Luckily this is pretty easy to fix. You're setting the page size correctly but you also want to use that page size's coordinates when placing the template:

var s = reader.GetPageSizeWithRotation(1);
cb.AddTemplate(page, s.Left, s.Bottom);


来源:https://stackoverflow.com/questions/33100786/blank-page-when-copy-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!