【问题标题】:iTextSharp adds white chunk at the top of the page when importing a PDFiTextSharp 在导入 PDF 时在页面顶部添加白色块
【发布时间】:2015-01-29 13:19:31
【问题描述】:

我在使用 iTextSharp 和 C# 时遇到了一个小问题。

上下文: 我下载PDF并将它们合并成一个巨大的。

问题: 在每一页上,前几厘米都是白色的,而我导入的 pdf 在该白色块之后开始。

每一页的结尾都是正确的。没有重叠或丢失的对象/文本 - 您会假设它必须处理更少的空间。我认为它可能会被垂直拉伸。

所以导入工作正常,但它总是在每一页的顶部添加几厘米的白色。 这感觉就像一个最高利润。但我似乎无法修复它。

有什么想法吗?

感谢您的帮助。非常感谢。

public void method()
{

    // needed variables for the pdf-merging part
    fs = new FileStream(Variables.destinationFile, FileMode.Create);
    writer = PdfWriter.GetInstance(doc, fs);
    doc.Open();
    doc.SetPageSize(PageSize.A4);
    doc.SetMargins(0f, 0f, 0f, 0f);
    pdfContent = writer.DirectContent; 

    byte[] result;
    int numPages;


    foreach (Tuple<string, string, int> currentTuple in someArray)

            try
                {
                    result = client.DownloadData(new Uri(adress + currentTuple.Item1 + ".pdf"));

                    // read and add the pages to the output file
                    reader = new PdfReader(result);
                    numPages = reader.NumberOfPages;

                    for (int i = 1; i < numPages + 1; i++)
                    {
                        doc.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        pdfContent.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                catch (Exception e)
                {
                }   

        }   

        doc.Close();
        writer.Close();
        fs.Close();
}

附言为什么它总是删除我的“你好”? :)

【问题讨论】:

标签: c# pdf itextsharp


【解决方案1】:

您使用了错误的方法来合并文档。您的方法抛弃了所有交互性并且不尊重页面大小(这解释了您报告的问题)。请告诉我你从哪里得到了以这种方式合并文档的灵感,这样我就可以去打你正在使用的示例的负责人;-)

连接文档的正确方法在chapter 6 of my book中解释。

您可以在此处找到更多示例:

如您所见,您的问题之前已在 StackOverflow 上得到多次回答,因为许多人一直在使用正确的方式来合并文档(使用 PdfCopy)而不是错误的方式(使用 @ 987654329@ 和 AddTemplate())。

在您的评论中,您说AddPage() 方法在PdfCopy 中不存在。让我们看一下该类的最新版本:PdfCopy.cs

我清楚地看到:

/**
 * Add an imported page to our output
 * @param iPage an imported page
 * @throws IOException, BadPdfFormatException
 */
public virtual void AddPage(PdfImportedPage iPage) {

请注意,最近的版本也有一个AddDocument() 方法:

virtual public void AddDocument(PdfReader reader) {

使用此方法,您不再需要遍历所有页面,而是可以一次添加PdfReader 正在阅读的 PDF 的所有页面。

如果您只想添加选择的页面,您可以使用:

virtual public void AddDocument(PdfReader reader, List<int> pagesToKeep) {

不要使用非官方版本!官方版本可以在这里下载:http://sourceforge.net/projects/itextsharp/files/itextsharp/

iText Group 不对 iTextSharp 的旧版本承担任何责任,我们也不对我们软件的分叉负责。

【讨论】:

  • 感谢您的帮助!他们似乎都在使用 PdfCopy.AddPage(page);然而,在我的情况下,从未找到该方法。它不存在。我不知道我使用的是什么版本。但它是最新的之一。最多 1 个月大。
  • 好的。第 8 层问题。谢谢你的帮助。修复。当我用谷歌搜索时,我发现大多数人都在使用 PdfWriter :/
猜你喜欢
  • 2011-01-31
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 2015-09-15
相关资源
最近更新 更多