2011-02-24 883 views
9

我使用itextsharp在asp.net c#中生成pdf文件。我無法繪製水平線/垂直線/虛線。使用itextsharp在pdf文件中繪製一條線的問題

我嘗試使用下面的代碼,我得到沒有錯誤畫一條線,但該線還沒有得到顯示在PDF文件

PdfContentByte cb = wri.DirectContent; 
    cb.SetLineWidth(2.0f); // Make a bit thicker than 1.0 default 
    cb.MoveTo(20, pdfDocument.Top - 40f); 
    cb.LineTo(400, pdfDocument.Top - 40f); 
    cb.Stroke(); 

中有什麼code.Is它,因爲這個問題xy座標的位置?我用粗略的點來了解pdf中的大概位置,但是這行不會在pdf文件中出現。

我正在尋找的輸出如下圖所示。 enter image description here

+3

以防萬一:你是不是要寫線成PDF纂信息?您需要實際刪除其他行下的文本內容,否則人們仍然可以從PDF中提取它。 – Rup 2011-02-24 10:35:37

回答

5

你應該始終確保設定您所執行操作的顏色,否則你不會知道你會得到什麼(這將是從任何執行之前的操作)。嘗試做cb.setStrokeColor(255,0,0)(純紅色),直到找到你想要的線爲止。

+0

適合我。你擊敗了我。 – 2011-02-24 15:23:47

+0

錯誤:沒有重載方法 'SetColorStroke' 需要 '3' 的論點 – Ishan 2011-02-28 07:44:07

+2

作爲5.3.2.0版本中,你可以使用 cb.SetColorStroke(新BaseColor(255,0,0)); – Maciej 2012-09-13 14:30:57

0

你知道在iTextsharp中,座標系統從左下角開始向上運行 - 你確定你的線路沒有被拉得更遠嗎?

+0

我知道在iTextsharp中,座標系統從左下角向上運行,是的,我檢查了整個頁面。 – Ishan 2011-02-24 10:52:09

3

您確定pdfDocument.Top正在返回一個值嗎? 我用PageSize.Width and PageSize.Height

iTextSharp.text.Document myDocument = new Document(PageSize.A4); 
PdfContentByte contentByte = writer.DirectContent; 
contentByte.SetLineWidth(1); 
contentByte.MoveTo(0, 14); 
contentByte.LineTo(myDocument.PageSize.Width,14); 
contentByte.Stroke(); 
5

iTextSharp的畫線: -

Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1) 
pdfDoc.Add(New Chunk(line1)) 
0

我結束了使用底座上面裝有lessly沿着答案的組合。使用StringBuilder函數,您可以阻止任何事情,然後手動繪製一條線,除非您有一個表格單元格與一個單詞一起佔用TD標籤的所有寬度。

StringBuilder chistHeader = new StringBuilder(); 
StringBuilder chistCourses = new StringBuilder(); 

HttpContext.Current.Response.ContentType = "application/pdf"; 
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=CourseHistory.pdf"); 
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

Document pdfDoc = new Document(); 
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream); 

pdfDoc.Open(); 

chistHeader = CourseHistoryHeader(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory"); 
chistCourses = CourseHistoryCourses(Convert.ToInt32(hdUserID.Value), hdSystemPath.Value, "CourseHistory"); 



     //write header for the pdf 
foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistHeader.ToString()), new StyleSheet())) 
    { 
     pdfDoc.Add(element); 
    } 

//have to manually draw a line this way as ItextSharp doesn't allow a <hr> tag.... 
iTextSharp.text.pdf.draw.LineSeparator line1 = new iTextSharp.text.pdf.draw.LineSeparator(1f, 100f, BaseColor.BLACK, Element.ALIGN_LEFT, 1); 
pdfDoc.Add(new Chunk(line1)); 

//write out the list of courses 
foreach (IElement element in HTMLWorker.ParseToList(new StringReader(chistCourses.ToString()), new StyleSheet())) 
    { 
     pdfDoc.Add(element); 
    } 

pdfDoc.Close(); 

HttpContext.Current.Response.Write(pdfDoc); 
HttpContext.Current.Response.End(); 
13
Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1))); 
document.Add(p); 
-2
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.Black, Element.ALIGN_LEFT, 1) 
pdfDoc.Add(New Chunk(line1)) 
+2

是不是我的downvote,但不是那樣[lessly的答案](http://stackoverflow.com/a/15223633)?爲什麼這更好? – Rup 2014-10-07 11:44:16