我的PDF

时间:2017-08-26 12:13:13

标签: c# visual-studio pdf text itext

我正在做一个小程序。 我有一个17页的PDF,我想在其中一些上放置columntext。 但是在创建PDF之后,我只得到了第一页。

{
        string newfile = "newfile.pdf";

        FileStream fs = new FileStream(newfile, FileMode.Create, FileAccess.Write);
        PdfReader reader = null;
        PdfWriter writer = null;
        PdfImportedPage page = null;
        Document dc = null;

        dc = new Document(reader.GetPageSizeWithRotation(1));
        writer = PdfWriter.GetInstance(dc, fs);
        dc.Open();
        int numberOfPages = reader.NumberOfPages;
        PdfContentByte cb = writer.DirectContent;
        reader = new PdfReader(pdfPath);


        for (int i = 1; i <= numberOfPages; i++)
        {
            page = writer.GetImportedPage(reader, startPage);
            cb.AddTemplate(page, 0, 0);
            if (startPage == 1)
            {
                cb.BeginText();
                ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(new Chunk("<<NAME>>", FontFactory.GetFont("c:\\windows\\fonts\\Roboto-BoldCondensed.ttf", 15, BaseColor.BLACK)).SetSkew(0, 10)), 367.7F, 140.7F, 10);
                cb.EndText();
            }
            startPage++;
        }
        dc.Close();
        fs.Close();
        writer.Close();
        reader.Close();

    }

添加最初发布为答案

我尝试使用压模,但PDF文件已损坏。我的代码字符串newfile =“newfile2.pdf”;

出错了
        FileStream fs = new FileStream(newfile, FileMode.Create, FileAccess.Write);
        PdfReader reader = null;
        reader = new PdfReader(pdfPath);
        PdfStamper stamper = new PdfStamper(reader,fs);
        int numberOfPages = reader.NumberOfPages;
        PdfContentByte cb;
        for (int i = 1; i <= numberOfPages; i++)
        {
            cb = stamper.GetOverContent(i);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(new Chunk("<<NAME>>", FontFactory.GetFont("c:\\windows\\fonts\\Roboto-BoldCondensed.ttf", 15, BaseColor.BLACK)).SetSkew(0, 10)), 367.7F, 140.7F, 10);
        }

        fs.Close();
        reader.Close();

0 个答案:

没有答案