2011-09-11 39 views
6

Tôi đang phát triển một ứng dụng và tôi sử dụng thư viện iTextSharp.Đặt Siêu dữ liệu trong iTextSharp

Tôi cũng đọc iText hoạt động từ Manning để tôi có thể tham khảo.

Trong Chương 12, mã này có mã sau để thay đổi siêu dữ liệu trong Java.

PdfReader reader = new PdfReader(src); 
PdfStamper stamper = 
new PdfStamper(reader, new FileOutputStream(dest)); 
HashMap<String, String> info = reader.getInfo(); 
info.put("Title", "Hello World stamped"); 
info.put("Subject", "Hello World with changed metadata"); 
info.put("Keywords", "iText in Action, PdfStamper"); 
info.put("Creator", "Silly standalone example"); 
info.put("Author", "Also Bruno Lowagie"); 
stamper.setMoreInfo(info); 
stamper.close(); 

Tôi có thể làm tương tự như thế nào trong C#?

Trả lời

11

Chuyển đổi từ Java sang C# thường khá đơn giản. Theo quy ước, các thuộc tính Java sử dụng tiền tố getset để chuyển đổi thành C# bạn chỉ cần thả tiền tố và biến nó thành một cuộc gọi .ter getter/setter. getInfo() trở thành InfosetMoreInfo(info) trở thành MoreInfo = info. Sau đó, bạn chỉ cần chuyển đổi các kiểu Java nguyên gốc sang các kiểu C# tương đương của chúng. Trong trường hợp này, Java FileOutputStream trở thành một .Net FileStreamHashMap<String, String> trở thành một Dictionary<String, String>.

Cuối cùng, tôi đã cập nhật mã để phản ánh các thay đổi gần đây đối với iTextSharp hiện tại (như 5.1.1.0) hiện tại IDisposable ngay bây giờ.

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", "Hello World stamped"); 
        info.Add("Subject", "Hello World with changed metadata"); 
        info.Add("Keywords", "iText in Action, PdfStamper"); 
        info.Add("Creator", "Silly standalone example"); 
        info.Add("Author", "Also Bruno Lowagie"); 
        stamper.MoreInfo = info; 
        stamper.Close(); 
       } 
      } 

      this.Close(); 
     } 
    } 
} 
+1

Đó là. Cảm ơn bạn rất nhiều vì câu trả lời của bạn :) –

7

Tôi chỉ cần thực hiện một này sau khi tìm kiếm nơi ngay trong cửa sổ đồng hồ của đối tượng PdfWriter, nó thay đổi "PDF Creator" trong PDF vì nó không thể truy cập theo mặc định:

private static void ReplacePDFCreator(PdfWriter writer) 
    { 
     Type writerType = writer.GetType(); 
     PropertyInfo writerProperty = writerType.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.PropertyType == typeof(PdfDocument)).FirstOrDefault(); 

     PdfDocument pd = (PdfDocument)writerProperty.GetValue(writer); 
     Type pdType = pd.GetType(); 
     FieldInfo infoProperty = pdType.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p => p.Name == "info").FirstOrDefault(); 
     PdfDocument.PdfInfo pdfInfo = (PdfDocument.PdfInfo)infoProperty.GetValue(pd); 

     PdfString str = new PdfString("YOUR NEW PDF CREATOR HERE"); 
     pdfInfo.Remove(new PdfName("Producer")); 
     pdfInfo.Put(new PdfName("Producer"), str); 
    } 

tôi nhận được một đề nghị từ "@ Yannic-donot-text" và nó là cách sạch !:

private static void ReplacePDFCreator(PdfWriter writer) 
    { 
     writer.Info.Put(new PdfName("Producer"), new PdfString("YOUR NEW PDF CREATOR HERE")); 
    } 

nhiêu tôi tought nó chỉ archievable bởi phản chiếu nhưng tôi đánh giá cao sự hợp tác của người được giáo dục hơn :)

Thx!

+0

Tôi đang cố gắng thay thế Nhà sản xuất PDF bằng cách tiếp cận phản chiếu của bạn. Nó không làm việc cho tôi. Nó đã làm việc cho bạn. – Karan

+0

Tôi không có mã nguồn nữa, nhưng trên một dự án tương tự tôi đã tải xuống nguồn của PdfSharp và chỉnh sửa "ghi đè void PrepareForSave()", như: if (info.Elements ["/ Creator"] == null) info.Creator = "Văn bản của bạn ở đây"; chuỗi str = info.Producer; nếu (str.Length == 0) str = "Văn bản của bạn ở đây"; else if (! Str.StartsWith ("Văn bản của bạn ở đây")) str = "PDFsharp 1.32.2608-g (www.pdfsharp.net) (Gốc:" + str + ")"; – coloboxp

0

public void pdfproperties() 
 
    { 
 
     string inputFile = @"D:\1.pdf"; 
 
     string outputFile = @"D:\48.pdf"; 
 
     PdfReader reader = new PdfReader(inputFile); 
 
     foreach (KeyValuePair<string, string> KV in reader.Info) 
 
     { 
 
      reader.Info.Remove(KV.Key); 
 
     } 
 
     using (FileStream FS = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
 
     { 
 
      using (Document Doc = new Document()) 
 
      { 
 
       using (PdfCopy writer = new PdfCopy(Doc, FS)) 
 
       { 
 
        Doc.Open(); 
 
        Doc.AddTitle("Add Title"); 
 
        Doc.AddSubject("Add Subject"); 
 
        Doc.AddKeywords("Add Keywords"); 
 
        Doc.AddCreator("Application Creator"); 
 
        Doc.AddAuthor("Add Author"); 
 
        for (int i = 1; i <= reader.NumberOfPages; i++) 
 
        { 
 
         writer.AddPage(writer.GetImportedPage(reader, i)); 
 
        } 
 
        writer.Info.Put(new PdfName("Producer"), new PdfString("Producer Name")); 
 
        Doc.Close(); 
 
       } 
 
      } 
 
     } 
 
    }

Các vấn đề liên quan