提问者:小点点

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


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

我在使用微软Access的同时还使用了Windows Forms和itextsharp。我的目标是让程序在用户与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();


            }


        }
    }

我发现的最类似的例子是这里:如何使用iTextsharp将文件插入现有PDF组合的文件夹中,我尝试实现(我认为我可以使用的),但我无法获得任何参考或实现目标的任何实际进展。

    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"
       };
    }

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

提前感谢,

菲利普·阿尔梅达


共1个答案

匿名用户

在下面查找源代码。

为了一些解释。首先,我创建了几个变量,如< code>folder1 、< code > folder 2 inside folder 2 和< code>fileName。生成的结构将如下所示:< code > folder 1 \ folder 2 \ pdf file . 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
        }
    }
}