当前位置: 首页 > 知识库问答 >
问题:

为目录中的pdf文件分配/写入标题

仰钧
2023-03-14

嗨,我有一个C#代码片段,它将分配标题的pdf文件。但我试图做同样的每一个pdf文件在一个目录。有谁可以帮助我…?

下面是代码片段“PdfReader PdfReader=new PdfReader(filePath);using(FileStream FileStream=new FileStream(newFilePath,filemode.create,fileaccess.write)){string title=PdfReader.info[”title“]as string;trace.writeline(”existing title:“+title);

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream); 

        Hashtable newInfo = pdfReader.Info; 

        newInfo["Title"] = "New title"; 

        pdfStamper.MoreInfo = newInfo; 

        pdfReader.Close(); 
        pdfStamper.Close(); 
    } 

共有1个答案

上官高畅
2023-03-14

试试这个代码,希望对你有用...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string inputFile = Path.Combine(workingFolder, "Input.pdf");
            string outputFile = Path.Combine(workingFolder, "Output.pdf");

            PdfReader reader = new PdfReader(inputFile);
            using(FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)){
                using (PdfStamper stamper = new PdfStamper(reader, fs))
                {
                    Dictionary<String, String> info = reader.Info;
                    info.Add("Title", "New title");

                    stamper.MoreInfo = info;
                    stamper.Close();
                }
            }

            this.Close();
        }
    }
}
 类似资料:
  • 问题内容: 我在下面的代码中将一些文本存储在主目录中的〜/ .boto文件中。 但是我得到这个错误: 这是代码: 问题答案: 您需要使用os.path.expanduser并使用以下命令打开以进行编写: os.path.expanduser(路径) 在Unix和Windows上,返回带有〜或〜user初始部分的参数替换为该用户的主目录。 在Unix上,如果设置了首字母〜,则由环境变量HOME代替;

  • 我无法在缓存目录中写入文件 使用context.filesdir(但不使用context.cachedir)可以很好地工作。我正在使用libaums库读取USB文件。

  • 我一直在使用PDF文件中的命名目标来打开文件中特定位置的PDF文件。负责生成PDF文档的团队使用工具从图书标记自动生成命名目的地,因此命名目的地的名称往往类似于*9\u Glossary*或*Additional\u Information*。我们被要求用多种语言制作相同的文档。我预计我们将收到多种外语的PDF文档,在相同的位置带有书签,但图书标记的名称当然将使用这些其他语言,自动生成的命名目的地

  • 问题内容: 为什么不行: 该代码可以正常工作,但无法打开.pdf文件。普通文本文件和pdf有什么区别?如果我想在python中创建并写入pdf文件怎么办? 问题答案: 您可以安装fpdf库,然后:

  • 读写文件是最常见的 IO 操作。通常,我们使用 input 从控制台读取输入,使用 print 将内容输出到控制台。实际上,我们也经常从文件读取输入,将内容写到文件。 读文件 在 Python 中,读文件主要分为三个步骤: 打开文件 读取内容 关闭文件 一般使用形式如下: try: f = open('/path/to/file', 'r') # 打开文件 data = f.

  • 本文向大家介绍php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中,包括了php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中的使用技巧和注意事项,需要的朋友参考一下 php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中 实例代码: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!