如何保持原始旋转页面在itextSharp(dll) [英] How to keep original rotate page in itextSharp (dll)

查看:226
本文介绍了如何保持原始旋转页面在itextSharp(dll)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i would like create the project, reading from Excel and write on pdf and print this pdf. From Excel file (from cell) read directory where is original pdf on computer or server, and next cell have info what write on the top in second pdf.

问题在这里,原始的pdf是水平的,横向的,旋转的,我的程序从原始的pdf创建副本,并从顶部的excel写信息在复制pdf文件。但是,这是景观的pdf旋转了270个deegres。这不行对于纵向旋转工作程序OK,复制OK并写在顶部的副本可以。
我的代码中我的问题在哪里

And problem is here, original pdf is horizontal, landscape, rotate and my program create copy from original pdf and write info from excel on the top on copy pdf file. But pdf which is landscape is rotate for 270 deegres. This is no OK. For portrait rotation working program OK, copy OK and write on the top of the copy is OK. Where is my problem in my code.

代码:

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:

推荐答案

如前所述许多次( ITextSharp包括输入文件中的所有页面 Itext pdf合并:文档溢出pdf(文本截断)页面,不显示等等),您应该阅读第6章我的书籍 iText在行动(您可以找到C#版本的示例 here )。

As explained many times before (ITextSharp include all pages from the input file, Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying, and so on), you should read chapter 6 of my book iText in Action (you can find the C# version of the examples here).

您正在使用文档 PdfWriter PdfImportedPage 分割PDF。请告诉我,你是怎么做到这一点的,所以我可以诅咒那个启发你的人(因为我已经回答了这个问题了数百次,而且我已经厌倦重复了自己)。这些课程不是这个工作的不错的选择:

You are using a combination of Document, PdfWriter and PdfImportedPage to split a PDF. Please tell me who made you do it this way, so that I can curse the person who inspired you (because I've answered this question hundreds of times before, and I'm getting tired of repeating myself). These classes aren't a good choice for that job:


  • 你失去了所有的互动,

  • 如果页面处于横向状态,您需要自行旋转内容,

  • 您需要考虑原始页面大小,

  • ...

  • you lose all interactivity,
  • you need to rotate the content yourself if the page is in landscape,
  • you need to take the original page size into account,
  • ...

您的问题类似于这一个 itextsharp:复制页面上的意外元素。有没有什么理由你没有阅读文件?如果你说:我没有时间,请相信我,如果我说我有20年的开发经验,而且从未见过阅读文档浪费时间。

Your problem is similar to this one itextsharp: unexpected elements on copied pages. Is there any reason why you didn't read the documentation? If you say: "I didn't have the time", please believe me if I say that I have almost 20 years of experience as a developer, and I've never seen "reading documentation" as a waste of time.

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

Long story short: read the documentation, replace PdfWriter with PdfCopy, replace AddTemplate() with AddPage().

这篇关于如何保持原始旋转页面在itextSharp(dll)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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