如何在合并的pdf itextsharp c#winform之上添加页码 [英] How do I add page number on top of combined pdf itextsharp c# winform

查看:262
本文介绍了如何在合并的pdf itextsharp c#winform之上添加页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





您好,我想在网格中从特定页面到结束页面的网格上创建一个新的组合pdf。使用下面的代码,我可以创建组合pdf,没有任何问题。但是,此代码还应该使用以下格式从左上角的网格中写入原始pdf名称,然后是原始pdf中该页面的页码。



例如,如果当前组合页面来自原始pdf examplepdf页面#10,那么它应该在左上角的组合pdf中以下列格式编写。



(examplepdf-page 10)



但是,它不起作用。请指导我出错的地方。





Hi,

Hi, I want to create a new combined pdf from different pdfs on grid from specific page to end page in the grid. Using the below code I am able to create the combine pdf without any issues. But, this code should also write the original pdf name from the grid at the top left followed by the page number of that page in the original pdf using the following format.

For eg if the current combined page is from the original pdf examplepdf page #10, then it should be written in the following format on the combined pdf in the top left.

(examplepdf-page 10)

But, it is not working. Kindly guide me where I am going wrong.


private void createReviewFiles()
{
    try
    {
        objarraylist = new ArrayList();
        if (dg_codedetails.Rows.Count > 0)
        {
            int pageOffset = 0;
            ArrayList master = new ArrayList();
            int f = 0;
            Document document = null;
            PdfCopy writer = null;

            String pfilename = String.Empty;
            String pdfilename = String.Empty;
            String outputPdfPath = String.Empty;


            DirectoryInfo dirReview = new DirectoryInfo(dir.FullName + "\\" + "ReviewFolder");
            if (!dirReview.Exists)
                dirReview.Create();
            outputPdfPath = dirReview + "\\" + "ReviewFiles.pdf";
            foreach (DataGridViewRow dgr in dg_codedetails.Rows)
            {
                pfilename = dgr.Cells[0].Value.ToString();//pdf file name
                pdfilename = dgr.Cells[7].Value.ToString();// pdf file name in specific format.


                Int16 startvalue = Convert.ToInt16(dgr.Cells[2].Value);//start page #
                Int16 endvalue = Convert.ToInt16(dgr.Cells[3].Value);//end page #


                PdfReader reader = new PdfReader(dir.FullName + "\\" + pfilename);
                reader.ConsolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;



                pageOffset += n;
                if (f == 0)
                {
                    // step 1: creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    // step 2: we create a writer that listens to the document
                    writer = new PdfCopy(document, new FileStream(outputPdfPath, FileMode.Create));
                    // step 3: we open the document
                    document.Open();
                    f += 1;
                }

                TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(1, 1, 100, 300), pdfilename);
                ////Change the orientation of the text
                tf.Rotation = 90;
                writer.AddAnnotation(tf.GetTextField());

                // put the alignment and coordinates here
                cb.ShowTextAligned(1, pdfilename, 1, 1, 0);

                // step 4: we add content
                for (int i = startvalue; i <= endvalue; i++)
                {

                    if (writer != null)
                    {
                        string text = "(" + pfilename.Replace(".pdf", "").Replace(".PDF", "") + "-page " + i + ")";
                        if (!objarraylist.Contains(text))
                            objarraylist.Add(text);
                        PdfContentByte cb = writer.DirectContent;
                        cb.BeginText();
                        cb.SetFontAndSize(BaseFont.CreateFont(), 10f);
                        cb.SetTextMatrix(600, 15);
                        cb.ShowText(pdfilename);
                        cb.EndText();
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        OnStartPage(page.PdfWriter, page.PdfDocument, pfilename);
                        writer.AddPage(page);

                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }
            }
            if (document != null)
            {
                document.Close();
            }
          

        }

    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }

}

推荐答案

引用:

但是,它不能正常工作

确定..它在做什么 - 如果有什么/程序在哪里停止会产生什么?



它不起作用对我们没用,因为我们看不到你的系统



你需要能够描述问题如果你希望人们能够帮助你,那就更准确了



我建议你开始把记录/显示放在像



ok .. what is it doing - what does it produce if anything/where does the program stop ??

"it is not working" is of no use to us whatsoever, because we cant see your system

You need to be able to describe the problem with a lot more accuracy if you expect people to be able to help you

I suggest you start putting logging/displays around things like

Quote:

pfilename = dgr.Cells [0] .Value.ToString(); // pdf文件名

pdfilename = dgr.Cells [7] .Value.ToString(); //具体格式的pdf文件名。

pfilename = dgr.Cells[0].Value.ToString();//pdf file name
pdfilename = dgr.Cells[7].Value.ToString();// pdf file name in specific format.





这样你就可以看到这些细胞有什么价值 - 这可能是你问题的一部分



so you can see what values these cells have - that may be part of your problem


这篇关于如何在合并的pdf itextsharp c#winform之上添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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