C# itextsharp - 创建 PDF,在文件夹中创建文件夹,将 PDF 放在最深的文件夹中

C# itextsharp - Create PDF, Create Folder within Folder, Place PDF in deepest Folder

我是 C# 的完全初学者,我正在寻找有关我必须进行的这个项目的帮助。

我正在使用 Windows Forms 和 itextsharp 以及 Microsoft Access。我的 objective 是制作程序,在用户与 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"
       };
    }

对于我的知识匮乏以及所提供的信息可能很少,我深表歉意。如果您需要特定的东西,我可以尝试上传更多代码。

提前致谢,

菲利佩·阿尔梅达

找到下面的源代码。

求解释。 首先,我创建了几个变量,例如 folder1folder2InsideFolder2fileName。 生成的结构看起来像这样: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
        }
    }
}