pdf电脑地址转网络地址.txt
程序员文章站
2022-04-11 10:28:15
...
List<LableFileResponse> data = new List<LableFileResponse>();
for (var i = 0; i < listServerCode.Count; i++)
{
if (listServerCode == null || listServerCode.Count == 0)
{
return new LabelResponse()
{
success = 0,
cnmessage = "未获取到标签信息" +
"",
enmessage = "Not get the label information"
};
}
WebHeaderCollection we = new WebHeaderCollection();
we.Add("ClientKey", ClientKey);
we.Add("ClientSecret", ClientSecret);
string strResult = Utils.HttpPost(strUrl, "orderNumber=" + listShipperCode[0], we);
Lable upsresult = JsonHelper.JSONToObject<Lable>(strResult);
List<string> urlList = new List<string>();
//生成pdf 文件地址
string pdf_file_path = string.Format("/api-lable/pdf/{0}/", DateTime.Now.ToString("yyyyMMdd"));
string pdf_file_name_pdf = string.Format("{0}.pdf", System.Guid.NewGuid().ToString());
string pdf_output_filepath = System.Web.HttpContext.Current.Server.MapPath("~" + pdf_file_path);
string strLabelSavePath = pdf_output_filepath + pdf_file_name_pdf;
string strDirectory = Path.GetDirectoryName(strLabelSavePath);
if (!Directory.Exists(strDirectory))
{
Directory.CreateDirectory(strDirectory);
}
byte[] bytes = Convert.FromBase64String(upsresult.data.image);
System.IO.FileStream stream =
new FileStream(strLabelSavePath, FileMode.CreateNew);
System.IO.BinaryWriter writer =
new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
png图片转pdf
//var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
//using (var stream = new FileStream(strLabelSavePath, FileMode.Create, FileAccess.Write, FileShare.None))
//{
// PdfWriter.GetInstance(document, stream);
// document.Open();
// using (var imageStream = new FileStream(pngPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
// {
// var image = iTextSharp.text.Image.GetInstance(imageStream);
// if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
// {
// image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
// }
// else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
// {
// image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
// }
// image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
// document.NewPage();
// document.Add(image);
// }
// document.Close();
//}
urlList.Add(strLabelSavePath);
string lable_file = string.Empty;
//多个合成一个pdf的地址
string filename_pdf = string.Format("{0}.pdf", System.Guid.NewGuid().ToString());
string file_path_pdf = string.Format("/api-lable/pdf/{0}/", DateTime.Now.ToString("yyyyMMdd"));
string output_filepath_pdf = System.Web.HttpContext.Current.Server.MapPath("~" + file_path_pdf);
string strLabelSavePathPdf = output_filepath_pdf + filename_pdf; //file_path + filename;//
using (FileStream targetStream = new FileStream(strLabelSavePathPdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
targetStream.Close();
}
PDFHeper.MergePages(strLabelSavePathPdf, urlList.ToArray());
List<string> logistics_codes = new List<string>();
logistics_codes.Add(listServerCode[0]);
lable_file = "http://" + DTRequest.GetCurrentFullHost() + file_path_pdf + filename_pdf;
LableFileResponse datas = new LableFileResponse() { lable_file_type = LabelPrintConst.FILE_TYPE_2, lable_file = lable_file, logistics_codes = logistics_codes };
data.Add(datas);
}
if (data.Count > 1)
{
List<string> sti = new List<string>();
for (var i = 0; i < data.Count; i++)
{
sti.Add(data[i].lable_file);
}
string pdf_file_path1 = string.Format("/api-lable/pdf/{0}/", DateTime.Now.ToString("yyyyMMdd"));
string pdf_file_name1 = string.Format("{0}.pdf", System.Guid.NewGuid().ToString());
string labelUrl1 = "~" + pdf_file_path1 + pdf_file_name1;
labelUrl1 = pdf_file_path1 + pdf_file_name1;
//PDFHeper.MergePDFFiles(sti, labelUrl);
labelUrl1 = System.Web.HttpContext.Current.Server.MapPath("~" + labelUrl1);
//sti.Add(labelUrl1);
PDFHeper.MergePages(labelUrl1, sti.ToArray());
labelUrl1 = "http://" + DTRequest.GetCurrentFullHost() + pdf_file_path1 + pdf_file_name1;
return new LabelResponse() { success = 1, data = new List<LableFileResponse>() { new LableFileResponse() { lable_file_type = LabelPrintConst.FILE_TYPE_2, lable_file = labelUrl1 } } };
}
else if (data.Count == 1)
{
return new LabelResponse() { success = 1, data = data };
}
public static void MergePages(string outputPdfPath, string[] lstFiles)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage;
sourceDocument = new Document();
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
try
{
foreach (string item in lstFiles)
{
reader = new PdfReader(item);
int pages = reader.NumberOfPages;
//Add pages of current file
for (int i = 1; i <= pages; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
reader.Close();
}
sourceDocument.Close();
}
catch (Exception ex)
{
throw ex;
}
}