【问题标题】:C# itextsharp - Create PDF, Create Folder within Folder, Place PDF in deepest FolderC# itextsharp - 创建 PDF,在文件夹中创建文件夹,将 PDF 放在最深的文件夹中
【发布时间】:2021-03-26 15:18:38
【问题描述】:

我是 C# 的完全初学者,我正在寻求有关我必须制作的这个项目的一些帮助。

我正在使用 Windows 窗体和 itextsharp 以及 Microsoft Access。我的目标是制作程序,在用户与 DataGridView 交互并在文本框上归档一些信息后,创建一个 PDF 文件(上面有 DataGridView),然后创建一个文件夹(名称是用户给出的机器 ID之前)在一个文件夹(名称是用户之前给出的客户端 ID)中分配 PDF。

此代码仅稍微格式化 DataGridView 并保存 PDF(通过询问用户的位置)。

    public void exportgridtopdf(DataGridView dgw, string filename)

    {
        BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
        PdfPTable pdftable = new PdfPTable(new float[] { 1, 4, 1, 1, 1 });
        pdftable.DefaultCell.Padding = 3;
        pdftable.WidthPercentage = 100;
        pdftable.HorizontalAlignment = Element.ALIGN_LEFT;
        pdftable.DefaultCell.BorderWidth = 1;

        iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
        //add header
        foreach (DataGridViewColumn column in dgw.Columns)
        {
            PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, text));
            cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
            pdftable.AddCell(cell);
        }

        //add datarow
        foreach (DataGridViewRow row in dgw.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                pdftable.AddCell(new Phrase(cell.Value.ToString(), text));
            }
        }

        var savefiledialoge = new SaveFileDialog();
        savefiledialoge.FileName = filename;
        savefiledialoge.DefaultExt = ".pdf";
        if (savefiledialoge.ShowDialog() == DialogResult.OK)
        {
            using (FileStream stream = new FileStream(savefiledialoge.FileName, FileMode.Create))
            {
                Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                PdfWriter.GetInstance(pdfdoc, stream);
                pdfdoc.Open();
                pdfdoc.Add(pdftable);
                pdfdoc.Close();
                stream.Close();


            }


        }
    }

我发现的最相似的案例在这里:How to insert files into folders of an existing PDF portfolio with folders using iTextsharp 我尝试实施(我认为我可以使用的),但我无法获得任何参考或实现我的目标的任何实际进展。

    public class FolderWriter
    {
        private const string Folder = @"C:\Path\to\your\pdf\files";
        private const string File1 = @"Pdf File 1.pdf";
        private readonly string file1Path = Path.Combine(Folder, File1);
        private readonly string[] keys = new[] {
    "Type",
    "File"
       };
    }

我很抱歉我缺乏知识并且可能提供的信息很少。如果您需要特定的内容,我可以尝试上传更多代码。

提前致谢,

菲利普·阿尔梅达

【问题讨论】:

    标签: c# datagridview directory itext pdf-generation


    【解决方案1】:

    在下面找到源代码。

    为了一些解释。 首先,我创建了几个变量,例如 folder1、folder2InsideFolder2 和 fileName。 生成的结构将如下所示:folder1\folder2\pdffile.pdf

    您基本上可以将我的变量设置为您已经获得的输入,例如机器 ID、客户端 ID 等。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        class Program
        {
            public static void Main()
            {
    
                string folder1 = "DocumentName"; // AKA Machine ID
                string folder2InsideFolder1 = "SomeID"; // AKA Client ID
                string fileName = "FancyPDFDocument2000.pdf";
    
                // The next line is building a of the two outside folders where the .pdf file will be placed in
                string directoryName = Path.Combine(folder1, folder2InsideFolder1);
                // directoryName is now: DocumentName\SomeID\
    
                // Let's try to create the directory
                try
                {
                    // This line creates the directory using the path that was just created
                    Directory.CreateDirectory(directoryName);
                }
                catch(Exception e)
                {
                    // Risen when directory couldn't be created see documentation of Directory.CreateDirectory
                }
                
                // now create the final path consisting of both directories and the .pdf file name
                string finalFilePath = Path.Combine(directoryName, fileName);
                // finalFilePath is now: DocumentName\SomeID\FancyPDFDocument2000.pdf
    
                // From here on you can use your remaining code to generate the .pdf file you can use the variable finalFilePath for that
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的意见!我尝试实现您的代码,但无法真正完成,您的代码中创建的文件夹和文件是什么时候?我似乎找不到他们。这应该在用户单击“保存”按钮后发生,但是如何将您给我的此代码连接到该按钮?再次感谢,
    • 我刚刚用更多解释更新了我的解决方案,并在我的示例中添加了更多 cmets。希望有帮助
    • 恐怕这是个误会...操作人员想要创建一个投资组合 pdf,其中有用于排列附件的文件夹。这与文件系统中的文件夹无关。
    • 啊,好吧,也许你是对的。很难理解 op 实际需要什么
    • 是的,我真的很抱歉我的错误解释,但英语不是我的母语,而且我的编程知识非常低 我假装我的程序在没有用户输入的情况下做什么(除了点击在我的界面上的保存按钮上)是创建一个 pdf 并将其放在文件夹内的文件夹中(程序在按下保存时也会创建)
    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    相关资源
    最近更新 更多