2019年8月26日 星期一

[Rgx] 推薦好用且簡單的正規表示 (.+)

平常會有很不少例行重複的工作

例如.

public string AA {get;set;}
public string BBB {get;set;}
public string CCCCC {get;set;}
....

假設你需要複製類似上述,但又遇到欄位長度不一致, 可以試試正規表示式,事半功倍

範例: 有一堆網址,我想要全部加入html 的<a>標籤,且名稱也是網址名稱

該如何處理?

AA.com
BBB.com
CCCC.com
DD.com
E.com
FF.com
GGGGG.com

完成後











2019年8月7日 星期三

[C#] console 實現全域變數



簡單的方式就是實做一個class, 裡面宣告一個靜態(static)的欄位即可達到共用變數效果

public class Global
{
    public static string _content = "";
}

2019年8月5日 星期一

[PDF] Itextsharp rotate 轉置角度


最近遇到的問題 : 合併多筆PDF文件,但內容裡面有轉90度跟270度怎麼辦?


第一種,使用pdfReader直接轉, 但如果後續還要進行頁碼等處理則不建議

主要就這句 page.Put(PdfName.ROTATE, new PdfNumber(rotation));

提供rotation 要旋轉的角度

而我有一個參數rotations只是有事先記錄該pdf本身選轉的角度

可以直接使用int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;

替代即可

public void RotatePDFFiles_bak(string fileList, string outMergeFile, List<int> rotations)
{
     PdfReader reader = new PdfReader(fileList);
    int pagesCount = reader.NumberOfPages;
 
    for (int n = 1; n <= pagesCount; n++)
    {
         PdfDictionary page = reader.GetPageN(n);
         PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);
        //int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;
        int rotation = 0;
        if (rotations[n - 1] == 90)
        {
            rotation = 180; //(90 + 90) % 360; //向左橫置的pdf逆轉90
        }
        else if (rotations[n - 1] == 270)
        {
            rotation = 0; // (270 + 90) % 360;
        }
        page.Put(PdfName.ROTATE, new PdfNumber(rotation));
    }
 
    PdfStamper stamper = new PdfStamper(reader, new FileStream(outMergeFile, FileMode.Create));
    stamper.Close();
    reader.Close();
}


2019年8月2日 星期五

[C#] MVC 下載檔案

利用session 暫存檔案 然後讓html 呼叫
注意這是有缺點的,不建議拿來存放大檔案會出現OOM
建議還是直接使用<a  href="" downloads> 直接下載


action1

var pdfFile = "xxx.pdf";
FileContentResult file = File(pdfFile, "application/vnd.ms-excel");
Session["pdfFile"] = file; // 將檔案儲存在 Session

2019年7月24日 星期三

[PDF] Itextsharp PDF翻轉成直式


遇到一份PDF有橫有直

並且合併過程中發現橫的PDF會被切掉

因此要判斷是否為橫的PDF 並轉直在合併

public void Rotate(PdfReader reader, string outMergeFile)
{
    int pageCount = reader.NumberOfPages;
    Document document = new Document();
 
    //MemoryStream outputStream = new MemoryStream();//要把PDF寫到哪個串流
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
    document.Open();
    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
    iTextSharp.text.pdf.PdfImportedPage page;
    int rotation;
    int i = 0;
    while (i < pageCount)
    {
        i++;
        var pageSize = reader.GetPageSizeWithRotation(i);
 
        // Pull in the page from the reader
        page = writer.GetImportedPage(reader, i);
        var pageWidth = pageSize.Width;
        var pageHeight = pageSize.Height;
        // 判斷是否為橫的PDF,並轉置
        if (pageWidth > pageHeight)
        {
            var newPageSize = new Rectangle(pageSize.Height, pageSize.Width, 90);
            writer.DirectContent.AddTemplate(page, 0, -1f, 1f, 0, 0, newPageSize.Height);
        }
        else
        {
            writer.DirectContent.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
        }
    }
    document.Close();
}

[Linq] grouy by + dictinoary

每次想分類然後針對分類做事情

總是忘記...

這裡記錄一下

var g =
    model.GroupBy(o => o.id)
   .ToDictionary(o => o.Key, o => o.ToList());

2019年7月23日 星期二

[C#] 16進位數字組成的字串轉換為Byte[]

將拿到的16進位pdf資料,轉為byte,以利後續處理~

//16進位數字組成的字串轉換為Byte[]
public static byte[] StringToByteArray(string hex)
{
    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}


//反向
public static string ByteToHex(this byte[] Bytedata)
{
    return BitConverter.ToString(Bytedata).Replace("-", "");
}