【问题标题】:How to keep original rotate page in itextSharp (dll)如何在itextSharp(dll)中保留原始旋转页面
【发布时间】:2013-02-23 18:47:29
【问题描述】:

我想创建项目,从 Excel 读取并在 pdf 上书写并打印此 pdf。 从 Excel 文件(从单元格)读取计算机或服务器上原始 pdf 的目录,下一个单元格包含第二个 pdf 顶部写的信息。

问题就在这里,原始 pdf 是水平的、横向的、旋转的,我的程序从原始 pdf 创建副本,并在副本 pdf 文件的顶部写入来自 excel 的信息。但是横向的pdf旋转270度。这不行。人像旋转工作程序OK,复制OK,在复制上面写就OK。 我的代码中的问题在哪里。

代码:

public int urediPDF(string inTekst)
{
    if (inTekst != "0")
    {
        string pisava_arialBD = @"..\debug\arial.ttf";
        string oldFile = null;
        string inText = null;
        string indeks = null;
        //razbitje stringa
        string[] vhod = inTekst.Split('#');
        oldFile = vhod[0];
        inText = vhod[1];
        indeks = vhod[2];

        string newFile = @"c:\da\2";

        //odpre bralnik pdf
        PdfReader reader = new PdfReader(oldFile);                
        Rectangle size = reader.GetPageSizeWithRotation(reader.NumberOfPages);
        Document document = new Document(size);

        //odpre zapisovalnik pdf                
        FileStream fs = new FileStream(newFile + "-" + indeks + ".pdf", FileMode.Create, FileAccess.Write);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        //document.Open();
        document.OpenDocument();
        label2.Text = ("Status: " + reader.GetPageRotation(reader.NumberOfPages).ToString());

        //določi sejo ustvarjanje pdf
        PdfContentByte cb = writer.DirectContent;

        //izbira pisave oblike
        BaseFont bf = BaseFont.CreateFont(pisava_arialBD, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(BaseColor.RED);
        cb.SetFontAndSize(bf, 8);

        //pisanje teksta v pdf
        cb.BeginText();
        string text = inText;

        //izbira koordinat za zapis pravilnega teksta v pdf (720 stopinj roatacija (ležeče) in 90 stopinj (pokončno))
        if (reader.GetPageRotation(1) == 720)               //ležeča postavitev
        {
            cb.ShowTextAligned(1, text, 10, 450, 0);
            cb.EndText();
        }
        else                                              //pokončna postavitev
        {
            cb.ShowTextAligned(1, text + " - pokončen", 10, 750, 0);
            cb.EndText();
        }


        // create the new page and add it to the pdf
        PdfImportedPage page = writer.GetImportedPage(reader, reader.NumberOfPages);
        cb.AddTemplate(page, 0, 0);

        // close the streams and voilá the file should be changed :)
        document.Close();
        fs.Close();
        writer.Close();
        reader.Close();
    }
    else
    {
        label2.Text = "Status: Končano zapisovanje";
        return 0;
    }
    return 0;
}

图片假pdf:

【问题讨论】:

标签: excel pdf rotation pdf-generation itextsharp


【解决方案1】:

如前所述(ITextSharp include all pages from the input fileItext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying 等),您应该阅读我的书chapter 6 中的chapter 6iText in Action(您可以找到示例的C# 版本here)。

您正在使用DocumentPdfWriterPdfImportedPage 的组合来拆分PDF。请告诉我是谁让你这样做的,这样我就可以诅咒启发你的人(因为我已经回答了数百次这个问题,而且我已经厌倦了重复自己)。这些类不是该工作的好选择:

  • 您将失去所有交互性,
  • 如果页面是横向的,您需要自己旋转内容,
  • 您需要考虑原始页面大小,
  • ...

您的问题与itextsharp: unexpected elements on copied pages 类似。您有什么理由不阅读文档吗?如果您说:“我没有时间”,请相信我,如果我说我有将近 20 年的开发经验,而且我从未将“阅读文档”视为浪费时间。

长话短说:阅读文档,将PdfWriter 替换为PdfCopy,将AddTemplate() 替换为AddPage()

【讨论】:

  • 能否请您修复损坏的 sourceforge 链接(tinyurl 链接)?
猜你喜欢
  • 2020-01-11
  • 1970-01-01
  • 2018-09-09
  • 2017-12-05
  • 2014-03-19
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多