Một thẻ đã tồn tại với tên chi nhánh được cung cấp. Nhiều lệnh Git chấp nhận cả tên thẻ và tên nhánh, vì vậy việc tạo nhánh này có thể gây ra hành vi không mong muốn. Bạn có chắc chắn muốn tạo nhánh này không?
Gần đây, một vấn đề nhỏ thú vị mà chúng tôi phải thực hiện cho một khách hàng; . chương trình dòng lệnh net
Có rất nhiều thư viện của bên thứ 3 làm điều đó, nhưng thông thường, chúng đắt tiền và không đáng tin cậy lắm, định dạng HTML mà chúng tôi muốn định dạng khá kém. Vì vậy, chúng tôi đã đưa ra điều này
---------------
Public Function ConvertHTML[oldfilename As String, newfilename As String] As String
Dim pngfilename As String = Path.GetTempFileName[]
Dim res As String = "" ' = ok
____1End Function
—————
Để điều này hoạt động, bạn sẽ cần thêm các thư viện sau vào dự án của mình
- PDFSharp [miễn phí sử dụng; tải xuống từ http. //pdfsharp. mật mã. com/phát hành]
- Hệ thống. các cửa sổ. Biểu mẫu [ngay cả khi bạn đang viết ứng dụng bảng điều khiển như chúng tôi]
- Hệ thống. Đang vẽ
Có bắt được không? . Nó sử dụng chức năng không có giấy tờ trong đối tượng WebBrowser [DrawToBitmap], nhưng chức năng đó đã hoạt động bình thường đối với một số bản phát hành của Microsoft về. NET [ít nhất là từ năm 2010] và nếu trang web quá lớn, nó sẽ bị cắt bớt ở 3000px [một hạn chế mà tôi đã tích hợp vào đoạn mã trên để kiểm tra lỗi; bạn có thể xóa nó nếu muốn, mặc dù có giới hạn về kích thước
Mã của chúng tôi ghi vào một trang PDF, hoàn toàn vì đó là những gì chúng tôi cần cho khách hàng của mình – với một chút thao tác hình ảnh, thật đơn giản để trải mã đó trên nhiều trang PDF nếu cần
Gói
Try0 được sử dụng để tạo tệp pdf từ đoạn mã HTML trong C#. Gói này giúp dễ dàng chuyển đổi một chuỗi chứa đoạn mã HTML thành tài liệu PDF chứa chế độ xem web của đoạn mã HTML đó. Gói
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
Try0 là gói bên ngoài và không được cài đặt sẵn thư viện
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
Try2. Chúng tôi phải cài đặt riêng gói này bằng trình quản lý gói NuGet. Lệnh cài đặt gói
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
Try0 được đưa ra bên dưới
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
dotnet add package HtmlRenderer.PdfSharp --version 1.5.0.6
Ví dụ mã sau đây cho chúng ta thấy cách chuyển đổi một chuỗi chứa đoạn mã HTML thành tệp PDF bằng gói
Try0 trong C#
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
Tệp
Try5
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
Trong đoạn mã trên, chúng tôi đã chuyển đổi đoạn mã HTML bên trong biến chuỗi
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
0 thành tệp PDF với gói Try0 trong C#. Trước tiên, chúng tôi đã khởi tạo chuỗi
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While
wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution
wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]
Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]
Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using
End Using
Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
End Try
Return res
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
0 bằng một đoạn mã HTML. Sau đó, chúng tôi đã tạo một thể hiện của lớp using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
3 với hàm using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
4 trong C#. Cuối cùng, chúng tôi đã lưu tài liệu vào một đường dẫn được chỉ định bằng hàm using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace html_to_pdf
{
class Program
{
static void Main[string[] args]
{
string htmlString = "Document This is an HTML document which is converted to a pdf file.
";
PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4];
pdfDocument.Save["C:/File/HTML to PDF Document.pdf"];
}
}
}
5. Nội dung tệp có thể được nhìn thấy trong đoạn mã màn hình được hiển thị ở trên