private void convertPDF() { DirectoryInfo di = new DirectoryInfo(_imagepath); FileInfo[] files = di.GetFiles("*" + ImageType); Array.Sort(files, mySort); Document document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0); try { PdfWriter pdfWrite = PdfWriter.GetInstance(document, new FileStream(PDFFilePath, FileMode.Create, FileAccess.ReadWrite)); document.Open(); iTextSharp.text.Image image; document.AddTitle(_book.Title); foreach (FileInfo file in files) { image = iTextSharp.text.Image.GetInstance(file.FullName); document.SetPageSize(new iTextSharp.text.Rectangle(image.Width, image.Height)); image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; document.NewPage(); document.Add(image); } try { BookContentNode content = BookContent.GetBookContent(_book.did, _book.PdgPath); PdfContentByte pdfcontent = pdfWrite.DirectContent; AddBookContents(pdfWrite, pdfcontent.RootOutline, content); } catch (Exception e) { if (e is ThreadAbortException) { throw e; } } Directory.Delete(_imagepath, true); } catch (Exception e) { if (e is ThreadAbortException) { throw e; } throw new Exception("转换PDF时出错"); } finally { if (document != null && document.IsOpen()) { document.Close(); } } }
public static BookContentNode GetBookContent(String did, String codepdgpath) { String host = DataExtraction.GetPdgHost(did); String pdgpath = DecodePdgPath(codepdgpath); String path = "http://" + host + "/" + pdgpath + "BookContents.dat"; Stream stream = HttpWebResponseUtility.CreateGetHttpResponse(path); stream.Read(new byte[0x28], 0, 0x28); StreamReader reader = new StreamReader(ZipUnCode(stream), Encoding.Default); BookContentNode root = new BookContentNode() { Parent = null }; root.Bookcontent = new BookContentInfo() { Title = "root", Lever = "", Page = 0, Reserved = null, Type = 0 }; BookContentNode curnode = root; while (!reader.EndOfStream) { BookContentNode newnode = new BookContentNode(); String str = reader.ReadLine().Trim(); String[] strlist = str.Split('|'); newnode.Bookcontent = new BookContentInfo() { Title = strlist[0].Trim(), Lever = strlist[1].Trim(), Page = Int32.Parse(strlist[2].Trim()), Reserved = strlist[3].Trim(), Type = Int32.Parse(strlist[4].Trim()) }; int newIndentation = newnode.Bookcontent.Lever.Length; int curIndentation = curnode.Bookcontent.Lever.Length; if (newIndentation <= curIndentation) { curnode = curnode.Parent; for (int i = newIndentation; i != curIndentation; i += 2) { curnode = curnode.Parent; } } newnode.Parent = curnode; curnode.Children.Add(newnode); curnode = newnode; } return(root); }
public void AddBookContents(PdfWriter writer, PdfOutline outline, BookContentNode content) { foreach (BookContentNode contentnode in content.Children) { int page = contentnode.Bookcontent.Page; if (page > _book.PagesNum) { break; } page += _frontpagesinfo.DownloadedPages; PdfAction action = PdfAction.GotoLocalPage(page, new PdfDestination(PdfDestination.FITB), writer); AddBookContents(writer, new PdfOutline(outline, action, contentnode.Bookcontent.Title), contentnode); } }
public static BookContentNode GetBookContent(String did, String codepdgpath) { String host = DataExtraction.GetPdgHost(did); String pdgpath = DecodePdgPath(codepdgpath); String path = "http://" + host + "/" + pdgpath + "BookContents.dat"; Stream stream = HttpWebResponseUtility.CreateGetHttpResponse(path); stream.Read(new byte[0x28], 0, 0x28); StreamReader reader = new StreamReader(ZipUnCode(stream),Encoding.Default); BookContentNode root = new BookContentNode() { Parent = null }; root.Bookcontent = new BookContentInfo() { Title = "root", Lever = "", Page = 0, Reserved = null, Type = 0 }; BookContentNode curnode = root; while (!reader.EndOfStream) { BookContentNode newnode = new BookContentNode(); String str = reader.ReadLine().Trim(); String[] strlist = str.Split('|'); newnode.Bookcontent = new BookContentInfo() { Title = strlist[0].Trim(), Lever = strlist[1].Trim(), Page = Int32.Parse(strlist[2].Trim()), Reserved = strlist[3].Trim(), Type = Int32.Parse(strlist[4].Trim()) }; int newIndentation = newnode.Bookcontent.Lever.Length; int curIndentation = curnode.Bookcontent.Lever.Length; if(newIndentation <= curIndentation) { curnode = curnode.Parent; for (int i = newIndentation; i != curIndentation; i += 2) { curnode = curnode.Parent; } } newnode.Parent = curnode; curnode.Children.Add(newnode); curnode = newnode; } return root ; }
public void AddBookContents(PdfWriter writer, PdfOutline outline, BookContentNode content) { foreach (BookContentNode contentnode in content.Children) { int page = contentnode.Bookcontent.Page; if (page > _book.PagesNum) break; page += _frontpagesinfo.DownloadedPages; PdfAction action = PdfAction.GotoLocalPage(page, new PdfDestination(PdfDestination.FITB), writer); AddBookContents(writer, new PdfOutline(outline, action, contentnode.Bookcontent.Title), contentnode); } }