【问题标题】:iTextSharp PDF writer content gets cutiTextSharp PDF writer 内容被删减
【发布时间】:2015-09-09 03:31:02
【问题描述】:

我使用 iTextSharp 将页脚注释添加到现有 pdf。问题是当我访问 pdf 文件并从其内容创建另一个文件时,页面的方向会改变并且内容会被切断。 我使用的代码是:

//using itextsharp
string oldFile = dtroldp + strfn;
if (File.Exists(oldFile))
{
    string newFile = strpath + "sys_" + docid + strext;

    PdfReader reader = new PdfReader(oldFile);
    int numberOfPages = reader.NumberOfPages;
    FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);

     Document document = new Document();

    // open the writer
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    writer.PageEvent = new PDFFooter();

    document.Open();

    for (int i = 1; i <= numberOfPages; i++)
    {
        document.SetPageSize(reader.GetPageSizeWithRotation(1));

        document.NewPage();
        PdfContentByte cb = writer.DirectContent;

        //// select the font properties
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(BaseColor.BLACK);
        cb.SetFontAndSize(bf, 32);

        // write the text in the pdf content
        cb.BeginText();

        cb.EndText();

        PdfImportedPage page = writer.GetImportedPage(reader, i);

        cb.AddTemplate(page, 0, 0);
    }

    document.Close();

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

谁能告诉我我做错了什么?为什么方向会自动改变?

【问题讨论】:

  • 请附上您生成的 pdf 的屏幕截图
  • 扔掉你的代码。阅读documentation。阅读有关PdfStamper 的所有答案。感到羞耻是因为你没有先阅读文档,因为你做错了。

标签: c# asp.net itextsharp


【解决方案1】:

你的方法是完全错误的。请扔掉你的代码。

向现有 PDF 添加内容是使用 PdfReaderPdfStamper 完成的,NOT 使用 DocumentPdfWriter。请先阅读这个问题的答案:Watermark in PDF file is hiding behind images

在这个例子中,我们在绝对位置添加了一些水印。在你的情况下,你有不同大小的页面,所以你不应该使用绝对位置。您应该计算 xy 的值,就像在这个问题的答案中所做的那样:How to watermark PDFs using text or images?

您需要知道以下问题的答案:

结合所有这些智慧,您应该能够使用适当的类和方法在适当的位置添加页脚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多