private DTO.Document UploadDocument(string documentName, string filePath, string fileContentType, nAssembla.DTO.Enums.AttachableType attachableType, string attachableTypeId) { var url = base.GetRequestUri().ToString(); string fileParamName = "document[file]"; NameValueCollection otherFields = new NameValueCollection(); otherFields.Add("document[name]", documentName); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; Stream rs = wr.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in otherFields.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, otherFields[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, fileParamName, filePath, fileContentType); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); } fileStream.Close(); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); rs.Close(); DTO.Document doc = new DTO.Document(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); } catch (Exception ex) { if (wresp != null) { wresp.Close(); wresp = null; } } finally { wr = null; } return(doc); }
public static bool Render(string fileName, DTO.Document source, Common.ImageSizeMode sizeMode) { try { Document document = new Document(); document.Info.Title = source.Title; document.Info.Author = source.Author; document.Info.Subject = source.Subject; document.Info.Keywords = source.Keywords; document.DefaultPageSetup.TopMargin = 0; document.DefaultPageSetup.BottomMargin = 0; document.DefaultPageSetup.LeftMargin = 0; document.DefaultPageSetup.RightMargin = 0; for (int i = 0; i < source.Pages.Count; i++) { Section section = document.AddSection(); var image = section.AddImage(source.Pages[i].Path); if (sizeMode == Common.ImageSizeMode.Fit) { double fRatioWidth = (double)document.DefaultPageSetup.PageWidth / (double)source.Pages[i].Width; double fRatioHeight = (double)document.DefaultPageSetup.PageHeight / (double)source.Pages[i].Height; double fRatio = (fRatioWidth < fRatioHeight) ? fRatioWidth : fRatioHeight; int iWidth = (int)(source.Pages[i].Width * fRatio); int iHeight = (int)(source.Pages[i].Height * fRatio); int iTop = (document.DefaultPageSetup.PageHeight > iHeight) ? (int)((document.DefaultPageSetup.PageHeight - iHeight) / 2) : 0; int iLeft = (document.DefaultPageSetup.PageWidth > iWidth) ? (int)((document.DefaultPageSetup.PageWidth - iWidth) / 2) : 0; image.Top = iTop; image.Left = iLeft; image.Width = iWidth; image.Height = iHeight; } else { image.Top = 0; image.Left = 0; image.Width = document.DefaultPageSetup.PageWidth; image.Height = document.DefaultPageSetup.PageHeight; } } document.UseCmykColor = true; bool unicode = false; PdfFontEmbedding embedding = PdfFontEmbedding.None; PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save(fileName); return(true); } catch (Exception error) { Log.Error(error); } return(false); }