static void test01(string file)
{
var db = RedisWrite.Db;
if (!File.Exists(file))
{
return;
}
var reader = new iTextSharpPdf.PdfReader(file);
reader.RemoveUnusedObjects();
long fileSize = reader.FileLength;
int currentPage = 1;
var readerCopy = new iTextSharpPdf.PdfReader(file);
readerCopy.RemoveUnusedObjects();
readerCopy.RemoveAnnotations();
readerCopy.RemoveFields();
readerCopy.RemoveUsageRights();
string CreationDate = "";
foreach (KeyValuePair <string, string> KV in readerCopy.Info)
{
if (KV.Key == "CreationDate")
{
CreationDate = KV.Value;
}
//readerCopy.Info.Remove(KV.Key);
}
//int headerSize = readerCopy.Metadata.Length;
//string mt = Encoding.UTF8.GetString(readerCopy.Metadata);
int max = reader.NumberOfPages;
if (max > 5)
{
max = 2;
}
string key = DocumentStatic.buildId(max, fileSize);
var obj = new Dictionary <string, object>()
{
{ "id", long.Parse(key) },
{ "file_name", Path.GetFileNameWithoutExtension(file) },
{ "file_type", "pdf" },
{ "file_size", fileSize },
{ "file_created", CreationDate },
{ "page", max }
};
string jsonInfo = JsonConvert.SerializeObject(obj);
var bufInfo = ASCIIEncoding.UTF8.GetBytes(jsonInfo);
var lsEntry = new List <NameValueEntry>()
{
new NameValueEntry(0, LZ4.LZ4Codec.Encode(bufInfo, 0, bufInfo.Length))
};
for (int i = 1; i <= max; i++)
{
////using (FileStream fs = new FileStream(@"C:\temp\" + i + "-.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
////{
//// using (var d = new iTextSharpText.Document())
//// {
//// using (var w = new iTextSharpPdf.PdfCopy(d, fs))
//// {
//// d.Open();
//// w.AddPage(w.GetImportedPage(reader, i));
//// d.Close();
//// }
//// }
////}
using (var ms = new MemoryStream())
{
var docCopy = new iTextSharpText.Document(reader.GetPageSizeWithRotation(currentPage));
//var pdfCopy = new iTextSharpPdf.PdfCopy(docCopy, new FileStream(@"C:\temp\" + i + "-.pdf", FileMode.Create));
var pdfCopy = new iTextSharpPdf.PdfCopy(docCopy, ms);
docCopy.Open();
var page = pdfCopy.GetImportedPage(readerCopy, currentPage);
pdfCopy.SetFullCompression();
pdfCopy.AddPage(page);
currentPage += 1;
//long len = ms.Length;
docCopy.Close();
pdfCopy.Close();
//m_app.RedisUpdate(key, i.ToString(), ms.ToArray());
lsEntry.Add(new NameValueEntry(i, ms.ToArray()));
}
}
readerCopy.Close();
reader.Close();
string did = db.StreamAdd("BUF", lsEntry.ToArray(), key + "-0");
}